views:

868

answers:

4

Hello all,

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

+2  A: 

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

Eric Wendelin
+1  A: 

You can use

HttpResponse.sendRedirect("Location: index.html?var1=a&var2=b");

See this link for more information.

Tommy Hui
+1  A: 

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");%>
Mork0075
+1  A: 

It is as simple as :

response.sendRedirect("index.html?var1=a&var2=b");
WiseTechi