I'm trying to create a very simple teller system that will ask the user for an account number, query the account number, then forward the user and the queried account to the JSP to take in transaction information. The system will ask for the the account number, but then goes to a blank page where nothing happens and it never forwards to the JSP. Netbeans doesn't show any errors in the method, html page, or servlet code and the console just keeps saying that the build is successful. I am able to successfully connect to the database using the MySQL Workbench. I'm really new to coding and I'm not really sure what's wrong. Any help would be greatly appreciated.
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Account;
/**
*
* @author b-rad
*/
@WebServlet(urlPatterns = {"/GetCustomer"})
public class Deposit extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* 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()) {
int custAcct = Integer.parseInt(request.getParameter("depAcctNum"));
/*
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://accounts.cl6oevzts9mf.us-east-2.rds.amazonaws.com:3306/";
*/
//Database to access
String dbName = "accounts";
String dbUsername = "root";
String dbPassword = "root";
//create ReadAccount class
ReadAccount ra = new ReadAccount(dbName, dbUsername, dbPassword, custAcct);
// use ReadAccount to get account
ra.doRead();
Account account = ra.getAccount();
//pass account and control to depositTrans.jsp
request.setAttribute("account", account);
String url = "/depositTrans.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
}
catch(Exception e){
System.out.println(e);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> 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 <code>POST</code> 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";
}// </editor-fold>
}
Here is the class to read and query the account:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Account;
/**
*
* @author b-rad
*/
public class ReadAccount {
private Connection connection;
private ResultSet results;
private Account account;
private int custAcct;
public ReadAccount(String dbName, String dbUsername, String dbPassword, int custAcct){
String url = "jdbc:mysql://accounts.cl6oevzts9mf.us-east-2.rds.amazonaws.com:3306/" + dbName;
this.custAcct = custAcct;
try{
Class.forName("com.mysql.jdbc.Driver");
this.connection = DriverManager.getConnection(url, dbUsername, dbPassword);
}catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ReadAccount.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void doRead(){
String query = "select * from accounts where accountNumber = ?";
try{
PreparedStatement ps = connection.prepareStatement(query);
ps.setInt(1, custAcct);
results = ps.executeQuery();
this.results.next();
this.account.setfName(results.getString("fName"));
this.account.setStatus(results.getString("status"));
this.account.setCurrentBal(results.getFloat("currentBal"));
this.account.setAvailableBal(results.getFloat("avaialbleBal"));
this.account.setFloatBal(results.getFloat("floatBal"));
this.account.setCustAcct(results.getInt("accountNumber"));
}catch (SQLException e){
e.printStackTrace();
}
}
public Account getAccount() {
return account;
}
}
and the JSP file that the servlet is supposed to forward to:
<%@page import="model.Account"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% Account account = (Account) request.getAttribute("account"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BankOne Teller_Deposits</title>
</head>
<body>
<h1>BankOne Teller</h1>
<h2>Customer Deposit</h2>
<br/>
<form name="depositTrans" action="depositTrans" method="post">
<br>
Deposit Account Number:<input type="number" name="custAcctNum" value="<%= account.getCustAcct()%>" disabled/>
<br>
Deposit Amount:<input type="number" name="depAmount"/>
<br>
<br>
Cash In:<input type="number" name="depCashIn"/>
<br>
Check In:<input type="number" name="depCheckIn"/>
<br>
<br>
<input type="Submit" value="Finish"/>
</form>
<button onclick="window.location.href='homePage.html'">Cancel</button>
</body>
</html>