wood burning stoves 2.0*
The moose likes JSP and the fly likes calling value from servlet in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

calling value from servlet in jsp

Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Hello Friends,
I am using tomcat and wish to call the value processed in my servlet in jsp using RequestDispatcher.I doing the following:
//in doPost()
//req=request and resp=response objects
String redirecturl = "www.yahoo.com";
//get the requestDispatcher
RequestDispatcher rd = getServletContext().getRequestDispatcher("/logsubmit.jsp");
if (rd != null) {
req.setAttribute("myObject", redirecturl);
rd.forward(req, resp)
}
My question is how do i call the above servlet in my logsubmit.jsp page.Please tell me the exact path as i am getting null value with the forward tag.
Thanks in advance,
Kavita
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Sorry.
Could you please rephrase what you wanted to ask.
Not sure if I understood the qstn.
Thanks.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Well, you wouldn't so much 'call' the servlet from your JSP as y ou would have the JSP collect some data, and that JSP's form would be submitted to the servlet. This is just like any other form processing.

Then from the servlet, it's forwarded wherever you send it through the request dispatcher.
Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Hello,
First of all thanks Madhav and Mike for the reply.I am posting my query again.Sorry for not being clear before.The query is:
I am passing a value(user) to my servlet through a java application.The user can be any user registered or unregistered.If this user has to be a registered user then i accept the username and password thru normal jsp.If the user is found valid then he is authorized to view a specific
page(say yahoo.com).Now I want this yahoo.com value which is there in my servlet to be made available in my jsp so that as soon as the user is autheticated i open the yahoo.com website
using the redirect option.
Through the previous posts i found out that RequestDispatcher is a suitable option for this kind
of work.So i used the foll. code in the doPost() method of my servlet :
String redirecturl = "www.yahoo.com";
//get the requestDispatcher
RequestDispatcher rd = getServletContext().getRequestDispatcher("/logsubmit.jsp");
if (rd != null) {
req.setAttribute("myObject", redirecturl);
rd.forward(req, resp);
}
In my logsubmit.jsp page i retrieve myobject value which is yahoo.com as under:
String myvalue = (String)request.getAttribute("myObject");
But there is no value in myvalue.It contains null
My question is how do i do that(pass myobject value from servlet to jsp) bcoz when i try to retreive it i get null value.The reason can be may be i am not calling my servlet correctly.Not
sure.

Kavita.
Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Please Reply!!!
Kavita
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Don't be at us...or is it Halloween?
Now, if you read the javax.servlet.ServletRequest,javax.servlet.ServletResponse)" TARGET=_blank rel="nofollow">
API for the rd.forward() correctly, please note the foll. sentence at the end:

The request and response parameters must be the same objects as were passed to the calling servlet's service method.

Which tells me that you cannot change the request obj in your servlet before forward()ing it to the JSP.
Anyone else interpreting it differently ?
regds.
- satya

ps: Can't get to post the URL correctly.
[This message has been edited by Madhav Lakkapragada (edited October 30, 2001).]
sanjay yermalkar
Greenhorn

Joined: Sep 19, 2001
Posts: 23
kavita,
It's true that one cannot change the request parameters (without using filters) but one can add attributes to the existing request. Your servlet and jsp worked when I tried them in my servlet engine.
I'll just suggest to check the servlet code.
:-)
[This message has been edited by sanjay yermalkar (edited October 30, 2001).]
swapna Kadali
Greenhorn

Joined: Oct 06, 2003
Posts: 22
Hello everbody,
I have the same problem which kavitha has,
but i just started learning java, so i was just trying to pass simple value from servlet to JSP.
My code in servlet is:
ServletContext context = this.getServletContext();
RequestDispatcher dispatch = context.getRequestDispatcher("/HelloJsp.jsp");
request.setAttribute("MyName", "Swapna");
dispatch.forward(request, response);
and My code in JSP is:
Hello <%= (String) request.getAttribute("MyName") %>
I use NetBeans IDE and Tomcat server. JSP page is returning NULL value as Hello null.
swapna Kadali
Greenhorn

Joined: Oct 06, 2003
Posts: 22
Pls... help me
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

swapna, this is the same question you asked in this topic which has new reponses that you need to address.
Please ask your questions in one place, and one place only. That way, the answers don't get spread out all over the place.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: calling value from servlet in jsp