2011-10-03 116 views
0

處理servlet時出現空白屏幕。之前它給了我一些錯誤,但在代碼修復後,它停止給我錯誤,並開始給我一個空白的屏幕。處理servlet時獲取空白屏幕

JSP文件

<%-- 
    Document : index 
    Created on : Oct 2, 2011, 3:59:35 PM 
    Author  : Ankur 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>ABC Corporation</title> 
    <h1>Terminal Login</h1> 
</head> 
<body> 
    <form name="login" action="/WebAccount/LoginServlet?" method="post" /> 
    Username: <input type="text" name="username" value=""/> 
    Password: <input type="text" name="password" value=""/> 
    <input type="submit" value="LOGIN"/> 

    Not User? Register Here: <input type="submit" action="/WebAccount/register.jsp" value="REGISTER"> 

</body> 
</html> 

Servlet的文件 公共類LoginServlet延伸的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, SQLException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 

    String usern = request.getParameter("username"); 
    String passw = request.getParameter("password"); 
    try { 

     Class.forName("com.mysql.jdbc.Driver"); 
     String url = "jdbc:mysql://localhost:3306/account"; 
     Connection conn = DriverManager.getConnection(url, "root", "school"); 

     Statement statement = (Statement) conn.createStatement(); 

     ResultSet rs = statement.executeQuery("SELECT * from Users where username='" + usern + "' and password='" + passw + "';"); 

     String user, pass; 

     while (rs.next()) { 
      user = rs.getString("username"); 
      pass = rs.getString("password"); 

      if (usern.equals(user) && passw.equals(pass)) { 
       out.println("Success!"); 
       conn.close(); 
      } 
     } 
     if (!rs.next()) { 
      out.println("Login failed"); 
      conn.close(); 
     } 
    } catch (SQLException ex) { 
     throw new ServletException("Cannot Connect", ex); 
    } catch (ClassNotFoundException ex) { 
     throw new ServletException("Login failed", ex); 

    } finally { 
     out.close(); 
    } 
} 

// <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 { 
    try { 
     processRequest(request, response); 
    } catch (SQLException ex) { 
     Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

/** 
* 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 { 
    try { 
     processRequest(request, response); 
    } catch (SQLException ex) { 
     Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

/** 
* Returns a short description of the servlet. 
* @return a String containing servlet description 
*/ 
@Override 
public String getServletInfo() { 
    return "Short description"; 
}// </editor-fold> 

}

任何錯誤或變化?

+0

整個HTML文件行這是非常不可能的,任何人將調試此代碼。您需要使用更多日誌記錄,調試或線路監控來查找您的問題。 – bmargulies

回答

0

我可以看到的唯一的錯誤駐留在您的jsp中,請注意您正在關閉表單標記並且不在其中輸入字段。

的形式應該是:

<form name="login" action="/WebAccount/LoginServlet?" method="post"> <!-- Remove the/--> 
    Username: <input type="text" name="username" value=""/> 
    Password: <input type="text" name="password" value=""/> 
    <input type="submit" value="LOGIN"/> 
</form> <!-- add the form closing tag --> 
0

您需要out.println()每行要在響應看出或做出重定向到另一個HTML