• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing parameter from servlet to jsp

 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write a programm that pass parameter from jsp to servlet and than according to the parameter the servlet get pass back a parameter to the servlet. I think that the first stage work but the second stage does not work. I attach my code hopefully somebody will tell me what wrong with my code.explain to my programm in the first jsp(I dont attach) I have a radio button with value fruit that choose one fruit .
the servlet get the kind of fruit we choose and for example if the fruit is
an apple the servlet sent us to applestore.jsp with attribute to the jsp of appleshop. but it doesnt pass the parameter appleshop from the jsp to the servlrt.please somebody can help me
String fruit = request.getParameter("fruit");
PrintWriter out = response.getWriter();
System.out.println(" your choice "+fruit);

if((fruit).equals("apple")){
fruit="apple";

String apple="appleshop";

request.setAttribute("appleshop",
fruit);
javax.servlet.RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/applestore.jsp");
rd.forward(request, response);
<% String fruit=(String)request.getAttribute("fruit");%>

your choice: <%= request%>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64622
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64622
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but you'll have to be more explicit about what is happening incorrectly.
 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is happening incorrectly is that the jsp page show me
:your choice: org.apache.catalina.core.ApplicationHttpRequest@269997
instead of show your choice: appleshop
(the parameter I send via request).
I will read what you send me about how to send my prorgamm and try again more clearly.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64622
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what you are telling it to display:
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that in your servlet you want to have this code:



and in your JSP you want:



Yes, that is somewhat confusing because:

fruit == "apple"
apple == "appleshop"
${apple} == "appleshop" (${apple} is the value of the attribute named "apple")
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I should not have thrown expression language (EL) usage into this, but after using EL I just can't seem to force myself to use server-side script any more. But if you want to sure server-side script, I think this is what it would look like for the JSP to get the results you want (based on the servlet code I posted previously):


[ September 09, 2008: Message edited by: Peter Johnson ]
 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank you .

I try to make the code you suggest me but still it doesnt work
your choice: null
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to tell the details first .Read this to know why it is important.

I think that the first stage work but the second stage does not work.

That means you get output for

in your servlet.
Then as Peter said your code is confusing, you need proper names for your attributes and variables.
Finally

should work for you.

Hope this helps
 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank you very much.
now it works well.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by alona ariel:
I thank you very much.
now it works well.


You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic