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);
}
}