| Author |
Servlet vs JSP
|
swapna Kadali
Greenhorn
Joined: Oct 06, 2003
Posts: 22
|
|
Hi, I am trying send some value from Servlet to JSP page, where JSP executes to show the value to the user. I tried following code in my doGet() method of servlet: ServletContext context = this.getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher"/HelloNewjsp.jsp"); request.setAttribute("MyName", "Swapna"); dispatch.forward(request, response); and Following code in my JSP Page which is: HelloNewjsp.jsp Hello <%= (String)request.getAttribute("MyName") %> The output i am getting on executing JSP page is : Hello null and the output i am getting on executing Servlet is: Hello Swapna Can anybody help me out why JSP Page is behaving like that. I use NetBeans IDE and Tomcat Server. Thanks, Swapna.
|
 |
Anthony Watson
Ranch Hand
Joined: Sep 25, 2003
Posts: 327
|
|
|
A request is considered invalid after content has been written to the response. Make sure that nothing is written to the response before you try to access the request object.
|
Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
|
 |
Sainudheen Mydeen
Ranch Hand
Joined: Aug 18, 2003
Posts: 218
|
|
Hi Swapna
Originally posted by swapna Kadali: The output i am getting on executing JSP page is : Hello null and the output i am getting on executing Servlet is: Hello Swapna
Obviously this is the result you should expect. Your request attribute is set only in your servlet. So when you acess your JSP directly it has no request attribute "MyName" set. -Sainudheen
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56151
|
|
The output i am getting on executing JSP page is : Hello null
If by this you mean that you are hitting the JSP page without going through the servlet, then of course Sainudheen is correct. If this is not what you mean, further investigation is necessary. bear
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
swapna Kadali
Greenhorn
Joined: Oct 06, 2003
Posts: 22
|
|
Hi Bear Bibeault and sainudheen, Thanks for the reply, I am trying to execute JSP Page through servlet. I need servlet to pass values to JSP and JSP to display it, this is according to Model2 if i am not wrong. Cheers, Swapna.
|
 |
swapna Kadali
Greenhorn
Joined: Oct 06, 2003
Posts: 22
|
|
Hi Anthony Watson, Can you pls.. elaborate more on this. I think I am doing something wrong. Thanks, Swapna A request is considered invalid after content has been written to the response. Make sure that nothing is written to the response before you try to access the request object.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56151
|
|
I am trying to execute JSP Page through servlet.
In that case, Anthony may have you on the right track. Are you trying to write output before forwarding to the JSP page? bear
|
 |
 |
|
|
subject: Servlet vs JSP
|
|
|