• It is an interface available in the javax.servlet package. The subclass is implemented by container vendor.
• It has two methods, as follows:
o Public void forward (ServletRequest req,ServletResponse res)
o Public void include (ServletRequest req,ServletResponse res)
Forward() Method
• Forward() method is used to forward the request from:
o Servlet to HTML
o Servlet to jsp
o Servlet to servlet
o Jsp to html
o Jsp to jsp
o Jsp to servlet.
• You can place many forward() methods in one conditional to forward the control to another resource, but only one forward() will be executed.
Usage:
RequestDispatcher rd=null;
rd=req.getRequestDispatcher(“test.html”);
rd=req.getRequestDispatcher(“test.jsp”);
rd=req.getRequestDispatcher(“testjtcl”);
rd.forward(req,res);
• When you try to invoke the two-forward method, then java.lang.illegalStateException Cannot forward after response has commited
• We can restrict the user from accessing the page directory by placing the page under the WEB-INF directory.
• When we have other statements after the forward method, those statements will be executed, but we will not be able to write any content in the response stream.
Required Files for Program
1. Index.html
2. Web.xml
3. TestServlet.java
1. Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>THIS IS INDEX HTML</h1>
<a href=”test.jtc”>Test</a>
<br/>
</body>
</html>
2. Web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>undefined</web-app>undefined<servlet>
<servlet-name>helloservlet</servlet-name>
<servletclass>com.jtcindia.servlet.HelloServlet</servletclass>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/test.jtc</url-pattern>
</servlet-mapping>
3. TestServlet.java
public class HelloServlet extends HttpServlet {
protected void
service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException,
IOException {
Writer out = res.getWriter();
out.write("<h1>Hi Welcome to jtc");
}
}
The include() method is used to include the response of one servlet or jsp in another servlet or jsp, and after evaluating the request control will return.
We can have more than one include() statement in one servlet or jsp, but we can only have one forward() method in one servlet, or JSP. After using forward, you cannot use request and response object processing statements.
Files Required for Servlet 2.X
1) Index.html
2) Header.html
3) Footer.html
4) Menu.html
5) Home.html
6) Login.html
7) Register.html
8) ShowHomeServlet.java
9) ShowLoginServlet.java
10) ShowRegisterServlet.java
11) Web.xml
1. Index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="showhome.jtc">GO TO HOME</a>
</body>
</html>
2. Header.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>JTC</h2>
</body>
</html>
3. Footer.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>JTC</h2>
</body>
</html>
4. Menu.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<font color="blue" size="3">
<a href="showhome.jtc">HOME</a>
</font>
<font color="blue" size="3">
<a href="showlogin.jtc">LOGIN</a>
</font>
<font color="blue" size="3">
<a href="showregister.jtc">REGISTER</a>
</font>
</center>
</body>
</html>
5. Home.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</center>
<h1>JTC</h1>
</center>
</body>
</html>
6. Login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</center>
<h1>LOGIN PAGE</h1>
</center>
</body>
</html>
7. Register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</center>
<h1>REGISTER PAGE</h1>
</center>
</body>
</html>
8. ShowHomeServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowHomeServlet extends
HttpServlet {
@Override
protected void
service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,
IOException {
System.out.println("service()....Of
ShowHomeServlet started ** ");
RequestDispatcher rd1 = req.getRequestDispatcher("header.html"); rd1.forward(req, res); RequestDispatcher rd2 = req.getRequestDispatcher("menu.html"); rd2.forward(req, res); RequestDispatcher rd3 = req.getRequestDispatcher("home.html"); rd3.forward(req, res); RequestDispatcher rd4 = req.getRequestDispatcher("footer.html"); rd4.forward(req, res); System.out.println("service() of
ShowHomeServlet Completed ");
}
}
9. ShowLoginServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowLoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,
IOException {
System.out.println("service()....Of
ShowLoginServlet started ** ");
RequestDispatcher rd1 = req.getRequestDispatcher("header.html"); rd1.forward(req, res); RequestDispatcher rd2 = req.getRequestDispatcher("menu.html"); rd2.forward(req, res); RequestDispatcher rd3 = req.getRequestDispatcher("home.html"); rd3.forward(req, res); RequestDispatcher rd4 = req.getRequestDispatcher("footer.html"); rd4.forward(req, res); System.out.println("service() of
ShowLoginServlet Completed ");
}
}
10. ShowRegisterServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowLoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,
IOException {
System.out.println("service()....Of
ShowLoginServlet started ** ");
RequestDispatcher rd1 = req.getRequestDispatcher("header.html"); rd1.forward(req, res); RequestDispatcher rd2 = req.getRequestDispatcher("menu.html"); rd2.forward(req, res); RequestDispatcher rd3 = req.getRequestDispatcher("home.html"); rd3.forward(req, res); RequestDispatcher rd4 = req.getRequestDispatcher("footer.html"); rd4.forward(req, res); System.out.println("service() of
ShowLoginServlet Completed ");
}
}