1

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); 
2
  • 1
    put the value in session scope and get that value in any jsp page Commented Jun 30, 2014 at 17:40
  • 2
    u just did it. set attribute to request before disp.forward(req,res). Access it from request(within the JSP you are forwarding to) using req.getAttribute(..); FYI request dispatcher doesn't sends response to web browser(watch your url when you do dispatch, it dosn't changes); hence no redirect .. CHEERS ! Commented Jun 30, 2014 at 17:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth mentioning you would want to url encode the parameters if you have any special characters :)
1

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"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.