I have a form on my index.html page which makes a POST request to a Java Servlet. This servlet does some processing and I would like to redirect back to index.html with some variables that the servlet has produced.
In PHP, it would be as simple as:
header("Location: index.html?var1=a&var2=b");
How can I acheive the same with Java, hopefully making use of a GET request.
Thanks all
To redirect from an HTML page, use the META Tag. With this, use the http-equiv attribute to provide an HTTP header for the value of the content attribute. The value of the content is the number of seconds; you want the page to redirect after.
forward() method This method forwards a request from a servlet to another servlet on the same server. It allows one servlet to do the initial processing of a request, obtains the RequestDispatcher object, and forwards the request to another servlet to generate the response.
In a Java Servlet, you'll want to write:
response.sendRedirect("index.html?var1=a&var2=b...");
Oh right, I should note that you'll want to do this in the processor method like doGet() or doPost()...
You redirect the response to the same servlet with some additional values:
req.setAttribute("message","Hello world");
rd =req.getRequestDispatcher("/index.jsp");
And in your servlet, you grab the data with:
<%=request.getAttribute("message");%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With