In: Computer Science
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you “ChattBank” Project. This MyServlet will display a message like “Go Braves” in a simple <h1> tag. Run this servlet from a Browser window by typing in the servlet name in the URL line.
(ie. http://localhost:8080/ChattBank/MyServlet). Make sure that your Server is up and running before you test this Servlet. The best way to do this is just Run your “ChattBank” Project once before you test the Servlet. Running the Project will start the Server.
Part II – Next, build a simple Servlet called LoginServlet in your “ChattBank” Project. Now make it so that when the Customer logs in, the LoginServlet will get called and will validate the user id and password.
<form action=”http://localhost:8080/ChattBank/LoginServlet” method=”post”>
Part III – Now, modify the LoginServlet.
Use : request.getParameter() to get these items. At first just read in these 2 strings and display them to the Server Log.
2.) If the id = “admin” and the Password = “123”, return an HTML page that says “Valid Login”.
3.) If not return an HTML page that says “InValid Login”. Use out.println() to send these HTML messages.
4.) Test out your WebApp.
Part IV– Lastly, modify the LoginServlet. This time we are going to go to the database to verify the user login. First look at the ChattBank database. There is a Customers table. In this table there is a UserID and a Passwd. Write the database code, in your LoginServlet to let anyone of these customers login, using their own ids and passwords.
As per the given details the program should be as follows :
1. index.html
<html>
<head>
<title>Chatt Bank</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>Welcome to Chatt Bank</div>
</body>
</html>
2. Login.html
<html>
<head>
<title>Chatt Bank | Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="align-content: center">
<form action="LoginServlet" method="post">
Id: <input type="text" name="id" id="id" value="" />
<br/><br/>
Password: <input type="password" name="password" id="password" />
<br/><br/>
<input type="submit" value="Login" name="Login" />
</form>
</div>
</body>
</html>
3. welcome.html
<html>
<head>
<title>Chatt Bank</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>Welcome to Chatt Bank</div>
</body>
</html>
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
5. MyServlet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Chatt Bank</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Go Braves </h1>");
out.println("<a href=\"Login.html\">Go for login</a>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet reques
HOPE THIS MAY HELP YOU---
THANK YOU ……. PLEASE LIKE,IT WILL INCREASE MY SCORE,
HOPE YOU WILL ENCOURAGE ME…..