• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scope of setAttribute() and getAttribute()

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Guys, he didn't say anything about parameters. Let's not go off on a tangent.
     
    Ranch Hand
    Posts: 110
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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

     
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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..!!!
     
    Bartender
    Posts: 4116
    72
    Mac TypeScript Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vijitha explained Clearly . otherwise use Hidden field .
     
    WARNING! Do not activate jet boots indoors or you will see a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic