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?
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?
Request parameters can be passed by using <jsp: param>
This tag contains two attributes:
name
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>
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
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))
Related
I have a portlet developed in Liferay, in which I want to get query parameter value from URL.
I tried this way but get "null" value from Query parameter:
HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
HttpServletRequest httpOrigReq = PortalUtil.getOriginalServletRequest(httpReq);
String myValue = httpOrigReq.getParameter("idProcessOrigin");
Any advice would be greatly appreciated!
The code you mention in your question should work, however, it's ignoring the peculiarities of a portlet environment. Typically, in a portlet, you'd rather "decorate" the names of parameters with <portlet:namespace/> (or whatever the equivalent in your UI library of choice is to this JSP tag). Instead of submitting a parameter "idProcessOrigin", you'd submit "<portlet:namespace/>idProcessOrigin" (of course, with properly replaced namespace, e.g. rather SOME_RANDOM_STUFF_idProcessOrigin)
If you don't want this, you can also declare the property com.liferay.portlet.requires-namespaced-parameters=<boolean> in your portlet-#Component's property list (as carried over from liferay-portlet.xml)
For the standard way of obtaining the parameters from a portlet request, you don't need to go through the HttpServletRequest at all - just use the PortletRequest's getParameter method. The result of this method depends, however, on properly decorated parameter names (or the deactivated option mentioned above). Note: When you call request.getParameter("idProcessOrigin"), you don't need the decoration any more, provided that request is a PortletRequest, not an HttpServletRequest.
I have a JSP that's accessed from an url like
http://localhost/products.jsp
(thus without a query string), while that page includes other JSP's with:
<jsp:include page="product.jsp">
<jsp:param value="1" name="recordNumber"/>
</jsp:include>
Inside product.jsp there's a call to a Java method that receives the request object:
NavigationUtils.getProductUrl(request)
That method logic is driven by the request parameters.
What I get is that :
request.getQueryString() returns null
request.getParameterMap() has an entry for "recordNumber"
Is this standard behaviour or am I doing something wrong?
I've looked up the docs about HttpServletRequest.getQueryString() and ServletRequest.getParameterMap(), but I can't find that behaviour described nor any reference to a container-dependent handling that may yield different results.
The main issue is that I may break existing code using getParameterMap() instead of getQueryString(), so any advice on that would help.
The query string is nothing more than a mechanism to encode parameters in a request, but it's not the only one. Typically, that's used when a browser sends a GET request to the server. Another mechanism would be in the body of a form-encoded POST request.
In your case, the JSP wants to include the results of another JSP, which all happens server-side. The servlet container can pass parameters from one JSP to the other without having to encode the parameter on the query string (which would be unnecessarily inefficient).
Using getParameter or getParameterMap is the more general solution. Using getQueryString only really makes sense in specific circumstances when that's eexplicitly what you need to look art.
So I'm writing a Spring 3 webapp with JSP views and JSTL tags. They normally work great, but there's this one controller call that doesn't grab the tags properly.
ModelAndView mav = new ModelAndView(
new RedirectView(RequestUtil.getWebAppRoot(request) + clientShortName, false)
);
mav.addObject("status","Session for interface successfully removed");
return mav;
So when I go to reference it in my view, I'll have a line that looks like:
<p>status="${status}"</p>
Which just displays as:
status=""
Now I would normally just dismiss this as something causing my view to render improperly, but I actually found this sitting appended to my URL:
?status=Session+for+interface+zFXDEV3+successfully+removed
So this leaves me with two questions:
Why can't I reference the object from a JSTL tag?
If I can't get it as a part of the tag context, what is it doing in the URL?
and for anyone wondering, the class types are:
org.springframework.web.servlet.view.RedirectView.RedirectView
org.springframework.web.servlet.ModelAndView.ModelAndView(View view)
This is not JSTL but Expression Language (commonly known as EL). The problem is that EL ${status} will look the variable in the request attributes, but when you redirect to your JSP you have status as request parameter but not as request attribute (note that this is normal behavior when you redirect to a page).
For a better example (taken from StackOverflow Expression Language code), this is what is executed:
<%
String status = (String) pageContext.findAttribute("status");
if (status != null) {
out.print(status);
}
%>
You have two possible options here:
As stated by #SotiriosDelimanolis, your #Controller class for this URL should take the request parameters and add them as request attributes. Lot of work if you could add more request parameters in the future.
Use the ${param} object from EL that gives you access to the request parameters. Using this, you should change ${status} to ${param.status}. End of story.
Because it is a RedirectView. The javadoc says:
By default all primitive model attributes (or collections thereof) are
exposed as HTTP query parameters (assuming they've not been used as
URI template variables), but this behavior can be changed by
overriding the isEligibleProperty(String, Object) method.
So your String objects are added as query parameters in the new, redirected, request. They are no longer available as model/request attributes to the new request.
The #Controller that handles the redirected URL should re-add the attribute to the model.
Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.
It is posible for a portlet to read a request parameter of its surrounding page?
E.g. the URL of the page the portlet resides in is http://example.com/mypage?foo=bar
Is it possible to read the "foo" parameter from a portlet that is on that page?
Portlet Container is Liferay 5.2.5.
Yes this can be achieved with something like this -
HttpServletRequest convertReq = PortalUtil.getHttpServletRequest(request);
HttpServletRequest originalReq = PortalUtil.getOriginalServletRequest(convertReq);
String productId = originalReq.getParameter("foo");
Where request is RenderRequest.
PortletRequest class has method getAttribute()
You can treat it like HttpServletRequest.
I haven't yet found a way besides using platform specific class com.liferay.portal.util.PortalUtil.