这是Servlet中的简单登录Eclipse的代码,该代码可从数据库的现有表中检查用户名和密码,并将其带到主页(如果存在)或发送到登录页面。当我在服务器上运行它并将信息放入登录页面时,它显示以下错误。你能帮忙吗?谢谢。

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);
    }
  }

最佳答案

您发布的错误:ClassNotFoundException的不可用捕获块。永远不会从try语句主体中抛出此异常

您的catch块是问题所在:

catch(ClassNotFoundException e){
        e.printStackTrace();
}
ClassNotFoundException是一个已检查的异常,不会被try块中的代码抛出-请参阅this page

10-07 19:23
查看更多