Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part I Modify the LoginServlet. change the Login Servlet to get the pw from the Database and check that against the password the User enters

Part I Modify the LoginServlet. change the Login Servlet to get the pw from the Database and check that against the password the User enters in the HTML Gui. Part II Modify the LoginServlet. Use the requestDispatcher to forward control onto an Error Page (ErrorPage.jsp), if the user login is not correct. Part III Modify your LoginServlet again. Again use the requestDispatcher to forward control onto an AccountLookup page (AccountLookup.jsp), Part IV Next, in LoginServlet, put the Customer object in the Session. This Customer object will be used by a later Servlet to display customer info. Part V Now build the AccountLookupServlet. This servlet should read the input from the previous HTML file and Find the Accounts information from the database, using the Account business class, then display the information back to the Server Log. The AccountLookupServlet should be scheduled when the user clicks on the Retrieve button from the AccountLookup.jsp file that was displayed by the LoginServlet. Note: Part VI Lastly, in AccountLookupServlet, put the Account object in the Session. This Account object will be used by a later Servlet to display Account info.

loginservlet

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 LoginServlet 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");

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("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ChattBankMDB"); 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("connected 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"; }//

}

Account.java

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JOptionPane;

/* * 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. */ /** * * @author adithya */ public class Account {

private int AcctNo, Cid; private double Balance; private String Type;

public int getAcctNo() { return AcctNo; }

public void setAcctNo(int AcctNo) { this.AcctNo = AcctNo; }

public int getCid() { return Cid; }

public void setCid(int Cid) { this.Cid = Cid; }

public double getBalance() { return Balance; }

public void setBalance(double Balance) { this.Balance = Balance; }

public String getType() { return Type; }

public void setType(String Type) { this.Type = Type; }

public void selectDB(int acctNo) { try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection conn = DriverManager.getConnection("jdbc:ucanaccess://D:ChattBankMDB.mdb"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("Select * from Accounts where AcctNo = " + acctNo); while (rs.next()) { setAcctNo(rs.getInt(1)); setCid(rs.getInt(2)); setType(rs.getString(3)); setBalance(rs.getDouble(4)); }

} catch (Exception e) { System.out.println(e.getMessage()); }

}

public void deposit(double amt) { setBalance(getBalance() + amt); try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection con = DriverManager.getConnection("jdbc:ucanaccess://D:ChattBankMDB.mdb"); Statement stmt = con.createStatement(); stmt.executeUpdate("update Accounts set [Balance] ="+getBalance()+" where [AcctNo] = "+getAcctNo()); } catch (Exception e) { System.out.println(e.getMessage()); } } public void withdraw (double amt) { setBalance(getBalance() - amt); try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection con = DriverManager.getConnection("jdbc:ucanaccess://D:ChattBankMDB.mdb"); Statement stmt = con.createStatement(); stmt.executeUpdate("update Accounts set [Balance] ="+getBalance()+" where [AcctNo] = "+getAcctNo()); } catch (Exception e) { System.out.println(e.getMessage()); } } public void display(){ JOptionPane.showMessageDialog(null,"Balance = "+getBalance()); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions