2012-01-23 48 views
-1

我創建了一個幾乎滿工作登錄系統,但是我的註冊頁面不能正常工作,因爲沒有什麼是對數據庫寫的,不明白爲什麼...任何想法?不工作寄存器頁面無法找到錯誤

這裏的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> 
} 

請幫幫忙!

+0

什麼不起作用?有什麼異常?什麼是預期的和實際的行爲? –

+0

即使像登錄和showUsers工作應用程序的其他組件,該INSERT是不行的,沒有什麼是插入分貝 – Franky

回答

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) { 
     } 
    } 
} 
+0

全部工作現在... 我有一個問題,如果你想.. 爲什麼同樣的代碼,而不「提交」在一個簡單的java應用程序中工作,並不能在jsp/servlet頁面中工作? – Franky

+1

因爲簡單的Java應用程序可能直接從驅動程序獲得其連接,該驅動程序返回啓用自動提交的連接。但自動提交應該幾乎從不使用。您的web應用程序可能使用連接池,該連接池會返回禁用自動提交的可連接連接(這是件好事)。 –

+0

非常感謝:)) – Franky