Java Netbeans Web Application and Servlet Create a web application that ask user
ID: 3903966 • Letter: J
Question
Java Netbeans Web Application and Servlet
Create a web application that ask users to login or register if the user has not registered before
On Register
the application is to read the user information (first name, last name, email, username,
password) process the information by saving it to a file.
The registration information should be sent to a server that saves the data in a file, the server is
to return an a result indicating that the action was successful, unsuccessfull, or that the user is
already registered
if the user has already registered, then let the user know and ask that s/he logins in.
Once a user register, return the main page and have the user login
when a user login that the information is sent to the server to check whether the username and
password are valid. The server is to respond by yes or no
If not valid return to the user and have the user try again. The user should get only 3 login trials
after which s/he is denied entry.
Once the user login is valid, the user is displayed a new html page that contains the company
logo and a welcome screen.
Explanation / Answer
login.html
-------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h1>Login Form</h1>
<form action="UserValidation" method="post" >
Username :<input type="text" name="userName" /><br/>
Password :<input type="password" name="password" /><br/>
<input type="submit" value="Login" />
<a href="/register.html" > Register </a>
</form>
</body>
</html>
register.html
------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration Page</title>
</head>
<body>
<h1>Registration Form</h1>
<form action="RegistrationServlet" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="firstname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>UserName</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password" /></td>
</tr>
</table>
<input type="submit" value="Register" />
</form>
</body>
</html>
welcome.html
---------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome</h1>
<img src="/html/images/logo.jpg" alt = "LOGO" />
</body>
</html>
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">
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>UserValidation</servlet-name>
<servlet-class>UserValidation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserValidation</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserRegistration</servlet-name>
<servlet-class>UserRegistration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserRegistration</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
UserValidation.java
-----------------------------
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class UserValidation extends HttpServlet {
private static final long serialVersionUID = 1L;
private int count = 0;
@Override
protected String doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String password = request.getParameter("password");
try {
// Connection to database. Drivers and sql credential details vary as per your requirements
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/login", "root", "");
PreparedStatement pst = conn.prepareStatement("Select userName,password from user where userName=? and password=?");
pst.setString(1, userName);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
//can print logo and any message
out.println("Welcome to my company");
return "YES";
}
else {
if(count < 3) {
out.println("Incorrect login credentials");
out.println("You can only attempt thrice. Make sure to enter correct credentials");
count++;
return "NO";
}
else {
return null;
}
}
conn.close();
}
catch (ClassNotFoundException | SQLException exception) {
exception.printStackTrace();
}
}
}
UserRegistration.java
--------------------------------
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class UserRegistration extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
// Connection to database. Drivers and sql credential details vary as per your requirements
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/login", "root", "");
Statement st = conn.createStatement();
if(firstname.isEmpty()||lastname.isEmpty()||email.isEmpty()||userName.isEmpty()||password.isEmpty())
{
RequestDispatcher rd = request.getRequestDispatcher("register.html");
out.println("<font color=red>Please fill all the fields</font>");
rd.include(request, response);
}
else {
int i = st.executeUpdate("insert into user(firstname, lastname, email, userName, password, regdate) values"
+ " ('" + firstname + "','" + lastname + "','" + email + "','" + userName + "','" + password + "', CURDATE())");
if (i > 0) {
response.sendRedirect("login.html");
out.print("Registration Successfull!");
} else {
response.sendRedirect("login.html");
out.println("You are already registered");
}
}
conn.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.