hi friend,
package javacode;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ForwardServlet extends HttpServlet{
private static final String forwardTo = "/jsp/ResultServlets";
private static final String includeIn = "/jsp/ResultServlets";
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><head><style>");
out.print("p,form{font-family:tahoma;font-size:10pt;}");
out.print("input{width:20;height:20;}");
out.print("</style></head><body>");
// Displaying Form
out.print("<form action=\"");
out.print( req.getRequestURI() );
out.print("\" method=\"post\">");
out.print("<input type=\"hidden\" name=\"mode\" ");
out.print("value=\"forward\">");
out.print("<input type=\"submit\" value=\" \"");
out.print("> ");
out.print(" Forward to another Servlet ..");
out.print("</form>");
out.print("<form action=\"");
out.print( req.getRequestURI() );
out.print("\" method=\"post\">");
out.print("<input type=\"hidden\" name=\"mode\" ");
out.print("value=\"include\">");
out.print("<input type=\"submit\" ");
out.print("value=\" \"> ");
out.print(" Include another Servlet ..");
out.print("</form>");
out.print("</body></html>");
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String mode = req.getParameter("mode");
// Forwarding to Servlet2
if(mode != null && mode.equals("forward")) {
req.setAttribute("mode", "Forwarding Response..");
req.getRequestDispatcher(forwardTo).forward(req, res);
}
// Including response from Servlet2
if(mode != null && mode.equals("include")) {
req.setAttribute("mode", "Including Response..");
req.getRequestDispatcher(includeIn).include(req, res);
}
}
}
-------------------------------------------------
package javacode;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ResultServlets extends HttpServlet{
public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Hello World</title></title>");
out.println("<body>");
out.println("<h1>Welcome to Roseindia</h1>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String mode = req.getParameter("mode");
// Forwarding to Servlet2
if(mode != null && mode.equals("forward")) {
req.setAttribute("mode", "Forwarding Response..");
}
// Including response from Servlet2
if(mode != null && mode.equals("include")) {
req.setAttribute("mode", "Including Response..");
}
}
}
--------------------
Read for more information.
http://www.roseindia.net/servletsThanks