Request.setAttribute vs session.setAttribute

When do you use one over the other?

Since request and session are both scopes within J2EE you have to ask yourself, how long is the data valid for ? If it is just valid for one request best to use that scope, if necessary to be available to the user across their session then use session. If necessary for use by all users then use application (SevletContext) scope.
Where poss use request though as with multiple users all using session variables memory usage on the server increases.
Hope that helps.

Similar Messages

  • Request.setAttribute between classes

    Hi I'm trying to keep a userid from a class call ActionLogin to then use it to select products bought by that user
    request.setAttribute("userid", login);Then I realize that userid isn't keeping anything when I'm tryin got access it with
    request.getParameter("userid") or
    request.getAttribute("userid") I just get a null string when I do this from another class or that same class.. am I supposed to use something else?

    Attributes which are set in the ServletRequest have a lifetime of one request. In your case, rather use session instead.
    And there is a certain difference between parameters and attributes.

  • Purpose of request.setAttribute(String parm1, Object parm2)

    HttpServletRequest request.setAttribute(String parm1, Object parm2) vs. HttpSession session.setAttribute(String parm1, Object parm2)
    hi
    can anybody please explain to me what does HttpServletRequest request.setAttribute() do? and for what purpose is it used for?
    i already know that HttpSession session.setAttribute(String parm1, Object parm2) is used to bind a session to a variable, so that it can be accessed throughout the servlet application.
    thanx
    shankha
    ps: please correct me if i'm wrong

    shankha,
    What does the API doco say?
    I think it just sets a request scoped attribute... ie a request parameter.
    This looks pertinant: http://www.rgagnon.com/javadetails/java-0401.html

  • How does request.setAttribute() works?

    How does request.setAttribute() works?
              Does it append data to http header and forward? Does it set to a memory
              space like session.setAttribute() does?
              Thank you
              Jim
              

              Q. Does it append data to http header and forward?
              No.
              Q. Does it set to a memory space like session.setAttribute() does?
              Yes. So you can set attributes in request scope and use them in the servlets and
              JSPs in the servlet chain via request dispatcher and jsp:include/jsp:forward.
              "Jim" <[email protected]> wrote:
              >How does request.setAttribute() works?
              >Does it append data to http header and forward? Does it set to a memory
              >space like session.setAttribute() does?
              >
              >Thank you
              >Jim
              >
              >
              

  • How can i write request.setAttribute/

    Hi,
    I have some data in a text box in jsp.
    I want to create session scope for that text box data
    like request.setAttribute
    How can I write request.setAttribute for a field in a jsp on that jsp itself.
    I using struts & JSTL.
    Advanced Thanks,
    Mahendra

    Use the javascript and create a session from JSP itself.

  • How to use request.setAttribute in javascript function.

    Here is my scenario.
    I am storing all open windows handles in the array( javascript ).
    I want to send this array to the servlet for which I need to do
    request.setAttribute("jsArray", windowArray);
    I am getting all kinds of errors while writing above statement in the javascript function.
    Here is my code:
    function save_javascript_array(){
    <% request.setAttribute("jsArray", %> + winArray + <% ); %>
    document.forms[0].action="/NASApp/inv/AuditServlet";
    document.forms[0].submit();
    Thanks in advance.

    try something like this:
    crate a JavaScript function that set a form parameter with the values contained in the array, for example make a string containing all elements of the array separated by colon (,):
    function save_javascript_array()
    var data;
    data = '';
    for( var i=0;i<winArray.length;i++ )
    data = data + winArray[i] + ',';
    document.forms[0].handles.value = data;
    document.forms[0].action="/NASApp/inv/AuditServlet";
    document.forms[0].submit();
    you must have a form like this:
    <form ...>
    <input type="hidden" name="handles">
    </form>
    Now in the servlet you must retirve the parameter called "handles" and iterate over the string:
    StringTokenizer tok = new StringTokenizer( request.getParameter("handles"),"," );
    while( tok.hasMoreElements() )
    String handle = tok.nextToken( );
    // do something with this handle...
    The code above may be different (I don't remember the names of the methods os StringTokenizer) and I don't known much about JavaScript (e.i. how to iterate over a array and how to concatenate each element in the array)
    JaimeS

  • Struts tag for request.setAttribute() in a jsp page

    Hello All
    Does anyone know struts tag that can be used for request.setAttribute() in a jsp page?
    Thanx in advance.

    Well
    <bean:define id="foo" name="newsIdAttribute" scope="request" type="java.lang.String" />is equivalent to
    <%
    (java.lang.String) foo = (java.lang.String)request.getAttribute("newsIdAttribute");
    %for setting bean properties you might have to use
    <jsp:setProperty/> or <c:set/> tags in JSP / JSTL respectively.
    Hope this might answer your question :)
    REGARDS,
    RaHuL

  • Request.setAttribute with strings that hold URLs

    Hi,
    In my servlet I do request.setAttribute(stringKey,stringValue) and then dispatch to a jsp. In that jsp I use the attributes with value = request.getparameter(stringKey) and value I use in <c:import> tags.
    This value is a string that holds complete URLs!
    Now upon doing this I get ArrayIndexOutOfBoundsException. How to solve this?
    Hope someone can explain me what's wrong and what possible solution there is.
    Greets,
    Timv
    java.lang.ArrayIndexOutOfBoundsException: 20
         at org.apache.catalina.util.RequestUtil.URLDecode(RequestUtil.java:465)
         at org.apache.catalina.util.RequestUtil.URLDecode(RequestUtil.java:427)
         at org.apache.catalina.util.RequestUtil.URLDecode(RequestUtil.java:391)
         at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:615)
         at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:174)
         at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:302)
         at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:179)
         at jasper.disclaimer_jsp._jspService(_disclaimer_jsp.java:131)
         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at com.iplanet.ias.web.jsp.JspServlet$JspServletWrapper.service(JspServlet.java:552)
         at com.iplanet.ias.web.jsp.JspServlet.serviceJspFile(JspServlet.java:368)
         at com.iplanet.ias.web.jsp.JspServlet.service(JspServlet.java:287)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.StandardWrapperValve.invokeServletService(StandardWrapperValve.java:720)
         at org.apache.catalina.core.StandardWrapperValve.access$000(StandardWrapperValve.java:118)
         at org.apache.catalina.core.StandardWrapperValve$1.run(StandardWrapperValve.java:278)
         at java.security.AccessController.doPrivileged(Native Method)
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:274)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:505)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:212)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:505)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:203)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:505)
         at com.iplanet.ias.web.connector.nsapi.NSAPIProcessor.process(NSAPIProcessor.java:158)
         at com.iplanet.ias.web.WebContainer.service(WebContainer.java:598)

    Ok, sorry, I was too fast..
    I was using rtexpr (<%=parameter%>) instead of ${parameter}",
    greets

  • Is there any limitation about request.setAttribute() ?

    I need to set a CLOB data to request.setAttribute() , just wonder if there any size limitation of putting data to setAttribute() and dispatch to other jsp to pick it up?

    Only the maximum amount of memory in the Servlet Container itself. The RequestDispatcher will always dispatch to the same JVM in a distributed environment, so the data doesn't need to be Serializable.
    Brian

  • Need Clarification - Request Bean vs Session Bean?

    Could someone clearify to me the difference between a Request bean and Session bean and when to use which one.
    In a netbeans Visual Web Project I notice that the backing beans or code behind files that the web pages use are request beans but there is also a Applications Bean, Session Bean, and Another Request Bean.
    What type of logic should be going where in terms of the jsp's request bean, the single session bean, and single request bean?
    A few years ago when I was in school we created regular Web projects that used session beans as the backing beans which handled all the logic and passed data to and from a EJB layer which did all the Database connectivity.
    And finally where do EJB fit into this style of Visual Web Projects?
    Thanks,

    So in terms of where certain types of logic goes I would so something like this login example:
    User clicks a Login button, this is bound to the RequestBean, the request beans checks the session bean if user is logged in, if no then use a EJB to validate against DB, return true/false. Set user login state in the session bean? That a proper flow and useage of the different types of beans?
    When all is said and done the request bean would be used to forward off actions fromt he jsp to wherever they need to go?
    Edited by: avalanche333 on Jun 6, 2008 11:37 AM

  • Request.setAttribute

    when the user click on the button, then go to servlet side to check the validation,if there is problem then will return msg by this method :
    RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("back.jsp");
              httpRequest.setAttribute("result", result);
              dispatcher.forward(httpRequest, httpResponse);
    and then forward to the back.jsp
    in the back.jsp
    using ${result} to display the message
    now, i would like to ask how and when to clear the result value when the user click on other button,
    because the message still here when user click another button.

    How about this: When the user clicks the other button, redo the validation check and return an empty string if it passes. Then have the JSP page not show any error message if the value is an empty string.

  • How to get a parameter from each request in a session scope BackingBean

    While calling my JSF page, I pass an id as parameter in the URL, and in the Backing bean I retrieve it from the request.
    On each request I need to get the id and populate the page accordingly.
    But I am forced to create my Backing Bean in session scope b'cos otherwise ValueChangeListener method does not work properly.
    So where and how do I get the id on each request?
    Pls. help

    What you can do is create it in the request scope like this:
    <managed-bean>
          <managed-bean-name>personBean</managed-bean-name>
          <managed-bean-class>
            com.PersonBean
          </managed-bean-class>
          <managed-bean-scope>request</managed-bean-scope>
           <managed-property>
                 <property-name>id</property-name>
                 <property-class>java.lang.Long</property-class>
              <value>#{param.id}</value>
          </managed-property>
    </managed-bean>And then in the page use a hidden field to set the id in case of a postback (validation error):
    <h:inputHidden id="id" value="#{personBean.id}"/>Does that help you?
    Thomas

  • How to submit a child concurrent request in same session as its parent?

    Hi All,
    I need one help. In one of my program, I am using something like this
    fnd_request.submit_request ()—prog A
    fnd_request.submit_request ()—prog B
    fnd_request.submit_request ()—prog c
    now I want prog A,B and C to use same session. the moment I use fnd_request.submit_request, it opens a separate session, I want to use same session. I even tried with request set but didn’t work.
    Kindly help on this.
    Kind regards
    Sandy

    Hi,
    now I want prog A,B and C to use same session. the moment I use fnd_request.submit_request, it opens a separate session, I want to use same session. I even tried with request set but didn’t workThis is an expected behavior, and I believe there is no way to have all requests running under one session.
    Regards,
    Hussein

  • How to work with request, response and sessions in JSC

    Hi
    I have two Servlets ServletA and ServletB. ServletA creates a session and add some attributes to this session. Then it forwards the request to ServletB where the attributes of this session are retrieved and used in response object.
    Here is what I am doing in ServletB
    - Get the session attrribute. This attribute is of type byte[]
    byte[] mydata = (byte[]) request.getSession.getAttribute(data);
    response.setContentLength(mydata.length);
    OutputStream stream = response.getOutputStream();
    stream.write(mydata,0,mydata.length);
    stream.close();
    My question is ...
    How do I achieve this in Creator. ?
    Thanks in advance
    Srinivas

    Hi
    I have two Servlets ServletA and ServletB. ServletA
    creates a session and add some attributes to this
    s session. Then it forwards the request to ServletB
    where the attributes of this session are retrieved
    and used in response object.
    Here is what I am doing in ServletB
    - Get the session attrribute. This attribute is of
    type byte[]
    byte[] mydata = (byte[])
    request.getSession.getAttribute(data);
    response.setContentLength(mydata.length);
    OutputStream stream = response.getOutputStream();
    stream.write(mydata,0,mydata.length);
    stream.close();
    My question is ...
    How do I achieve this in Creator. ?
    Thanks in advance
    SrinivasBy "this" do you mean "how do I access a session attribute that has been placed there by another servlet?"
    The base class for all page beans has a bunch of convenience methods available. The simplest one to use for this purpose is getBean(), which searches through request scope, session scope, and application scope for a bean with the name you specify -- and, if its a managed bean, will create it for you as well. So, your code would look like this:
      String data = ...; // Name of the attribute you want
      byte[] mydata = (byte[]) getBean(data);There is an additional quirk related to what you're actually trying to do, though ... it looks like you are trying to write out binary content (perhaps an image loaded from the database or something). It is not possible to mix binary and text output in a single response.
    Your best bet for this particular purpose, then, is to continue using ServletA and ServletB just as you have been. The applications created by Creator are standard servlet-based web applications, so you can use additional servlets and all the other goodies (although you'll have to declare them yourself in the web.xml file, as usual).
    Craig

  • Adoption of CFID/CFTOKEN from request for new session

    Hi
    We've noticed that ColdFusion automatically uses CFID/CFTOKEN
    from the request, if there's no session available for this token
    combination and ColdFusion creates a new session. Is there a way to
    tell ColdFusion that it shouldn't use the values from the request
    but generate a pair of its own?
    Regards
    Sargon

    10.12 is the latest and greatest SW version for the device (from mid-2010). 
    I'm sad to say, but this device model has been out of production already for a long while and no new SW releases can be expected. Also some of the things you listed are such, which would not even be fixable by new SW version, but would require other changes.
    you could probably try updating e.g. the browser app to a newer version via http://download.browser.ovi.com.
    http://www.microsoft.com/en/mobile/nokia-x-updates/
    http://www.microsoft.com/en/mobile/nokia-x2-update/
    http://www.microsoft.com/en/mobile/asha-software-update/
    http://www.microsoft.com/en/mobile/support/software-update/wp8-software-update/
    http://www.developer.nokia.com/Community/Wiki/Nokia_firmware_change_logs
    https://twitter.com/LumiaSWUpdates

Maybe you are looking for

  • Poor battery life and WiFi connectivity on iPhone 5s

    I have bought a new iPhone 5s 16GB running iOS 8.1.2 3 days ago. The device that I have got has a poor battery life and WiFi connectivity. I have tried lots of methods given on the internet like turned off the auto-brightness, reset the phone to defa

  • Memory Slots on 15" Powerbook?

    I have a 15" Powerbook (1.25 mhz). According to the System Profiler there are 2 256 of ram sticks for a total of 512 mb of ram. Are there two available slots on this machine so if I want to upgrade the memory I'd have to take out one of the ram stick

  • How do i use itunes on two PC's

    I have just purchased a new PC for home use, but my original itunes is on my work PC now every time i try to use my ipod with my home PC it asks me if i want to clear all songs on my ipod and relist new songs from this PC. How can i keep the tunes on

  • Not able to print on Purchase order whatever we fill in item text

    not able to print on Purchase order whatever we fill in item text, from where can we link to get printed item text on purchase order

  • Text Menus in DVDSP4 or Photoshop?

    I'm creating a menu in DVDSP4 that goes through the history of the project.. it is just text with a "NEXT" arrow to navigate through the information.. what is the best way to make the background and text so that it looks crisp and professional as pos