Question

Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...

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 IINext, 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.

  1. At first, just make sure that the Servlet gets called correctly. So just print a simple message like “LoginServlet Running…”.
  2. Remember, to call the LoginServlet, you will need to modify the FORM tag in the “Login.html” file:

<form action=”http://localhost:8080/ChattBank/LoginServlet”      method=”post”>

  1. Test it out. When you click the Login Button on the LoginForm, you should see “LoginServlet Running….”

Part IIINow, modify the LoginServlet.

  1. Make it so that when the Servlet gets called, it reads the id and password from the Login Form.

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 IVLastly, create a new LoginServletDB. 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.

  1. Write the database code, in your LoginServlet to let anyone of these customers login, using their own ids and passwords.(Hint: You will need to add all 6 database steps[Load Driver, get connection….] to your LoginServletDB. Then get the password from the database abd compare it to the one you read from the HTML file.
  2. Remember, to call the LoginServletDB, you will need to modify the FORM tag in the “Login.html” file:

<form action=”http://localhost:8080/ChattBank/LoginServletDB"

ALSO NEED HELP CONNECTING DATABASE AND HOW TO ADD THE ACCCESS FILES TO NETBEANS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers:

I have used Mysql database. so use suitable jdbc driver to run this code. Drivers are mentioned in lib folder. Please find screenshot of project structure for the same .

Part I :

package com.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public MyServlet() {
  
}

   /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       response.getWriter().append("<h1>Go Braves</h1>");
   }


}

===============================================================================================

Part II & III

LoginServlet.java

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}

   /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
       PrintWriter out = response.getWriter();
       String id = request.getParameter("id");
       String password = request.getParameter("password");
      
       if(null != id && null != password && id.equals("admin") && password.equals("123")) {
           response.sendRedirect("http://localhost:8080/ChattBank/ValidLogin.html");
       }else {
           out.println("InValid Login");
       }
   }

}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<!-- for Part II & III -->
<form action="http://localhost:8080/ChattBank/LoginServlet" method="post">
ID:<input type="text" name="id"/><br/><br/>
Password:<input type="password" name="password"/><br/><br/>
<input type="submit" value="login"/>
</form>

</body>
</html>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

ValidLogin.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <h1>Valid Login</h1>
</body>
</html>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Part IV

Creation of database

create table Customers (
UserID char(20),
Passwd char(20)
);

insert into Customers values ("admin", "123");

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

LoginServletDB.java

package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginServletDB
*/
@WebServlet("/LoginServletDB")
public class LoginServletDB extends HttpServlet {
   private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginServletDB() {
super();
}

   /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
       PrintWriter out = response.getWriter();
       String id = request.getParameter("id");
       String password = request.getParameter("password");
      
       try {
           Class.forName("com.mysql.jdbc.Driver");
           Connection con=DriverManager.getConnection(
           "jdbc:mysql://localhost:3306/test","root","root123");
          
           PreparedStatement ps=con.prepareStatement(
           "select * from Customers where UserID=? and Passwd=?");
           ps.setString(1,id);
           ps.setString(2,password);
          
           ResultSet rs=ps.executeQuery();
           boolean status = rs.next();
          
           if(status) {
               response.sendRedirect("http://localhost:8080/ChattBank/ValidLogin.html");
           }else {
               out.println("InValid Login");
           }
          
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch (SQLException e) {
           e.printStackTrace();
       }
      
   }

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<!-- for Part IV -->
<form action="http://localhost:8080/ChattBank/LoginServletDB" method="post">
ID:<input type="text" name="id"/><br/><br/>
Password:<input type="password" name="password"/><br/><br/>
<input type="submit" value="login"/>
</form>

</body>
</html>

----------------------------------------------------------------------------------------------------------------------------------------------------------

ValidLogin.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <h1>Valid Login</h1>
</body>
</html>

==============================================================================================

Screenshots:

Add a comment
Know the answer?
Add Answer to:
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help creating a netbeans application and linking database with steps Part II – Next,...

    I need help creating a netbeans application and linking database with steps Part II – Next, build a simple Servlet called LoginServlet in your “Bank” Project. Now make it so that when the Customer logs in, the LoginServlet will get called and will validate the user id and password. At first, just make sure that the Servlet gets called correctly. So just print a simple message like “LoginServlet Running…”. Remember, to call the LoginServlet, you will need to modify the...

  • Project Description In this project, you will be developing a multithreaded Web server and a simple...

    Project Description In this project, you will be developing a multithreaded Web server and a simple web client. The Web server and Web client communicate using a text-based protocol called HTTP (Hypertext Transfer Protocol). Requirements for the Web server The server is able to handle multiple requests concurrently. This means the implementation is multithreaded. In the main thread, the server listens to a specified port, e.g., 8080. Upon receiving an HTTP request, the server sets up a TCP connection to...

  • Hi I need some help writing a security code using python mongodb restful api. I just...

    Hi I need some help writing a security code using python mongodb restful api. I just need help on how to provide a security login to user to enter their username and password or to have a block in using extra access once they have logined and they want more access to the databases they will be force to sign out of the server but I just do not know how to start on it can someone show me how...

  • I have a web development project. First, it requires to have a website with a simple...

    I have a web development project. First, it requires to have a website with a simple design like the picture. Second, after fill in the information in the text box, click submit, the information will be save in the database using MySQL. Must have a captcha (like Google Captcha) to reduce spam. Also, must have a database password encryption. You can code this using php, html for coding, and css for designing. Third, code a php file to show all...

  • 12. Suppose you are going to make an online questionnaire to collect information of current trend...

    12. Suppose you are going to make an online questionnaire to collect information of current trend of the social networking App. The questionnaire has already been drafted for you as shown below. When the questionnaire is submitted to the Web server, the server-side logic will check whether all necessary information has been entered. If not, an error message "The survey is not completed!” will appear on a web page and it will automatically divert the browser to the questionnaire page...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- 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. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • You are designing a simple system for managing grades of students in a computer science course...

    You are designing a simple system for managing grades of students in a computer science course and need to track each student's first name, last name, eight-digit CUNY Id, grade, and courseld - referencing a second table with all the names and numbers of computer science courses. [1] Write the SQL that creates these two tables, including any designation of keys. (Assume that the database itself already exists.) 2] Write the code for a client-side search form that allows a...

  • URGENT HELP NEEDED: JQuery. PLEASE POST SCREEN SHOTS Task 1: Downloading jQuery Right-click the link to...

    URGENT HELP NEEDED: JQuery. PLEASE POST SCREEN SHOTS Task 1: Downloading jQuery Right-click the link to download the uncompressed latest version of jQuery Copy the jQuery.x.x.x.js file in the folder and specified as source file. Task 2: Download and install HTML-Kit 1. Navigate to htmlkit.com. 2. Click Download HTML-Kit 292. After it downloads, launch HKSetup.exe. Choose Full installation (the default) Uncheck Yes, download and install HTML-Kit Tools Trial. 6. Click Next>Finish. Task 3: Creating a Simple jQuery Application Launch HTML-Kit....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT