Forums Register Login
Unable to retreive request object in servlet!!!
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

<% 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>

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

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
page = (String) req.getAttribute("screen");

I'm getting page=null. What is the problem?

<% 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")
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.

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 ]

This thread has been viewed 504 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 20, 2018 06:23:55.