Initiate request from java class
Hello
I got a peculiar question
My web container contains the following
a JSP page page1.jsp
second JSP pagepage2.jsp
A servletPushPage
A java classContactServlet
What I want to achieve is when browser is displaying page1.jsp. The ContactSevlet.java class calls the servlet PushPage which redirects to page2.jsp
I am able to successfully load the servlet from ContactServlet.java since I am able to print the response(the contents of page2.jsp) on to console.
But the page2.jsp does not display on the browser, it still displays page1.jsp after servlet completes its function.
Any idea how can I make the page2.jsp to display on the browser?
In other words i should make the Servlet feel like the request has come from page1.jsp for page2.jsp.
The code for servlet and java class are below
package testpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: PushPage
*
*/
publicclass PushPageextends javax.servlet.http.HttpServletimplements javax.servlet.Servlet{
public PushPage(){
super();
}
protectedvoid processAll(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
response.sendRedirect("http://localhost/rimpush/page2.jsp");
}
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
processAll( request, response);
}
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
processAll( request, response);
}
}
/*Java class*/
package testpackage;
import java.io.*;
import java.net.*;
import javax.servlet.http.HttpServletRequest;
publicclass ContactServlet{
public ContactServlet(){
}
publicstaticvoid main( String [] args ){
/*Call the servlet PushPage*/
try{
URL url =new URL("http://localhost/rimpush/PushPage");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
}
catch ( MalformedURLException ex ){
// a real program would need to handle this exception
}
catch ( IOException ex ){
// a real program would need to handle this exception
}
}
}
Any idea would be gracefully appreciated
Thank you.

