以下是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);
}
}
答案 0 :(得分:1)
您发布的错误:
ReadOnlyCollectionBase
的无法访问的catch块。永远不会从try语句主体
您的public class VirtualFloatArray
{
public byte[] ExtensionBytes {get;set;}
public int Length { return ExtensionBytes.Length / sizeof(float); }
public float this[int index]
{
get
{
var result = new float[1]{};
Buffer.BlockCopy(ExtensionBytes, index * sizeof(float), result, 0, sizeof(float));
return result[0];
}
}
}
阻止了问题:
ClassNotFoundException
catch
是一个经过检查的例外情况,catch(ClassNotFoundException e){
e.printStackTrace();
}
阻止内的代码永远不会抛出 - 请参阅this page
答案 1 :(得分:0)
我在doGet方法中删除了catch(ClassNotFoundException e)。它工作正常。
ClassNotFoundException是一个经过检查的异常,它永远不会出现在你的doGet方法代码中。