2016-06-09 238 views
-4

這裏是Eclipse簡單登錄Servlet的代碼,它從現有的數據庫表中檢查用戶名和密碼,並在存在或發送到登錄頁面時將其帶到主頁。當我在服務器上運行它並將信息放入登錄頁面時,它顯示以下錯誤。你能幫忙嗎?謝謝。ClassNotFoundException的無法訪問的catch塊。這個例外永遠不會從try語句主體中拋出

1.錯誤解決

root cause 


    java.lang.Error: Unresolved compilation problem: 

    Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body 

    com.loginapps.servlets.LoginServlet.doGet(LoginServlet.java:90) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723) 

編碼

package com.loginapps.servlets; 

    import java.io.IOException; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.SQLException; 
    import java.sql.ResultSet; 
    import java.sql.PreparedStatement; 

    import javax.servlet.ServletConfig; 
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import javax.servlet.http.HttpSession; 

/*** Servlet implementation class LoginServlet* @param <con>*/ 

    public class LoginServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 


    Connection con; 
    PreparedStatement pst; 

    public void init(ServletConfig config) throws ServletException { 
    String dname = config.getInitParameter("drivername"); 
    String uname = config.getInitParameter("username"); 

    System.out.println(dname); 
    System.out.println(uname); 

    try{ 
     Class.forName(dname); 
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb",uname,""); 
    } 

    catch(SQLException e){ 
     e.printStackTrace(); 
    } 

    catch(ClassNotFoundException e){ 
     e.printStackTrace(); 
    } 

} 



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 


    HttpSession session = request.getSession(); 

    String uname = request.getParameter("txtuname"); 
    String pwd = request.getParameter("txtpwd"); 

    session.setAttribute("username",uname); 


    try { 
    pst = con.prepareStatement("select * from users where username = ? and password = ?"); 
    pst.setString(1,uname); 
    pst.setString(2,pwd); 

    ResultSet rs = pst.executeQuery(); 
    if(rs.next()){ 
     response.sendRedirect("home.jsp"); 
    } 
    else { 
     response.sendRedirect("login.html"); 
    } 
    } 

    catch(SQLException e){ 
     e.printStackTrace(); 
    } 

    catch(ClassNotFoundException e){ 
     e.printStackTrace(); 
    } 

} 




    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 

     doGet(request, response); 
    } 
    } 
+0

你有tomcat(??)lib目錄下的mysql jar嗎? –

+0

,你可以發佈你的'web.xml' \ – emotionlessbananas

+0

是的。我在WebInf/lib forder中有mysql jar。這裏是web.xml文件。謝謝。 – Nikil

回答

1

您發佈的錯誤:ClassNotFoundException 無法到達的catch塊。此異常不會從try語句體內拋出

catch塊的問題是:

catch(ClassNotFoundException e){ 
     e.printStackTrace(); 
} 

ClassNotFoundException是經過檢查的異常,並永遠不會被代碼所引發的try塊內 - 見this page

+0

當我刪除該catch塊時,它顯示此錯誤。 java.lang.Error:未解決的編譯問題: \t未處理的異常類型ClassNotFoundException。有沒有辦法解決這個問題?謝謝。 – Nikil

+0

您是否刪除了doGet方法中的一個?這就是我指的那個,你可以通過堆棧跟蹤來判斷:com.loginapps.servlets.LoginServlet.doGet(LoginServlet.java:90) –

+0

yes。它的工作非常感謝你的幫助。 – Nikil

0

我已經在doGet方法中刪除catch(ClassNotFoundException e)。它工作正常。

ClassNotFoundException是一個檢查的異常,它不會發生在您的doGet方法代碼中。