2014-12-05 174 views
-1

我們需要使用Java,HTML,Beans和JSP編寫Web應用程序。問題是我從來沒有參加過HTML或CSS課程。Servlet不指向JSP頁面

我有一個名爲index.html的基礎HTML頁面,它在一個帶有提交按鈕的文本字段中接受用戶的名字和姓氏。當我按提交時,我得到一個「頁面無法顯示的錯誤。」我將我的代碼與我的導師進行了比較,它非常相似,所以我不確定我做錯了什麼。任何幫助將不勝感激。

此外,不介意我可怕的格式。

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Final Project</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <h1>Welcome to Banking by Tyler Weaver</h1> 
     <h4>Please Enter your First and Last Name</h4> 
     <form action="BankingControl" method="POST"> 
      <input type="hidden" name="action" value="Menu"> 
      First Name: 
      <input type="text" name="FirstName" required/> <br></> 
      Last Name: 
      <input type="text" name="LastName" required/> <br></> 
      <input type="submit" value="Login"/> 
     </form> 
    </body> 
</html> 

BankingControl.java

import beans.User; 
import database.MySQL; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.util.logging.Level; 
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 utilities.ErrorLogger; 

@WebServlet(name = "BankingControl", urlPatterns = {"/BankingControl"}) 
public class BankingControl extends HttpServlet { 

    private static final String mysqlPrefix = "jdbc:mysql://"; 
    private static final String hostname = "cs3db.bloomu.edu"; 
    private static final String databaseName = "CS3"; 
    private static final String databaseURL = mysqlPrefix + hostname + "/" + databaseName; 
    private static final String userName = "tgw46366"; 
    private static final String password = "tgw46366"; 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 

     //Creates mysql database connection with specified information 
     MySQL mysql = new MySQL(databaseURL, userName, password); 

     //The URL where processor will send. 
     String JSP_URL = "/index.html"; 

     //Tries making a connection to the database 
     try (Connection conn = mysql.getConnection()) { 
      ErrorLogger.log(Level.INFO, "Database Connection Obtained"); 

      //Retrieve action from page 
      String action = request.getParameter("action").trim(); 
      //If action is null, make it menu 
      if (action == null) { 
       action = "Menu"; 
      } 

      User user = new User(); 
      user.setFirstName(request.getParameter("firstName").trim()); 
      user.setLastName(request.getParameter("lastName").trim()); 
      request.setAttribute("User", user); 

      //Check to see if action is menu 
      if (action.equalsIgnoreCase("Menu")) { 
       //Place to send information 
       JSP_URL = "/Menu.jsp"; 

       ErrorLogger.log(Level.INFO, "User " + user.getFirstName() + " " 
         + user.getLastName() 
         + " logged in - Menu Page Returned"); 
      } else if (action.equalsIgnoreCase("NewCustomer")) { 
       JSP_URL = "/NewCustomer.jsp"; 

       ErrorLogger.log(Level.INFO, "New Customer Selected"); 
      } else if (action.equalsIgnoreCase("NewAccount")) { 
       JSP_URL = "/NewAccount.jsp"; 

       ErrorLogger.log(Level.INFO, "New Account Selected"); 
      } else if (action.equalsIgnoreCase("AccountTransaction")) { 
       JSP_URL = "/Transaction.jsp"; 

       ErrorLogger.log(Level.INFO, "Account Transaction Selected"); 
      } else if (action.equalsIgnoreCase("Logout")) { 
       JSP_URL = "/index.html"; 

       ErrorLogger.log(Level.INFO, "Logout Selected"); 
      } else { 
       ErrorLogger.log(Level.WARNING, 
         "Invalid Option -- index.html Returned"); 
      } 

      //If connection cannot be made, throw here 
     } catch (SQLException ex) { 
      ErrorLogger.log(Level.SEVERE, 
        "Not Making a Database Connection at this Time", ex); 
     } 

     //forward to correct JSP 
     getServletContext() 
       .getRequestDispatcher(JSP_URL) 
       .forward(request, response); 
    } 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    } 

} 

MySQL.java

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import utilities.ErrorLogger; 

public class MySQL { 

    private final String databaseURL; 
    private final String userName; 
    private final String password; 

    public MySQL(String databaseURL, String userName, String password) { 
     this.databaseURL = databaseURL; 
     this.userName = userName; 
     this.password = password; 
     initDB(); 
    } 

