aspose file tools
The moose likes JSP and the fly likes Servlet vs JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Servlet vs JSP" Watch "Servlet vs JSP" New topic
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
    
  13

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
    
  13

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
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Servlet vs JSP
 
Similar Threads
getting data from java classes
calling value from servlet in jsp
Question on EL: If it’s NOT a String literal, it’s evaluated
Problem with Jsp Init parameter
Servlet reporting progress