Pages

Thursday 2 January 2020

Servlet


Servlet is a web technology that is used to develop server-side web components.
Web components reside in server and perform the following:
                       Receive the incoming request data
                       Process the request
                       Prepare the HTML response
Servlets will be used to develop Dynamic Web application
One web application can have one or more web components like Servlets and JSPs.
One web container can have one or more web applications
One web server will have only one web container.
Client-Server

Steps to develop and run the first Servlet 4.x based Web App.
1. Consider the login requirement.
Assignment

2. Create the Dynamic Web Project in STS/eclipse as follows:
            Select File->New->Dynamic Web Project 
                  Project Name: Lab1
                  Click on New Runtime button (for the first time)
                                Select Apache Tomcat v9.0 and click on Next button
                                Browse the Tomcat Home Directory i.e D:\apache-tomcat-9.0.30
                                Click on the finish button
                 Click on the finish button
3. Observe the following Directory structure of Dynamic Web Project in STS.
4. Create login.html under the WebContent folder.
5. Create a package com.demo.servlets under Java Rsources>src
6. Create LoginServlet.java file in package com.demo.servlets.
7. Deploy into Tomcat 9 as follows
                 Right-click on Lab1
                 Select Run As->Run on Server
8. Open the Browser
9. Provide the following URL
   http://localhost:8080/Lab1/login.html

Required Files
login.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form action="login.do">
  9. <h1>Account Login</h1>
  10. <p>Username</p>
  11. <input type="text" name="username"/>
  12. <p>Password</p>
  13. <input type="password" name="password"/>
  14. <p><input type ="submit" value="Login"/></p>
  15. </form>
  16. </body>
  17. </html>
LoginServlet.java
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;

  8. @WebServlet("/login.do")
  9. public class LoginServlet extends HttpServlet {
  10. private static final long serialVersionUID = 1L;
  11. protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. String username= request.getParameter("username");
  13. String password= request.getParameter("password");
  14. System.out.println(username);
  15. String message="";
  16. if(username.equals(password)) {
  17. message ="<h1>Welcome"+username+"<h1>";
  18. }else {
  19. message="<h1> Invalid username or password</h1>";
  20. }
  21. PrintWriter out = response.getWriter();
  22. out.write(message);
  23. out.flush();
  24. out.close();
  25. }
  26. }