获取java登录servlet的异常错误

问题描述 投票:0回答:1

我正在尝试创建一个登录servlet但仍然收到此错误?我是编程新手,所以不太明白这意味着什么。我查了一下这个错误,但是看不出我做错了什么?任何帮助赞赏。这是错误

Cannot call sendRedirect() after the response has been committed
Description The server encountered an unexpected condition that prevented it 
from fulfilling the request.

例外 java.lang.IllegalStateException:响应提交后无法调用sendRedirect()org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:488)Loginn.processRequest(Loginn.java:52)Loginn.doPost(Loginn .java:71)javax.servlet.http.HttpServlet.service(HttpServlet.java:660)javax.servlet.http.HttpServlet.service(HttpServlet.java:741)org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)

这是我的代码

public class Loginn extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try(PrintWriter out = response.getWriter()) 
    {

       String name = request.getParameter("name");
       String pass = request.getParameter("pass");
       MyDb1 db = new MyDb1();
       Connection con = db.getCon();
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("select uid,name,pass from register where email = '"+name+"' and  pass = '"+pass+"'");

      while ((rs.next())) {

          String uid = rs.getString("uid");

          HttpSession session=request.getSession();  
          session.setAttribute("name",uid);
          response.sendRedirect("http://localhost:8080/Final_Year_Project_5_/userprofile.jsp");  
      } 


 }catch (SQLException ex) {
        Logger.getLogger(Loginn.class.getName()).log(Level.SEVERE, null, ex);
       }
 }
    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse  response)
 throws ServletException, IOException {
    processRequest(request, response);
} 

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  processRequest(request, response);
 }
}
java mysql servlets
1个回答
0
投票

使用请求distpatcher而不是sendRedirect

RequestDispatcher dispatcher = req.getRequestDispatcher("/userprofile.jsp");
                dispatcher.forward(req, resp);
© www.soinside.com 2019 - 2024. All rights reserved.