不工作的注册页面找不到错误

时间:2012-01-23 15:49:07

标签: java sql jsp servlets

我已经创建了一个几乎完整的登录系统,但是我的注册页面不起作用,因为没有在db上写,没有理解为什么......任何想法?

这是jsp登录页面“LoginPage.jsp”:

 <%@page contentType="text/html" pageEncoding="UTF-8"%> 
 <table>
 <form action="RegistraUtenteServlet" method="get">
 <tr><td><b>Nome</b>
 <td><input type="text" name="nomeDaRegistrare"><td></tr>
 <tr><td><b>Cognome</b></td>
 <td><input type="text" name="cognomeDaRegistrare"></td></tr>
 <tr><td><b>Email</b></td>
 <td><input type="text" name="emailDaRegistrare"></td></tr>
 <tr><td><b>Username</b></td>
 <td><input type="text" name="passwordDaRegistrare"></td></tr>
 <tr><td><input type="submit" value="Registrati"></td></tr>
 </form>
 </table>

这是经理类“UtentiManager.java”:

 public class UtentiManager {

public void registraUtente(String nomeDaRegistrare, String cognomeDaRegistrare,String emailDaRegistrare, int tipoDaRegistrare, String passwordDaRegistrare) throws SQLException {
    Connection con = DBConnectionPool.getConnection();
    String query = "INSERT INTO utenti(nome,cognome,email,tipo,password) VALUES(?,?,?,?,?)";
    PreparedStatement ps=con.prepareStatement(query);
    ps.setString(1, nomeDaRegistrare);
    ps.setString(2, cognomeDaRegistrare);
    ps.setString(3, emailDaRegistrare);
    ps.setInt(4, tipoDaRegistrare);
    ps.setString(5, passwordDaRegistrare);
    ps.executeUpdate();
    ps.close();
}
}

这是servlet“RegistraUtenteServlet.java”:

 public class RegistraUtenteServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        UtentiManager um = new UtentiManager();
        String nomeDaRegistrare = request.getParameter("nomeDaRegistrare");
        String cognomeDaRegistrare = request.getParameter("cognomeDaRegistrare");
        String emailDaRegistrare = request.getParameter("emailDaRegistrare");
        String passwordDaRegistrare = request.getParameter("passwordDaRegistrare");
        int tipoDaRegistrare = 1;
        um.registraUtente(nomeDaRegistrare, cognomeDaRegistrare, emailDaRegistrare, tipoDaRegistrare, passwordDaRegistrare);
    }  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(RegistraUtenteServlet.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(RegistraUtenteServlet.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>
 }

请帮忙!

1 个答案:

答案 0 :(得分:0)

您的registraUtente方法永远不会提交并关闭连接。将其更改为

public void registraUtente(String nomeDaRegistrare, String cognomeDaRegistrare,String emailDaRegistrare, int tipoDaRegistrare, String passwordDaRegistrare) throws SQLException {
    Connection con = DBConnectionPool.getConnection();
    PreparedStatement ps = null;
    try {
        String query = "INSERT INTO utenti(nome,cognome,email,tipo,password) VALUES(?,?,?,?,?)";
        ps = con.prepareStatement(query);
        ps.setString(1, nomeDaRegistrare);
        ps.setString(2, cognomeDaRegistrare);
        ps.setString(3, emailDaRegistrare);
        ps.setInt(4, tipoDaRegistrare);
        ps.setString(5, passwordDaRegistrare);
        ps.executeUpdate();
        con.commit();
    }
    finally {
        if (ps != null) {
            try {
                ps.close();
            }
            catch (SQLException ignored) {
            }
        }
        try {
            con.close();
        }
        catch (SQLException ignored) {
        }
    }
}