• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing parameter from servlet to jsp

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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%>
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is exactly what you are telling it to display:
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thank you very much.
now it works well.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by alona ariel:
I thank you very much.
now it works well.



You are welcome
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic