• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to retreive request object in servlet!!!

 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greeting's Ranchers,

I want to transfer a String in request object from jsp to the servlet. In the jsp I use the following action

<BODY>
<% pageContext.setAttribute("screen","adminscreen",PageContext.REQUEST_SCOPE); %>
<A href="/admin/AdminServlet">Add new user Account</A><BR>
<A href="/admin/AdminServlet">View esisting user Account</A>
</BODY>

In the servlet code I'm simply trying to retreive it as shown below.


===================================
Servlet
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
page = (String) req.getAttribute("screen");
System.out.println("page..."+page);
}
===================================

I'm getting page=null. What is the problem?
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% pageContext.setAttribute("screen","adminscreen",PageContext.REQUEST_SCOPE); %>


This will place the "screen" attribute in request scope for the life time of current request to your JSP page and it will be destroyed when you page is rendered.

Either store the attribute in Session Scope or pass it as a request parameter in the url like


and then retrieve it in your servlet using request.getParameter("screen")
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Ali, but I'm unable to undustand your it; You said that it will place the attribute in request scope for the current request... and then you said that the attribute will be destroyed!!! I mean the whole point of passing the http request object to the server is that we can store the attribute in the request scope and pass it to the server where we can later retreived and the attribute can be extracted!!! Can you please elaborate a little bit more... or you can tell me the page in HF servlet and jsp book where it is a little bit explained more.
 
arun shanmugam kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When the attribute is request scoped then it will live only for that particular request, unless the request is dispached to another servlet in the container. Here the JSP just returns the page to the client and at that point the request is completed and so do the scope the request attribute ends then and there.

Refer servlet 2.4 spec under the topic The Request and SRV4.10 subsection.
[ March 25, 2007: Message edited by: arun s.k ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic