无法从servlet转发到jsp

时间:2018-01-06 05:35:49

标签: jsp servlets

我正在做一个简单的表单验证。我无法转发到jsp页面。请参考下面的代码。 Line-B和Line-C工作正常但Line-A产生错误

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

@WebServlet("/client")
public class client extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public client() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = null;
        String password = null;

        username = request.getParameter("username");
        password = request.getParameter("password");

        SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
        Session s1 = sf1.openSession();
        s1.beginTransaction();
        Query q = s1.createQuery("from userdb where username = ?");
        userdb u2 = (userdb) q.setString(0, username).uniqueResult();
        if (u2 != null) {
            if (u2.password.equals(password)) {
/*Line - A*/    request.getRequestDispatcher("home.jsp").include(request, response);
            } else {
                request.setAttribute("error", "Invalid username/password, please try again");
/*Line - B*/    request.getRequestDispatcher("Clientlog.jsp").include(request, response);
            }
            s1.getTransaction().commit();
            s1.close();
        }

        userdb u1 = new userdb();
        u1.setUsername(username);
        u1.setPassword(password);
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
        s.save(u1);
        s.getTransaction().commit();
        s.close();

/*Line - C*/    request.getRequestDispatcher("home.jsp").include(request, response);    
    }
}

Line-B和Line-C成功转到请求的页面,但是Line-A正在生成此ERROR

1 个答案:

答案 0 :(得分:0)

克服这种情况的一种方法是(因为Line-A和Line-C相同),如果我使用flag=0来检查它是否进入包含Line-A的块内并将标记标记为1 ,并将执行移动到Line-C它正在工作

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

@WebServlet("/client")
public class client extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public client() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int flag = 0;
        String username = null;
        String password = null;

        username = request.getParameter("username");
        password = request.getParameter("password");

        SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
        Session s1 = sf1.openSession();
        s1.beginTransaction();
        Query q = s1.createQuery("from userdb where username = ?");
        userdb u2 = (userdb) q.setString(0, username).uniqueResult();
        if (u2 != null) {
            if (u2.password.equals(password)) {
                flag = 1;
 /*Line - A*/   // request.getRequestDispatcher("home.jsp").include(request, response);
            } else {
                request.setAttribute("error", "Invalid username/password, please try again");
/*Line - B*/    request.getRequestDispatcher("Clientlog.jsp").include(request, response);
            }
            s1.getTransaction().commit();
            s1.close();
        }

        if(flag == 0){
            userdb u1 = new userdb();
            u1.setUsername(username);
            u1.setPassword(password);
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
            Session s = sf.openSession();
            s.beginTransaction();
            s.save(u1);
            s.getTransaction().commit();
            s.close();
        }

/*Line - C*/    request.getRequestDispatcher("home.jsp").include(request, response);    
    }
}