控制台上的servlet错误不会重定向到错误页面

时间:2015-01-06 01:44:42

标签: java mysql jsp servlets redirect

当我尝试输入重复的条目时遇到问题,您可以在下面的servlet中看到。问题是tomcat显示重复错误,但servlet没有重定向到所需的错误页面(dup_organism.jsp)。但是,当我输入新记录并重定向到另一个页面时,我没有问题,而代码几乎相同。 我有这个servlet:

package package_ergasia;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class AddOrganism extends HttpServlet 
{
@Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
   {
    response.setContentType("text/html");    
    Connection connection= null;    
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "ergasia";    
    String user = "root";
    String password = "password";
    String org_id = request.getParameter("id");  
    String oname = request.getParameter("oname");    


        try {   
            connection = DriverManager.getConnection(url + dbName, user, password);


            Statement statement = connection.createStatement() ;
            ResultSet resultset = statement.executeQuery("SELECT * FROM organism") ;

            while(resultset.next()){

                if(resultset.getString("org_id").equalsIgnoreCase(org_id)){
                    String contextPath= "http://localhost:8084/secured";
                    response.sendRedirect(response.encodeRedirectURL(contextPath + "/dup_organism.jsp"));
                }


                    else{
                        PreparedStatement ps = connection.prepareStatement("INSERT INTO organism (org_id,organismName) VALUES (?,?)");
                        ps.setString(1, org_id);
                        ps.setString(2, oname);                     
                        ps.executeUpdate(); 
                        String contextPath= "http://localhost:8084/secured";
                        response.sendRedirect(response.encodeRedirectURL(contextPath + "/all_organisms.jsp"));
                    }
            }

        } catch (SQLException e) {
            e.printStackTrace();        
        }
    }
    @Override
    public String getServletInfo() {
        return "info";
    }
}  

这是错误页面dup_organism.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
        pageEncoding="ISO-8859-1"%>  
<html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <link rel="stylesheet" type="text/css" href="../CSS/mystyle.css">
    <title>Duplicate entry</title>  
    </head>  
<body>
          <h1>Error</h1>
          <% String org_id = request.getParameter("org_id"); %>

            <h2><div align="center">
            <br><br><br>
            Duplicate data <br> @Organism Id: 
            <% out.println(org_id); %>
            </div></h2>      
</body> 
</html>  

浏览器不显示任何内容(仍在servlet页面上),tomcat显示以下错误:com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:重复条目&#39; 02&#39;关键&#39; PRIMARY&#39;

1 个答案:

答案 0 :(得分:0)

您获取null的原因是您使用response.sendRedirect创建了一个新的请求和响应对象实例。您可以执行以下任一操作:

  1. 将其替换为request.getRequestDispatcher(url);
  2. 在会话对象中设置Org_id并在jsp中检索它。