    private void initDB() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); //Not needed for MySQL - here for show only 
     } catch (ClassNotFoundException ex) { 
      ErrorLogger.log(Level.SEVERE, "Could not find the class com.mysql.jdbc.Driver \n" 
        + "Program will now exit. ", ex); 
      System.exit(1); 
     } catch (InstantiationException ex) { 
      ErrorLogger.log(Level.SEVERE, "Could not instaniate the class com.mysql.jdbc.Driver \n" 
        + "Program will now exit. ", ex); 
      System.exit(1); 
     } catch (IllegalAccessException ex) { 
      ErrorLogger.log(Level.SEVERE, "Could not access the class com.mysql.jdbc.Driver \n" 
        + "Program will now exit. ", ex); 
      System.exit(1); 
     } 
     try { 
      Connection conn = DriverManager.getConnection(databaseURL, userName, password); 
      conn.close(); 
     } catch (SQLException ex) { 
      ErrorLogger.log(Level.SEVERE, "Could not connect to the database. " 
        + "Database string = " 
        + databaseURL + " user = " + userName + " password " + password, ex); 
      System.exit(1); 
     } 
    } 

    public Connection getConnection() { 
     Connection conn = null; 
     try { 
      conn = DriverManager.getConnection(databaseURL, userName, password); 
     } catch (SQLException e) { 
      ErrorLogger.log(Level.SEVERE, "Could not connect to the database. " 
        + "Database string = " 
        + databaseURL + " user = " + userName + " password " + password); 
      System.exit(1); 
     } 
     return conn; 
    } 

    public void closeConnection(Connection connection) { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
       ErrorLogger.log(Level.SEVERE, "SQL Exception is thrown while " 
         + "trying to close a Connection object. The connection " 
         + "object was not null.", e); 
      } 
     } 
    } 

} 

ErrorLogger.java

import java.io.IOException; 
import java.sql.Date; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.logging.FileHandler; 
import java.util.logging.Handler; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ErrorLogger { 

    private static final String errorFileName = "SQLErrorLogger"; 
    public static boolean showLogInErrorWindow = true; 

    private static Logger errorLogger; 

    private static void initializeLogging() throws IOException { 
     String logFile = getLogfileName(); 
     errorLogger = Logger.getLogger(logFile); 
     Handler handler = new FileHandler(logFile); 
     handler.setFormatter(new java.util.logging.SimpleFormatter()); 
     if (showLogInErrorWindow) { 
      errorLogger.setUseParentHandlers(true); 
     } else { 
      errorLogger.setUseParentHandlers(false); 
     } 
     errorLogger.addHandler(handler); 
    } 

    private static String getFormattedDate(Date date) { 
     DateFormat format; 
     format = new SimpleDateFormat("MM-dd-yyyy"); 

     return (format.format(date)); 
    } 

    private static String getFormattedTime(Date date) { 
     DateFormat format; 
     format = new SimpleDateFormat("hh.mm.a"); 

     return (format.format(date)); 
    } 

    private static String getDateTime(Date date) { 
     String sDateTime = getFormattedDate(date) + "_" 
       + getFormattedTime(date); 
     return sDateTime; 
    } 

    private static String getLogfileName() { 
     String logFileName = errorFileName; 

     Date date = new Date(System.currentTimeMillis()); 
     String sFormattedDateTime = getDateTime(date); 
     logFileName += "_" + sFormattedDateTime; 
     logFileName += ".log"; 

     return logFileName; 
    } 

    public static String getNewLogFileName(String logFileBase, String logFileExt) { 
     String logFile = logFileBase; 
     Date date = new Date(System.currentTimeMillis()); 
     String sFormattedDateTime = getDateTime(date); 
     logFile += "_" + sFormattedDateTime; 
     logFile += logFileExt; 
     return logFile; 
    } 

    public static void log(Level level, String message, Throwable ex) { 
     if (errorLogger == null) { 
      initLogger(); 
     } 
     errorLogger.log(level, message, ex); 
    } 

    public static void log(Level level, String message) { 
     if (errorLogger == null) { 
      initLogger(); 
     } 
     errorLogger.log(level, message); 
    } 

    private static void initLogger() { 
     try { 
      initializeLogging(); 
     } catch (IOException ex) { 
      errorLogger = Logger.getLogger(getLogfileName());//Will not write to a file 
      errorLogger.log(Level.SEVERE, "Could not create a file handler for teh error logger"); 
     } 
    } 

    public static void main(String[] args) { 
     ErrorLogger.log(Level.SEVERE, "Test error Message"); 
    } 

} 

這應該是重現除了示例數據庫和信息之外哪些是錯誤的最低要求。所有JSP頁面單獨加載,但不會從index.html開始。它使調試應用程序的其餘部分變得困難。任何幫助將不勝感激。

目錄:Director img


錯誤的形象! First image

ErrorImage

+0

您應該提供完整的web.xml以及您錯誤的含義(檢查控制檯 - 服務器返回的錯誤或瀏覽器根本不想連接)。夥計們,請盡最大努力! – 2014-12-05 19:01:41

+0

我想看看你可能有的目錄結構和其他控制器。您正在重定向到一個資源,但我不知道該資源是否位於與root相同的目錄中,或者是否位於其他位置?乾杯。 – lupus137 2014-12-05 19:06:45

+0

@MichalWilkowski如何顯示web.xml?我將在上面列出我的目錄結構! – 2014-12-05 19:09:37

回答

1

只是把答案在這裏如果有人運行到這一點。問題是由於缺少罐子而發生的內部服務器錯誤。