aspose file tools*
The moose likes Servlets and the fly likes request.setAttribute and getAttribute problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.setAttribute and getAttribute problem" Watch "request.setAttribute and getAttribute problem" New topic
Author

request.setAttribute and getAttribute problem

Srinivas Ivaturi
Ranch Hand

Joined: Jan 28, 2003
Posts: 50
Hi

I have a problem in accessing the attribute set in JSP while accessing in Servlet. here is the code.

Here is the JSP page that sets an attribute.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<%
request.setAttribute("akp","AKP");
%>
<BODY>
<form name=testSubmit method=post action="TestServlet">
<input type=text name=testText value="">
<input type=hidden name=hidSubmit value=4>
<input type=hidden name=akp value=4>
<input type=submit name=submit >
</form>
</BODY>
</HTML>

When I click on submit, It is submitted to TestServlet. There I am trying to access the attribute. But I am getting null. Any wrong way I am doing.


Here is the servlet Code.

protected void doGet(HttpServletRequest request, HttpServletResponse res)
throws ServletException, IOException
{
if( (request.getParameter(ExportConstants.HIDSUBMIT) != null) &&
(request.getParameter(ExportConstants.HIDSUBMIT).equals("4")) )
{
System.out.println("Request Attribute :: " + (String)request.getAttribute("akp"));
}
}

Thanks for your time and help
Srinivas Ivaturi
Pandurangan Rakesh
Greenhorn

Joined: Jun 06, 2004
Posts: 24
Hi

I think the problem may be trivial..you have given the hidden field with name="hidSubmit"..ok..but in your servlet u access as "HIDSUBMIT"....may be u shud access as request.getParameter("ExportConstant.hidSubmit)..

Regards
Rakesh
Srinivas Ivaturi
Ranch Hand

Joined: Jan 28, 2003
Posts: 50
if( (request.getParameter("hidSubmit") != null) &&
(request.getParameter("hidSubmit").equals("4")) )
{
System.out.println("Request Attribute :: " + (String)request.getAttribute("akp"));
}

I removes that Hidden field in JSP page and trying to access in servlet as above. Even then I could not able to get the value. I am still getting null.

Thanks
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
we can't set attribute inside jsp like this:
request.setAttribute(Obj, Obj),
you will obviuosly get null.

not so sure about it, but it would be like the same.
Srinivas Ivaturi
Ranch Hand

Joined: Jan 28, 2003
Posts: 50
Can you tell me then how to pass the value/object from JSP to servlet? I dont want to use session. I want to do it only with request object.

Thanks
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
you may try hidden fields inside form. i mean just set the value of that hidden field. and to get the value in your servlet or action class use:

request.getParameter("field")

hope this will help you.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Srinivas Ivaturi,

If you want to pass additional parameters when a request is submitted from a JSP to a servlet the way to do it is using hidden fields on your form, as Adeel said.

If you're interested in why your attempt to use request.setAttribute() doesn't work, consider this:
  • JSP is requested in the browser address bar (say)
  • JSP container receives request and processes JSP including request.setAttribute() which has request scope, i.e. only this request.
  • Response is served to the browser
  • Client submits the form to the servlet
  • Servlet receives all form parameters in the request, but no akp attribute as that was only in the previous request
  • Servlet queries the value of akp attribute: it's null as it doesn't exist in this request
  • Response is served to the browser
  • Srinivas Ivaturi is not happy!


  • Hope that helps.

    Jules
    Srinivas Ivaturi
    Ranch Hand

    Joined: Jan 28, 2003
    Posts: 50
    Thank for you answers. I am still wondering and there should be a way to do this. Anyway I am unahppy .
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    There are two. Use session instead of request scope (wrong way). Use a hidden form field for akp - right way.

    Jules
    R K Singh
    Ranch Hand

    Joined: Oct 15, 2001
    Posts: 5371
    Originally posted by Srinivas Ivaturi:
    Anyway I am unahppy .


    While using JSP, lot of people get confuse about the part that will be executed on server and the part that will be executed on client i.e. browser(I.E., Netscape, Mozila etc.)

    req.setAttribute(obj, obj) is executed on the server and once a response is sent to browser, scope of "request" object is over as it has executed the request.

    When you submit your html page (Note: not a jsp) from browser to your Servlet, that is a new "request". New request does not know anything about previous request as HTTP is stateless protocol.
    So servlet cant retrieve the attribute that you have set in JSP.

    I will recommend you to read more about HTTP being stateless and the methods of maintaining session in HTTP.

    Servlet provides a method to maintain session on server. You can use HttpSession object to set attribute.

    But before you use session object, better know why you are using it and how it maintains session.

    All The Best.
     
    Don't get me started about those stupid light bulbs.
     
    subject: request.setAttribute and getAttribute problem