开发者

Getting a Blank Screen while processing servlet

I am getting a blank screen while processing the servlet. Earlier It was giving me some errors but after code fixing it stopped giving me error and started giving me a blank screen.

JSP File

<%-- 
    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 File public class LoginServlet 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, 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 {
        process开发者_StackOverflow社区Request(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>

}

Any mistakes or changes?


The only error I can see resides in your jsp, note that you are closing the form tag and not including the input fields into it.

The form should be:

<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 -->


You need to out.println() the entire HTML file line per line you want to see in the response or make a redirect to another HTML

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