Reputation: 13537
Is there any way to add parameters in a servlet or jsp? Because as far as I have searched, parameters can only be set in forms. Why is there no command in java to add parameters manually? Another doubt I have is that, when forwarding from one jsp page to other, is the input request(with the parameters, attributes etc) forwarded to the next jsp page? What if I don't want certain parameters or attributes to be forwarded?
Upvotes: 1
Views: 9267
Reputation: 32222
Request parameters can be passed by using <jsp: param>
This tag contains two attributes:
eg:
<jsp: param name="myParam" value="Amar Patel"/>
This tag is used as a nested tag within <jsp:forward>
or <jsp:include>
blocks.
For example:
<jsp: forward page="ssParameters.jsp">
<jsp: param name="myParam" value="Amar Patel"/>
<jsp: param name="Age" value="15"/>
</jsp: forward>
Upvotes: 3
Reputation: 5603
Is there any way to add parameters in a servlet or jsp?
Yes. If it's possible to use the RequestDispatcher.forward(ServletRequest, ServletResponse)
or RequestDispatcher.include(ServletRequest, ServletResponse)
you can use the the HttpServletRequestWrapper
to add or filter Request Parameters
// assuming this code is part of a Servlet or JSP
HttpServletRequest request = ...;
final Map<String,String> additionalParameters = ...;
RequestDispatcher dispatcher = this.getServletConfig().getServletContext().getRequestDispatcher("/");
dispatcher.forward(new HttpServletRequestWrapper(request){
public String getRequestParameter(String parameterName) {
if (additionalParameters.contains(parameterName)) {
return additionalParameters.get(parameterName);
} else {
if (!"filteredParameter".equals(parameterName)) {
return super.getParameterMap().get(parameterName());
} else {
return null;
}
}
}
}
, response);
If you only want to pass additional "parameters" to the forwarded/included page / servlet
it's recomended to use the ServletRequest.setAttribute(String, Object)
. ServletRequest Attributes may be added / removed during processing the request and allow to add complete java Objects rather than Strings when using Request Parameters.
Why is there no command in java to add parameters manually?
The ServletRequest Request Parameters should usually be treated as unmodifiable as it is Warpper of the Request sent from the Client to server. If you want to add Parameters use the Request Attributes.
Another doubt I have is that, when forwarding from one jsp page to other, is the input request(with the parameters, attributes etc) forwarded along to the next jsp page?
Most of the time the original HttpServletRequest
is passed to the forwarded page / servlet. But as shown in my snipplet it's also possible to pass a different ServletRequest to the forwarded / included servlet / jsp.
What if I don't want certain parameters or attributes to be forwarded?
See code snippet above. You can filter the forwarded parameters or attributes using your own HttpServletRequestWrapper
Upvotes: 2
Reputation: 91
You can pass your parameters by using it in your url. Regarding your questions about request. If you do forward to another jsp/servlet, all your parameters and attributes will be in the request. If you don't want to see it there, then you should use redirect instead (response.sendRedirect(url))
Upvotes: 0
Reputation: 8101
Is there any way to add parameters in a servlet or jsp?
-- Append it to the URL. If you are thinking of some hypothetical function such asrequest.setParameter
, then there is no such method provided. Don't you think it would be a security breach then?
Upvotes: 3