Introduction to Servlet Communication:
The communication between the Java servlets is known as Servlet communication. It is sending users request, and the response object passed to a servlet to another servlet. We are using the method getParameter(), which is basically used to get the input from user value in a specified field. So, when Servlet request the object is passed from one servlet to another servlet, then we will use this method to get the input that the user has given in an HTML/JSP form. Java servlets are the server-side programs (running inside a web server) that can handle the clients’ requests and revert the customized or dynamic response for each request. The dynamic response, which is responded could be based on the user’s input like search, online shopping, online transaction with data retrieved from databases or other applications, or time-sensitive data (such as news and stock prices).
[box type=”success” align=”” class=”” width=””]Java servlets typically run on the HTTP protocol.[/box]
RequestDispatcher in Servlet:
It is an interface that provides the facility of dispatching the request to other resources, and it may be HTML, servlet, or JSP. This RequestDispatcher interface can also be used to include the content of other resources also. There are two methods defined in the RequestDispatcher interface. Methods of RequestDispatcher interface. The RequestDispatcher interface provides two methods. They are:
- public void forward(ServletRequest srequest, ServletResponse sresponse)throws ServletException,java.io.IOException: This method Forwards a request from a servlet to other resources (i.e., servlet, JSP file, or HTML file) on the server.
- public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Includes the content of a resource (servlet, JSP page, or HTML file) in the response.
[divider style=”normal” top=”20″ bottom=”20″]
[tie_list type=”checklist”]Home.html[/tie_list]
<html> <head> <title>Introduction to Servlet Communication</title> </head> <body> <form action="/servlettest/demo" method="post"> FirstName: <input type="text" name="fname" width="20" /> LastName: <input type="text" name="lname" width="25" /> Password: <input type="password" name="passw" width="20" /> <input type="submit" name="submit" value="Login" /> </form> </body> </html>
[divider style=”normal” top=”20″ bottom=”20″]
[tie_list type=”checklist”]Servlet Communication – web.xml[/tie_list]
<web-app> <servlet> <servlet-name>comservlet</servlet-name> <servlet-class>FirstServletTest</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>FinalServletTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>lservlet</servlet-name> <url-pattern>/we</url-pattern> </servlet-mapping> </web-app>
[divider style=”normal” top=”20″ bottom=”20″]
[tie_list type=”checklist”] FirstServletTest.java[/tie_list]
import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class FirstServletTest extends HttpServlet { public void doPost(HttpServletRequest sreq,HttpServletResponse sres) throws ServletException,IOException { String fname=req.getParameter("fname"); String lname=req.getParameter("lname"); String passw=req.getParameter("passw"); getServletContext().getRequestDispatcher("/we").forward(sreq,sres); } }
1.getServletContext():
- It gets the ServletContext object.
- It is an interface, or we say it may be an implementation class of ServletContext is written, and its object is sent.
- It is created by the Servlet Container, her we are using Tomcat, and by using this getServletContext() method, we will call it.
2. getRequestDispatcher():
- getRequestDispatcher() is a method of the ServletContext interface.It returns the RequestDispatcher object with some implementation class is written, and its object is sent (to us).
- The purpose of this getRequestDispatcher() method is to pass the request to other servlets.
[divider style=”normal” top=”20″ bottom=”20″]
[tie_list type=”checklist”]FinalServletTest.java[/tie_list]
import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class FinalServletTest extends HttpServlet { public void doPost(HttpServletRequest sreq,HttpServletResponse sres) throws ServletException,IOException { String fname=req.getParameter("fname"); String lname=req.getParameter("lname"); String passw=req.getParameter("passw"); PrintWriter pw=res.getWriter(); pw.println("<h1>Your firstname is "+fname+"</h1>"); pw.println("<h1>Your lastname is "+lname+"</h1>"); pw.println("<h1>Your password is "+passw+"</h1>"); } }
[box type=”note” align=”” class=”” width=””]
Note: By using methods, the req, res objects which are given by the FirstServletTest.java, and those are used here. These objects are passed from FirstServletTest.java to FinalServletTest.java by Servlet container (Tomcat here).
[/box]
2 Responses