How to redirect in Asynchronous Servlet? - java

According to this document, I understand a Request could be dispatch forward to another servlet in one Asynchronous Servlet which also makes container to call complete() and trigger another Servlet or JSP automatically.
But, what if I want to redirect the page under async Servlet? Should I call complete() and response.redirect("") both? do we have any example of redirection?
Thanks.

What you can do in this situation is that give specific json response to client (browser) using response.getWriter.write(jsonObject) and in JavaScript you write code for changing the location i.e location.assign("/newPage").

Related

Why write a servlet filter for a DispatcherType other than REQUEST?

I've seen plenty of servlet filters in my time for doing all kinds of things but always on REQUEST never anything else. Does anyone have any use cases or examples of why it might be useful to write a servlet filter for another dispatcherType, other than REQUEST?
The DispatcherTypes as in javax.servlet package.
public enum DispatcherType {
FORWARD, INCLUDE, REQUEST, ASYNC, ERROR
}
Well the names are rather self-explaining. The REQUEST is indeed the most often used one and it's applied on the incoming request chain. Here is when you may need to use the others (because the REQUEST only dispatched filters will not be applied):
FORWARD - if you want to filter request/response when one servlet forwards the request to another servlet. Often this is used when a servlet forwards to a JSP page.
INCLUDE - if you want to filter request/response when one servlet calls another servlet in order to include it's response in the own response. Often this is used when a JSP page includes JSP page.
ASYNC - This is something I personally never used but AFAIK it's needed to be able to filter the asynchronous requests introduced in Servlet 3 spec
ERROR - if you want to filter request/response when the servlet call results in error.
See https://sling.apache.org/documentation/the-sling-engine/filters.html#filter-chains for some more details.

How to redirect a HTTPRequest from one controller class to other controller class?

I have two controller. One is main controller and other is intermediate controller. In intermediate controller I should add header to HTTPRequest. After Adding I should redirect it to main controller where I should check the header I added. How can I do this? Can anyone help me for this?pls........
Seems like a good place to use Servlet Filter . If you want to pre-process an incoming request you could do this with servlet filters and then simply chain it to the appropriate servlet .
Refer BalusC Answer for details .
If this is not the case your can merely forward the request like :
request.getRequestDispatcher("/yourServlet").forward(request, response); // forward to the main servlet
Forward Dispatching
getServletContext().getRequestDispatcher().forward("second page");
The request forwarding is done internally by the JSF Controller Servlet to another resource. The browser is unaware of what has happened in the server side at the web container. So it still thinks it is tending to the original request and displays the original URL in its address bar. However, the page content displayed is from the second page.
Redirect Dispatching
response.sendRedirect("second page");
In this case, the JSF Controller Servlet instructs the client browser (via HTTP response header) to fetch another URL. So the browser fetches entirely a new URL and displays the second URL in its address bar. This could cause slight performance delay
from here
I think you need to forward the request rather than redirecting.
RequestDispatcher dispatcher= request.getRequestDispatcher("servlet-mapped-url");
dispatcher.forward(request, response);

How servlet handle multiple request from same jsp page

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.

Sending redirect to another servlet/JSP without loosing the request parameters.

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

How to get a Struts form bean in an Action class using AJAX

I'm currently developing with Struts 1 a web application. I've recently started to use AJAX technology and I'm stuck with something.
I call an action method (via AJAX) to validate my form but no values are changed in the form bean when it gets to the action method. I suppose this is because calling the action via AJAX doesn't submit the form to the action method. Am I right? I've tried to send form values as a JSON object, but I can't parse it in the action class because; as far as I know, I need an external library to do so and, unfortunately, company policies doesn't allow me to use external libraries. Is there any other way to send the form?
Thanks in advance,
Carlos
Yes, you are correct. All that's happening with AJAX is a request is being sent to a particular URL, not a form submission.
The easiest way to deal with this is to add the fields you want validated as parameters in the URL then simply pull them off the HTTP request in the server (can't remember how easy that is with Struts).
I think this will do for you.
request = $.ajax({
url :'/your_action.do',
type :'post',
cache:false,
data :$("#formId").serialize()
});
the data field will send the required actionForm attributes.

Categories