Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope of setAttribute() and getAttribute()

 
sharjeel afzal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scope of setAttribute() and getAttribute()?

can we use setAttribute() and getAttribute() with HttpServletRequst
can we use setAttribute() and getAttribute() with ServletContext
can we use setAttribute() and getAttribute() with HttpSession


what effects... or what is doing on with these
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The answere is 3x yes:
  • Request attributes
    --> accessbile to only thoss with access to a specific ServletRequest
    --> lifetime of the request, which means until the Service's service() method exists
  • Session attributes
    --> accessible to only those with access to a specific HttpSession
    --> lifetime of a session
  • Context attributes
    --> everyone in the application has access
    --> lifetime of an application



  • Regards,
    Rok
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just a hint:

    Dont' get confused with getAttribute() and getParameter() of HttpServletRequest At times people do a mistake and find the value being null.
     
    Saravanan Mahalingam
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A good hint.

    getParameter() returns String. It's actually used to get the queryString appended in the URL or request parameters submitted through form

    In case of getAttribute() accepts Object. So we can keep any subclass of Object in request/session/application
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65227
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guys, he didn't say anything about parameters. Let's not go off on a tangent.
     
    PrasannaKumar Sathiyanantham
    Ranch Hand
    Posts: 110
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are 4 scopes actually..

    Application
    Session
    Request
    Page

    Read about them in this link http://www.jguru.com/faq/view.jsp?EID=53309

    Everything has been described beautifully

     
    Anshul Singhal
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I ve this piece of code in 1.jsp

    <%request.setAttribute("variable","anshul");%>
    <form action="2.jsp">
    <input type="submit" value="Click" />
    </form>

    2.jsp
    <%
    String q = (String)request.getAttribute("variable");
    out.println(q);
    %>

    it is printing null value

    can someone please help..!!!
     
    Vijitha Kumara
    Bartender
    Pie
    Posts: 3918
    10
    Chrome Fedora Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anshul Singhal wrote:I ve this piece of code in 1.jsp
    <%request.setAttribute("variable","anshul");%>
    <form action="2.jsp">
    <input type="submit" value="Click" />
    </form>

    2.jsp
    <%
    String q = (String)request.getAttribute("variable");
    out.println(q);
    %>

    it is printing null value

    Hi Anshul ,
    Welcome to JavaRanch , You may create a new thread for your question(s) since this is bit different from the original question. This "<%request.setAttribute("variable","anshul");%>" works only for the request scope which within the request to the 1.jsp and the response it generates. Once you get the response that attribute is out of scope inside the server. So that won't work. You may store the attribute in a more wider scope that is may be HttpSession, ServletContext but that will depend on your requirement.
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vijitha explained Clearly . otherwise use Hidden field .
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic