Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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 重定向到不同页面的条件?_Java_Servlets - Fatal编程技术网

Java 重定向到不同页面的条件?

Java 重定向到不同页面的条件?,java,servlets,Java,Servlets,我写了一个servlet,其目的是只有在查询执行时才登录应用程序…现在,无效用户名和id的条件是什么…我无法编写条件..请帮助我…servlet是 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try{

我写了一个servlet,其目的是只有在查询执行时才登录应用程序…现在,无效用户名和id的条件是什么…我无法编写条件..请帮助我…servlet是

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl ","scott","tiger");
            System.out.println("cnnection est");
        int Id = Integer.parseInt(request.getParameter("id"));
        String Name=request.getParameter("firstname");
        boolean b=true;

        //Connection con =JdbcConnectionUtil.getConnection();

        PreparedStatement pst = con.prepareStatement("select * from login where id=? and firstname=?");
        pst.setInt(1, Id);
        pst.setString(2, Name);

        ResultSet rs = pst.executeQuery();
        if(rs!=null && rs.next())
        {

        //while(rs.next()){
            PrintWriter pw = response.getWriter();
            System.out.println("here");
            pw.println("hello");
            pw.println(rs.getInt(1));
            pw.println(rs.getString(2));
            pw.println(rs.getString(3));

        }
        //}
        else
        {
            RequestDispatcher rd = request.getRequestDispatcher("/LoginFailed.html");

        }
//      


        }
        catch(Exception ex){

            ex.printStackTrace();

        }
    }

我认为使用
rd.forward
可以解决这个问题


首先检查参数是否正确,然后执行逻辑。也不要忘记关闭语句和连接以避免内存泄漏

以下是重构代码:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //get parameters from request
    try {
        String idParam = request.getParameter("id");
        String name = request.getParameter("firstname");

        //check if request contains such parameters
        if (idParam == null || name == null) {
            throw new IllegalArgumentException(
                    "Id and Name parameters must not be null.");
        }

        //try casting idParam to int
        Integer id = null;
        try {
            id = Integer.parseInt(idParam);
        } catch (NumberFormatException nfe) {
            throw nfe;
        }

        PreparedStatement pst = null;
        Connection con = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl ", "scott", "tiger");

            pst = con.prepareStatement(
                    "select * from login where id=? and firstname=?");
            pst.setInt(1, id);
            pst.setString(2, name);

            //check if result returned any data
            ResultSet rs = pst.executeQuery();
            if (!rs.next()) {
                throw new Exception(
                        "No such user for id: " + id + " and name: " + name);
            }

            PrintWriter pw = response.getWriter();
            pw.println("hello");
            pw.println(rs.getInt(1));
            pw.println(rs.getString(2));
            pw.println(rs.getString(3));

        } catch (Exception ex) {
            throw ex;
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException sqle) {
                throw sqle;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        RequestDispatcher rd = request.getRequestDispatcher("/LoginFailed.html");
        rd.forward(request, response);
    }
}

我认为这样做是合适的。

这个条件有效吗??如果(rs!=null&&rs.next())是,则为。它将检查您的结果集
rs
是否为null/空。如果
userid
firstname
不匹配,resultset
rs
将为空,因此该条件将失败,导致else块中的语句被执行。@Anil:the
rs!=空
检查无效。
executeQuery()
方法永远不会返回空结果集。@JBNizet:并非所有情况下都是这样。在这里,这是一种可能性,但在其他情况下,executeQuery可以返回空结果集。例如,使用
CallableStatement
调用SP,它不返回任何数据,而是通过执行
返回来返回语句。@Logan:空结果集是一个在调用next()时返回false的结果集。它与null不同。CallableSTatement从PreparedStatement继承其executeQuery()方法,该方法的javadoc表示:返回:包含查询生成的数据的ResultSet对象;永不失效