Bookmark Topic Watch Topic
  • New Topic

calling value from servlet in jsp

 
Kavita Ghia
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator

Sorry.
Could you please rephrase what you wanted to ask.
Not sure if I understood the qstn.
Thanks.
- satya
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please Reply!!!
Kavita
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Don't be at us...or is it Halloween?
Now, if you read the
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Pls... help me
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
    Bookmark Topic Watch Topic
  • New Topic