Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to a html page and pass variables to that page in Java?

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

like image 577
Abs Avatar asked Mar 14 '09 16:03

Abs


People also ask

How do I redirect a URL to another page?

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.

How do I forward a Java request?

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.


2 Answers

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

like image 50
Eric Wendelin Avatar answered Oct 12 '22 19:10

Eric Wendelin


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");%>
like image 29
Mork0075 Avatar answered Oct 12 '22 19:10

Mork0075