GeeCON Prague 2014*
The moose likes Servlets and the fly likes passing parameter from servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "passing parameter from servlet to jsp" Watch "passing parameter from servlet to jsp" New topic
Author

passing parameter from servlet to jsp

alona ariel
Ranch Hand

Joined: Sep 09, 2008
Posts: 36
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Sorry, but you'll have to be more explicit about what is happening incorrectly.
alona ariel
Ranch Hand

Joined: Sep 09, 2008
Posts: 36
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

That is exactly what you are telling it to display:
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

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")


JBoss In Action
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

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

Joined: Sep 09, 2008
Posts: 36
I thank you .

I try to make the code you suggest me but still it doesnt work
your choice: null
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
alona ariel
Ranch Hand

Joined: Sep 09, 2008
Posts: 36
I thank you very much.
now it works well.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing parameter from servlet to jsp