Reputation: 11
I am trying to forward a request using "RequestDispatcher". I have JSP pageContext object. I tried to forward my request to my servlet "MyServlet" using the following code. I set some attributes in my request and forwarded it. But i am not able to access those variables in my servlet class.
My code :
pageContext.getRequest().setAttribute("AValue","A");
pageContext.getRequest().setAttribute("BValue", "B");
ServletContext context= pageContext.getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/MyServlet");
rd.forward(pageContext.getRequest(),pageContext.getResponse());
Help me !! Thanks in advance.
Upvotes: 0
Views: 1196
Reputation: 12983
When dynamically including or forwarding to a servlet from a JSP page, you can use a jsp:param
tag to pass data to the servlet.
A jsp:param
tag is used within a jsp:include
or jsp:forward
tag.
<jsp:include page="/servlet/MyServlet" flush="true" >
<jsp:param name="AValue" value="A" />
<jsp:param name="BValue" value="B" />
</jsp:include>
The use of scriptlets <% %>
in JSP is indeed highly discouraged since decade.
See How to avoid Java Code in JSP-Files?
Upvotes: 0