Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SERVLETS: Create the following Servlets: 10 pts LoginServlet – Code the servlet

ID: 2247525 • Letter: S

Question

SERVLETS:

Create the following Servlets: 10 pts

LoginServlet – Code the servlet to retrieve the username and password from the form we added to login.html. Have the servlet check that the username is equal to HYPERLINK "mailto:jsmith@toba.com" jsmith@toba.com and the password is equal to “letmein”. Make sure you use these values or I can’t test your work. If the username and password match, the servlet to forward the request to the account_activity.html page. If it is incorrect, it should forward the request to the login_failure.html page.

NewCustomerServlet – Code the servlet to retrieve the new customer form data and just redirect to the success.html page.

TransactionServlet – Just create an empty servlet for now.

Server Side Validation – Code the NewCustomerServlet to validate the user has entered in values for all the form fields. If not, assign a message variable such as “Please fill out all the form fields” and display the message on the new_customer.jsp page. 10 pts

Explanation / Answer


Step 1: Login.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet">
Username
<input type="text" name="username"/><br>
Password
<input type="text" name="password"/>
<input type="submit" value="Submit">
</form>
</body>
</html>

Step 2: Create s servlet named login servlet
LoginServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try
{
UserBean user = new UserBean();
user.setUserName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user = UserDAOClass.login(user);

if (user.isValid())
{

HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("account_activity.html");
}

else
response.sendRedirect("login_failure.html");
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
}
Step 3: create a userBean class
public class UserBean {
private String username;
private String password;
private String firstName;
private String lastName;
public boolean valid;
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
public String getPassword() {
return password;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getUsername() {
return username;
}
public void setUserName(String newUsername) {
username = newUsername;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean newValid) {
valid = newValid;
}
}
Step 4 : UserDAOClass:
import java.text.*;
import java.util.*;
import java.sql.*;
public class UserDAOClass
{
static Connection currConnection = null;
static ResultSet rs = null;
public static UserBean login(UserBean bean) {
Statement stmt = null;
String username = bean.getUsername();
String password = bean.getPassword();   

String searchQuery =
"select * from users where username='"
+ username
+ "' AND password='"
+ password
+ "'";

System.out.println("Your user name: " + username);
System.out.println("Your password:" + password);
System.out.println("Query searched: "+searchQuery);

try
{
  
currConnection = ConnectionManager.getConnection();
stmt=currConnection.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean boo = rs.next();

if (!boo)
{
System.out.println("you are not a registered user please sign up first!");
bean.setValid(false);
}


else if (boo)
{
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");

System.out.println("Welcome " + firstName);
bean.setFirstName(firstName);
bean.setLastName(lastName);
bean.setValid(true);
}
}
catch (Exception ex)
{
System.out.println("Login failed due to exception" + ex);
}
finally
{
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {}
stmt = null;
}
if (currConnection != null) {
try {
currConnection.close();
} catch (Exception e) {
}
currConnection = null;
}
}
return bean;
}
}

Step 5: ConnectionManager class
import java.sql.*;
import java.util.*;
public class ConnectionManager {
static Connection conObj;
static String url;

public static Connection getConnection()
{
  
try
{
String url = "jdbc:odbc:" + "DataSource";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  
try
{
conObj = DriverManager.getConnection(url,"jsmith@toba.com","letmein");

}
  
catch (SQLException ex)
{
ex.printStackTrace();
}
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
return conObj;
}
}
Step 6: account_activity.html:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1256">
<title> Login Successfully </title>
</head>
<body>
<center>
<% UserBean currentUser = (UserBean (session.getAttribute("currentSessionUser"));%>
Welcome <%= currentUser.getFirstName() + " " + currentUser.getLastName() %>
</center>
</body>
</html>
Step 7: login_failure.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Error Login</title>
</head>
<body>
<center>
Sorry, credentials are incorrect. please try again later!
</center>
</body>
</html>