• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet vs JSP

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic