Question
I modify my LoginServlet but it still is not working Please advise Modify your LoginServlet.The old LoginServlet passed control to an HTML file called AccountLookup.html.Change
I modify my LoginServlet but it still is not working
Please advise
Modify your LoginServlet.The old LoginServlet passed control to an HTML file called AccountLookup.html.Change this.Have the LoginServlet pass control to different JSP, called "DisplayAccounts.jsp".
Part B - Now build the DisplayAccounts.jsp file. This file will be different than the AccountLookup.jsp that we built in Lab #6. This JSP should get the Customer object out of the Session. This Customer Object should contain the AccountList object that holds a list of Accounts.Then using scriptlet tags, display a table of accounts for this customer.
See Code below.
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.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet2 extends HttpServlet {
/**
* Processes requests for both HTTP GET
and POST
* 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()) {
out.println("LoginServlet Running... ");
String id = request.getParameter("id");
String pass = request.getParameter("password");
// code for Part III - Now, modify the LoginServlet.
/*if (id.contains("admin") && pass.equals("123")) {
out.println("Valid Login");
RequestDispatcher rs = request.getRequestDispatcher("welcome.html");
rs.forward(request, response);
} else {
out.println("InValid Login ");
RequestDispatcher rs = request.getRequestDispatcher("Login.html");
rs.include(request, response);
} */
// Code for Part IV- Lastly, modify the LoginServlet.
// a method for checking user in database
if (checkUser(id, pass)) {
out.println("Valid Login");
RequestDispatcher rs = request.getRequestDispatcher("welcome.html");
rs.forward(request, response);
} else {
out.println("InValid Login ");
RequestDispatcher rs = request.getRequestDispatcher("Login.html");
rs.include(request, response);
}
}
}
/**
* Handles the user validation method.
*
* @param id String user id request
* @param pass user password request
* @return true if valid user, otherwise false
*/
public static boolean checkUser(String id, String pass) {
boolean valid = false;
try {
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
/* Here some modify by you as per your database
1. root- mysql username
2. 12345- mysql user password so please change as per your mysql
3. Bank- database name */
//creating connection with the database
Connection con = DriverManager.getConnection("jdbc:derby:/localhost:1527/MyDatabase");
PreparedStatement ps = con.prepareStatement("SELECT * FROM customers WHERE UserID=? and Passwd=?");
ps.setString(1, id);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
valid = rs.next();
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Can not connect to database" + e);
}
return valid;
}
//
/**
* Handles the HTTP GET
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started