Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何接收2个或更多不同的异常?_Java_Sql_Jsp_Servlets_Exception Handling - Fatal编程技术网

Java 如何接收2个或更多不同的异常?

Java 如何接收2个或更多不同的异常?,java,sql,jsp,servlets,exception-handling,Java,Sql,Jsp,Servlets,Exception Handling,我已经编写了一个代码,将用户添加到数据库中。当我们收到重复的条目时,我需要重定向到EmpInfo.jsp。我需要使用更多的异常,而且我想知道如何重定向 response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String

我已经编写了一个代码,将用户添加到数据库中。当我们收到重复的条目时,我需要重定向到EmpInfo.jsp。我需要使用更多的异常,而且我想知道如何重定向

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "cervlet";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
String password = "1234";
int Empid =Integer.parseInt(request.getParameter("Empid").toString()); 
String Name = request.getParameter("Name").toString();
int Age =Integer.parseInt(request.getParameter("Age").toString()); 
int Salary =Integer.parseInt(request.getParameter("Salary").toString());
PreparedStatement stmt;
try {
 Class.forName(driver).newInstance();
 conn = DriverManager.getConnection(url+dbName,userName,password);
 System.out.println("Connected to the database");
 //ArrayList al=null;
 //ArrayList userList =new ArrayList();
 String query = "insert into employee set Empid='"+Empid+"',name='"+Name+"',Age='"+Age+"',Salary='"+Salary+"'";
stmt = (PreparedStatement) conn.prepareStatement(query);
  int i = 0;
try {
   i = stmt.executeUpdate(query);
    }
  catch (SQLException e) {
    String nextj = "/AddUser.jsp";
    RequestDispatcher rd = getServletContext().getRequestDispatcher(nextj);
   rd.forward(request, response);
    }
   System.out.println("i="+i);
System.out.println("query: " + query);
//if(i==0)
 //{
  //String nextj = "/EmpInfo.jsp";
    //RequestDispatcher cd = getServletContext().getRequestDispatcher(nextj);
    //cd.forward(request, response);
 //response.sendRedirect("servletRecord");
//}
 response.sendRedirect("/EmpInfo.jsp");
 conn.close();
 System.out.println("Disconnected from database");

} catch (Exception e) {
e.printStackTrace();
更新

当它添加一个新条目时,我需要重定向到
EmpInfo.jsp
。 在执行方法
executeupatte()
之后,我使用了它的返回值 值重定向到另一个名为
EmpInfo.jsp的页面。

但这不是重定向。我正在使用eclipse。告诉我对
.jsp
页面进行多次重定向的常用方法。

好吧,如果您询问如何捕获不同的异常,下面的代码是如何执行的:

try {
    ...
    // Code that may throw a few types of exceptions
    ...
} catch(FileNotFoundException fnfe) {
    // handle very specific exceptions
} catch (IOException ioe) {
    // handle less specific exceptions
} catch (Exception e) {
    // handle the most generic exception case
} 

这将允许您在一个代码块中处理多个异常类型。

处理多个异常已得到响应

关于重定向到其他页面:您可以使用(正如您已经使用的)转发方法(若其他页面在同一个域中)和发送重定向方法(其他页面可以在其他域中)。但您需要注意的是,不会将任何内容写入浏览器,否则在转发/重定向时会出现异常

在您的情况下,您已经以以下语句的形式写入浏览器:

   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
查看您的代码,似乎您甚至不需要这些语句,因为在任何情况下,您都应该转发到其他页面


注意:您在一个位置使用forward,在一个位置使用sendRedirect。希望您能意识到这两者之间的区别。在我看来,您需要在两个位置都使用转发方法。

与问题无关:转发/重定向失败的可能性很大,因为您更改了响应头并在代码开头获得了响应编写器。不要那样做。在这种特殊情况下,绝对没有必要这样做。拆下那些线。另外,您应该避免使用
PreparedStatement
进行SQL注入,并关闭
finally
中的JDBC资源。您应该在另一个问题中询问重定向。您是否阅读了我在问题更新前2小时发布的评论?:)