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.

Similar Messages

  • 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

  • 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
              >
              >
              

  • 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

  • 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.

  • 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.

  • GetParameter, setAttribute and session values

    I am having a problem forwarding values to a jsp from my servlet.
    This problem only occurs when multiple users are accessing the same pages to make a payment.
    The value is passed from the page to the servlet.
    The servlet gets the value using a getParameter.
    //Get the value from the previous page
    String DocTotal = request.getParameter("DocTotal");
    logger.debug("DocTotal: " + DocTotal);
    //set the value to pass to next page
    request.setAttribute("payAmount", DocTotal);
    displayTemplate(CREDITCARDPAYMENT);
    displayTemplate code follows.
    RequestDispatcher rd = getServletContext().getRequestDispatcher(template);
    rd.forward(req, res);
    I then set the value in the request object to pass to the next page.
    This all seems to work fine for a single user but when I I have more than one user, one user will get a null for value being passed on the next page and the other user will not get the page displayed.
    It is like my session values are getting stomped on by the other user.

    The changes that you have suggested seem to work, but I still have a problem.
    One user seems to be able to get the data passed successfully to the next jsp, but the other just gets a blank screen. If the user with the blank screen refreshes they get the screen with the data. It is as if the servlet hangs up when calling the page. I am not sure.
    Here is the code that is being accessed with formatting.
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    req = request;
    res = response;
    HttpSession session = req.getSession(true);
    logger.debug("Made it to the doPost method of DownloadUtilServlet!");
    logger.debug("session: " + session);
    String cmd = req.getParameter("cmd");
    logger.debug("cmd equals: " + cmd);
    if ((cmd != null) && cmd.equalsIgnoreCase("forwardPaymentInfo")){
    XCBillerCenterItem bci = null;
    try{
    logger.debug("XCConfig.CCUSER: " + XCConfig.CCUSER);
    bci = new XCBillerCenterItem(XCConfig.CCUSER);
    catch(Exception e) {
    logger.debug("XCBillerCenterItem Create: " + e);
    String ccList = bci.getCCTypes();
    req.setAttribute("ccList", ccList);
    logger.debug("cmd equals: " + cmd);
    String DocTotal = req.getParameter("DocTotal");
    logger.debug("DocTotal: " + DocTotal);
    req.setAttribute("payAmount", DocTotal);
    displayTemplate(CREDITCARDPAYMENT);
    }//User has paid, proceed to download
    else if ((cmd != null) && cmd.equalsIgnoreCase("downloadreportfilter")){
    displayTemplate(DOWNLOADREPORTFILTER);
    else{       
    GetParameters(req, res, session);
    public void displayTemplate(String template)
    try
    RequestDispatcher rd = getServletContext().getRequestDispatcher(template);
    rd.forward(req, res);
    catch(Exception e)
    e.printStackTrace();

  • Remove parameter from Request

    Hi,
    my Servlet computes a request (doGet()-method) with some parameters (e.g. http://myApp/test?param=Hello).
    Then it forwards to another servlet/jsp:
    request.getRequestDispatcher("/mySite").forward(request, response);
    But before forwarding, I want to remove the parameters! How can I do this?
    I cannot find a method like "request.removeParamter(param)" in the API!
    Thanks
    Daniel :-)

    Hi everyone.
    I had a similar problem and I've resolved it with a workaround: I needed to forward a Servlet to the Servlet itself, for multiple data posting, so I used a request attribute to determine if I was processing the first call or the next ones.
    I think this workaround will work also when you must forward Servlet1 to Servlet2.
    Here's the code:
    In the first time call (or in Servlet1):
    if (...some conditions...) {
    // Instead of removing parameter...
    request.setAttribute("switch","Y");
    In the next calls (or in Servlet2):
    String par = null;
    String switch = (String)request.getAttribute("switch");
    if (switch == null) {
    par = (String)request.getParameter("par");
    I know that in this way the "par" parameter is not really removed, however, if you managed the attribute "switch" correctly, it would be null.
    Don't forget to remove the attribute from the request when you no longer need it. Let's clean our junks... ;-)
    Hope this helps.
    Carlo

  • Get a parameter from a Request

    Hello everybody,
    I noticed that there is not a method like request.setParameter(...,...), so i was wondering if i can use the method request.setAttribute(...,...) to put an object in the request and then get it with the method request.getParameter().....;anyway what is the difference between a Parameter and an attribute in the request object?
    Cheers.
    Stefano

    Hello,
    Basically the difference I see is that a request's parameter is information that is already part of the request. An attribute is something that you add into a request and is not essential to the object. As for putting an object in there it can be done with the setAttribute just as you mention.
    Hope this helps

  • Unable to retrieve values from request.getAttribute()

    I had a JSP file called targetJspPage.jsp that contains the following statement within a set of <form> tags:
    <% request.setAttribute "url","/myProj/targetJspPage.jsp");%>This information is then submitted to a servlet using POST method which will use a RequestDispatcher to retrieve the the url from the request attribute that I had set just now. The statement is as follows:
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher((String)request.getAttribute("url"));
              if (dispatcher != null)
                   dispatcher.forward(request, response);The purpose of this procedure is to let the servlet know which JSP file it is suppose to forward the processed results. The url is always the url of the page that is calling the servlet. Eg. In this case, my JSP file is targetJspPage.jsp which resides in myProj folder. So, in this JSP file, the url set for the servlet will be /myProj/targetJspPage.jsp. But my problem now is that the (String)request.getAttribute("url") statement in the servlet always returns null. Why is that so?
    This type of question was asked numerous times in the forums but the solutions aren't working. I do not want to use a session to store the value. However, I also tried using session before and the result is the same.

    Hi Sad,
    1.
    Is it possible that you post the codes that we can see clearly what is going on? (If possible only).
    (Unless I'm wrong, it may be that the request.setAttribute is used to pass object on the server side only, ie you can't use the request.setAttribute to keep data like the session object during the client-server trips of request/response.
    The fact that you get null for (String)request.getAttribute("url") is because the Browser send a new request object which is different from the old one (request.setAttribute which is gone once we down the client)
    (2. I want to learn too, and hope some one will find a solution for you)
    -- Paul.

  • Request.getParameter() always returns null

    I have a html file and am trying to retrieve the values from a formin my servlet.
    here is the html code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <h1><b>Add DVD</b></h1>
    </head>
    <body>
    <form action="add_dvd.do" method="POST">
    Title:<input type="text" name="title" />
    Year:<input type="text" name="year" />
    Genre: <select name='gselected'>
    <option value='Sci-Fi'>Sci-Fi</option>
    </select>
    or enter new genre:<input type="text" name='gentered' value="" />
    <input type="submit" value="Add DVD" />
    </form>
    </body>
    </html>
    and here is the servlet code:
    public class AddDVDServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    // System.out.println("in AddDVDServlet post method");
    List errorMsgs = new LinkedList();
    //retrieve form parameters
    try{
    String title = request.getParameter("title").trim();
    String year = request.getParameter("year").trim();
    *String gentered = request.getParameter("gentered");
    String gselected = request.getParameter("gselected");
    String genre="";
    if("".equals(gentered))
    genre = gselected;
    else
    genre = gentered;
    // System.out.println("parameter retrieved");
    if(!year.matches("\\d\\d\\d\\d"))
    // System.out.println("year not 4 digit long");
    errorMsgs.add("Year must be four digit long");
    if("".equals(title))
    // System.out.println("title not entered");
    errorMsgs.add("Please enter the title of the dvd");
    if("".equals(genre))
    // System.out.println("genre not valid");zdf
    errorMsgs.add("Enter genre.");
    if(! errorMsgs.isEmpty())
    //System.out.println("errors in entry ");
    request.setAttribute("errors",errorMsgs);
    // System.out.println("error attribute set in request");
    RequestDispatcher rd = request.getRequestDispatcher("error.view");
    rd.forward(request, response);
    return;
    //create DVDItem instance
    DVDItem dvd = new DVDItem(title,year,genre);
    request.setAttribute("dvdItem",dvd);
    RequestDispatcher rd = request.getRequestDispatcher("success.view");
    rd.forward(request, response);
    catch(Exception e){
    errorMsgs.add(e.getMessage());
    request.setAttribute("errors",errorMsgs);
    RequestDispatcher rd = request.getRequestDispatcher("error.view");
    rd.forward(request, response);
    e.printStackTrace();
    System.out.println("exception:"+e);
    why does getParameter always return null??? whats wrong?

    I don't know. However, I suspect that because you have a tag with the same name as 'title', its causing a name conflict. Chnage the name to something else. If it works, then that's the likely explaination.

Maybe you are looking for