Hello viewers, today I got to know few more things in servlet like heirarchy through which HttpServlet extends, fetching data through request parameter in servlet, printing HTML content in servlet page, RequestDispatcher for including and forwarding the request to other resources. I am gonna discuss about them one by one.
Heirarchy:
As we saw, whenever we create a servlet class, it extends HttpServlet. Now, I will tell the basic heirarchy from which HttpServlet extends.
- First, we have an interface named Servlet which has declaration of methods init, service, destroy.
- GenericServlet class implements this interface Servlet. In GenericServlet there is definition of methods init, service, destroy.
- HttpServlet class extends GenericServlet. HttpServlet class provides the methods such as doGet, doPost, doTrace, doPut etc.
- Finally our servlet class extends HttpServlet to get access to all the important methods for servlet functioning.
Note: doPost method is called when request method is post. doGet method is called when request method is get. service method performs the task of both. It can receive the get as well as post request.
Getting data in servlet:
In service method, we get have two parameters- reference of HttpServletRequest and reference of HttpServletResponse. Using request reference we can get the data which has been sent along with request for example form data in HTML page after submission. Let’s see how you receive that.
index.html-
<!DOCTYPE html> <html > <head><title>Login Form</title></head> <body> <h2>Login to your account</h2> <form action="Login" method="post"> <input type="text" placeholder="Email" name="txtEmail"/> <input type="password" placeholder="Password" name="txtPassword"/> <button type="submit" value="Login">Login</button> </form> </body> </html>
Then in servlet class, you created, write the following code where you will use request.getParameter to get the input values of form elements entered by user.
@WebServlet("/Login")
public class ServletClass extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("txtEmail");
String password = request.getParameter("txtPassword");
//rest of code
}
}
Display HTML content in servlet:
For displaying HTML content in servlet page, PrintWriter is used to print text data to a character stream. request.getWriter returns the PrintWriter object. Now, you can simply write the text to be printed using print method.
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>
<h3>You are successfully logged in</h3>
");
out.println("</body></html>");
RequestDispatcher:
RequestDipatcher is an interface which plays very important role in dispatching the request to other servlet, jsp or html page. Here, we can use two methods for performing two much required functions. They are:
- include: This method includes the content of the resource file which we have specified. Whatever print statements are written in current servlet will be erased as we are including some other resource file.
- forward: This method will forward the request to the specified resource and include the content of that resource file without affecting the content of current(calling) servlet.
include:
RequestDispatcher dispatcher = request.getRequestDispatcher("index.html");
dispatcher.include(request, response);
forward:
RequestDispatcher dispatcher = request.getRequestDispatcher("Home");
dispatcher.forward(request, response);
