1

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?

geekosaur
  • 59,309
  • 11
  • 123
  • 114
Ashwin
  • 12,691
  • 31
  • 118
  • 190

4 Answers4

3

Request parameters can be passed by using <jsp: param>

This tag contains two attributes:

  1. name
  2. value.

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>
andih
  • 5,570
  • 3
  • 26
  • 36
Nishant
  • 32,082
  • 5
  • 39
  • 53
  • @user1310825 : Will this add new parameters? – Ashwin Apr 13 '12 at 14:14
  • @Ashwin Yes it will add new parameters which are visible within the forwarded or included page. On the target page you would call something like `request.getParameter("myParam")` to get the additional parameter. – andih Apr 14 '12 at 13:28
3

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?

Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
2

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

andih
  • 5,570
  • 3
  • 26
  • 36
0

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

  • You can use a `HttpServletRequestWrapper` to add or remove request parameters. If the target is within the same WebContext you can allways use `request.setAttribute(String, Object)`to add additiona "parameters" for the target. If you use an Http Redirect (HTTP 302) you have to copy the form parameters from the original request. Also you'll have to keep in mind that the InternetExplorer has per default a URL length restriction of about 2058 characters. – andih Apr 14 '12 at 13:11