-1

I am trying to create a login servlet but keep receiving this error? I am new to programming so don't quite understand what this means. I have looked up this error but cant see what I am doing wrong? Any help appreciated. This is the error

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

Exception

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed 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)

This is my code

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);
 }
}
Qwerty
  • 578
  • 1
  • 5
  • 31
Moladhh
  • 7
  • 5
  • Check this link https://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-sendredirect-create-session – soorapadman Mar 07 '19 at 12:04
  • Can you post your `JSP` code? That you are using? And what method are you using? `POST` or `GET` –  Mar 07 '19 at 12:07
  • Check this, https://stackoverflow.com/questions/26352632/how-to-fix-cannot-call-sendredirect-after-the-response-has-been-committed –  Mar 07 '19 at 12:10
  • Are you sure you're getting only one DB record from your query? Change `while ((rs.next())) {` to `if ((rs.next())) {` to prevent multiple redirects. – Jozef Chocholacek Mar 07 '19 at 12:12
  • 1
    Side note: never EVER use string concatenation to create SQL commands from user input, not even in a school project. This makes your code vulnerable to [SQL Injection](https://www.w3schools.com/sql/sql_injection.asp). Use [PreparedStatement](https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html) instead. – Jozef Chocholacek Mar 07 '19 at 12:12
  • I am using a post method – Moladhh Mar 07 '19 at 12:14
  • @Moladhh So why do you code in `processRequest`. Put your code under `doPost` method and replace `while` with `if` as @Jozef said –  Mar 08 '19 at 02:58

1 Answers1

0

use request distpatcher instead of sendRedirect

RequestDispatcher dispatcher = req.getRequestDispatcher("/userprofile.jsp");
                dispatcher.forward(req, resp);
sagar226
  • 5
  • 1
  • 6