• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request.setAttribute and getAttribute problem

 
Srinivas Ivaturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 50
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 823
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 5384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic