Servlet - Servlet Communication
Communication between Java servlets is called as Servlet communication. It is nothing but, sending users request and the response object passed to a servlet to another servlet. You may ask, what is the use of passing these objects. Well, i have an answer. As seen in the servlet examples written till date, one common method we are using, getParameter(), used to get the user input value in a specified field. So, when a request object is passed from one servlet to another servlet, then you can use this method to get the input that the user has given in a HTML/JSP form. Following example, gives better understand of what is said now.
Servlet Communication - Folder Structure
webapps
|_ servletcom
|_ index.html
|_ WEB-INF
|_ web.xml
|_ classes
|_ FirstServlet.java
|_ FirstServlet.class
|_ FinalServlet.java
|_ FinalServlet.class
Servlet Communication - index.html
<html>
<head>
<title>Servlet-Servlet Communication</title>
</head>
<body>
<form action="/servletcom/pass" method="post">
Name: <input type="text" name="uname" width="20" />
Password: <input type="password" name="pass" width="20" />
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>
action="/servletcom/pass": /pass is the url pattern in web.xml and the servletcom is the name of the project (folder name). The project folder name must be specified here for sure, else the statement means to search in the webapps directory but not within the project.
method="post": Invisible submission, at the background.
name="uname": Used to get the value in this field in Servlet class.
name="pass": Used to get the value in this field in Servlet class.
Servlet Communication - web.xml
<web-app>
<servlet>
<servlet-name>comservlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>comservlet</servlet-name>
<url-pattern>/pass</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>lservlet</servlet-name>
<servlet-class>FinalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>lservlet</servlet-name>
<url-pattern>/we</url-pattern>
</servlet-mapping>
</web-app>
Here i wrote 2 classes, for servlet communication, as /pass is the url pattern pointing that the corresponding action controlling class is FirstServlet, and also, the user's data sent to this url pattern, FirstServlet will be called first by the servlet container. Next one, /we, when servlet container gets request to the url-pattern /we, the FinalServlet class will be invoked.
For better understand, See Understanding web.xml in Java Servlets
Servlet Communication - FirstServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class FirstServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
getServletContext().getRequestDispatcher("/we").forward(req,res);
}
}
getServletContext(): Gets the ServletContext object. ServletContext is an interface, and may be an implementation class of ServletContext is written and it's object is sent. ServletContext is created by the Servlet Container (Tomcat here) and by using this method we will call it. This object contains common data for the entire web application, so reusable and also helps in communicating with the servlet container because it was generated by the Servlet Container (could also be one of the reason).
getRequestDispatcher(): This is a method of the ServletContext interface.This returns RequestDispatcher object which is again an interface and may be some implementation class is written and its object is sent (to us). The purpose of this method is to pass the request to another servlet. It takes String as parameter, which is the url-pattern pointing the servlet that we are going to send the request to (here FinalServlet).
forward(req,res): This is a method in RequestDispatcher interface, that helps us to forward user's request to another servlet (the servlet that the RequestDispatcher object points out to) (FinalServlet here).
Servlet Communication - FinalServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class FinalServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
PrintWriter pw=res.getWriter();
pw.println("<h1>Your username is "+uname+"</h1>");
pw.println("<h1>Your password is "+pass+"</h1>");
}
}
javax.servlet.http.*: Contains HttpServlet, HttpServletRequest, HttpServletResponseclasses (used here).
javax.servlet.*: Contains ServletException (used here).
java.io.*: Contains PrintWriter, IOException (used here).
pw.println("<h1>Your username is "+uname+"</h1>"): Present in the PrintWriter class, prints username (the value that user gives in field named uname).
pw.println("<h1>Your password is "+pass+"</h1>"): Present in the PrintWriter class, prints password (the value that user gives in field named pass).
Note: The req,res objects are given by the FirstServlet.java and those are used here. These objects are passed from FirstServlet.java to FinalServlet.java by Servlet container (Tomcat here).
Another Note: Do not forget to compile the class and re/start tomcat! ;)