简体   繁体   中英

Forward parameters to jsp from servlet

Is there a way to send parameters from servlet to jsp without redirecting the browser to that page?

RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp");
disp.forward(request, response);

There can be one way as below:

RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp"+"?myParam=myValue");
    disp.forward(request, response);    

If you are fine with "GET" method then you can solve this problem with appended parameters.

Well you can either set the attributes(used in case of internal communication with servlets or servlet to jsp or vice-versa) to the response object and forward the request you can achieve this as :

      request.setAttribute("someKey","someValue");

You can also use the session scope to share the attributes between servlet and jsp like this:

     Http session = request.getSession();
     session.setAttribute("someKey","someValue");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM