Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet vs JSP

 
swapna Kadali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
swapna Kadali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!