处理servlet时获取空白屏幕

时间:2011-10-03 23:08:42

标签: jsp servlets

我在处理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>

}

有任何错误或变化吗?

2 个答案:

答案 0 :(得分: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 -->

答案 1 :(得分:0)

您需要out.println()每行要在响应中看到的整个HTML文件行,或者重定向到另一个HTML