The servlet redirects from page1.jsp to page2.jsp like this:
String url = getServletContext().getContextPath() + "page2.jsp?command=reload";
response.sendRedirect(url);
Than the filter which try to get "command" parameter can get it. But servlet does not see the parameter.
I also tried to do this without any filters. The result is the same.
What is the reason?
What's your servlet here? The one that you get by compiling page2.jsp as a servlet? If that's the case, make sure you are retrieving the parameter correctly by using:
request["command"]
If your servlet is a real servlet (no JSP page), then you need to map a URL to that servlet and invoke the servlet with that URL mapping.
Make sure you post more info so you will get better help.
Related
I have been trying this for a while with no luck.
I want to know if there is a way to include response of a server request (i.e. a Spring controller) inside a JSP.
I am trying to build an independent module which will rely on some specific objects and will print the HTML based on these objects.
So I want to create a Controller method which will take care of dependencies and return this JSP as response.
I can then include a call in the parent JSP so that it hits the controller method and injects the response returned by this method in the parent JSP.
I read somewhere that jsp:include can be used for this purpose as follows:
<jsp:include page="/test-url" flush="true"/>
Where /test-url will map to a Spring controller method.
But when I run this, i get the following exception:
Servlet.service() for servlet dispatcher threw exception: java.lang.IllegalStateException: Cannot forward after response has been committed
Please provide your valuable inputs if you have some idea about this?
use this way
String pPath = yoururl
<jsp:include page="<%=pPath%>" flush="true">
it work fine for me
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.
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.
I'was debugging a client-side AJAX problem that is making request to servlet. But the bug turned out to be at server side. You can refer to my original question here. From the discussion with more experienced people, I found out that servlet is using request.getAttribute() method to retrieve parameters from the request instead of getParameter(). So I thought to open a new question to clear my doubt.
Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?
You should always use getParameter, when attribute come from GET or POST method. And use getAttribute when request is forwarded from another servlet/jsp. Such as :
ServletA:
request.setAttribute("test","value")
request.getRequestDispatcher("/ServletB").forward(request, response)
ServletB:
request.getAttribute("test") <-- you can get test attribute by using getAttribute
Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?
Complete nonsense. You're apparently working on an existing project which has a lot of other existing servlets and filters. My cents that there's another filter in the request-response chain which maps request parameters to request attribtues for some unobvious reason.
Please create a blank playground project and create a playground servlet to familarize yourself better with servlets without all that noise in existing projects.
See also:
Our Servlets wiki page
How do i specify a redirection to another servlet, in the doPost() method of a servlet.
at the moment I'm using
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
which is wrong since, my parameters in the doGet() method of products are not being called and initialized.
So I'm left with an empty products page after logging in :/
You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of /products:
response.sendRedirect("/products");
This way the webbrowser will be instructed to fire a new HTTP GET request on the given URL and thus the doGet() method of the servlet instance will be called where you can in turn load the products and forward to a JSP which displays them the usual way.
In your doPost you can always call:
return doGet(request, response);