Error; java.lang.IllegalStateException: Cannot create a session after the response has been committed...












0















This question already has an answer here:




  • java.lang.IllegalStateException: Cannot create a session after the response has been committed

    2 answers




I am trying to create a login page for a java web app using MySQL. When I run the code I receive this exception; java.lang.IllegalStateException: Cannot create a session after the response has been committed



    protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
`String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();
Connection con = db.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select uid,email,pass from register where email = '"+email+"' and pass = '"+pass+"'");
if ((rs.next())) {

String uid = rs.getString("uid");
response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");


HttpSession session=request.getSession();
session.setAttribute("name",uid); }

else {
RequestDispatcher rd = request.getRequestDispatcher("/Incorrect.html");
rd.forward(request, response);


}

} catch (SQLException ex) {
Logger.getLogger(Logi.class.getName()).log(Level.SEVERE, null, ex);
}









share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 1 '18 at 9:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
    – Krease
    Nov 19 '18 at 16:39


















0















This question already has an answer here:




  • java.lang.IllegalStateException: Cannot create a session after the response has been committed

    2 answers




I am trying to create a login page for a java web app using MySQL. When I run the code I receive this exception; java.lang.IllegalStateException: Cannot create a session after the response has been committed



    protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
`String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();
Connection con = db.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select uid,email,pass from register where email = '"+email+"' and pass = '"+pass+"'");
if ((rs.next())) {

String uid = rs.getString("uid");
response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");


HttpSession session=request.getSession();
session.setAttribute("name",uid); }

else {
RequestDispatcher rd = request.getRequestDispatcher("/Incorrect.html");
rd.forward(request, response);


}

} catch (SQLException ex) {
Logger.getLogger(Logi.class.getName()).log(Level.SEVERE, null, ex);
}









share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 1 '18 at 9:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
    – Krease
    Nov 19 '18 at 16:39
















0












0








0








This question already has an answer here:




  • java.lang.IllegalStateException: Cannot create a session after the response has been committed

    2 answers




I am trying to create a login page for a java web app using MySQL. When I run the code I receive this exception; java.lang.IllegalStateException: Cannot create a session after the response has been committed



    protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
`String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();
Connection con = db.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select uid,email,pass from register where email = '"+email+"' and pass = '"+pass+"'");
if ((rs.next())) {

String uid = rs.getString("uid");
response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");


HttpSession session=request.getSession();
session.setAttribute("name",uid); }

else {
RequestDispatcher rd = request.getRequestDispatcher("/Incorrect.html");
rd.forward(request, response);


}

} catch (SQLException ex) {
Logger.getLogger(Logi.class.getName()).log(Level.SEVERE, null, ex);
}









share|improve this question














This question already has an answer here:




  • java.lang.IllegalStateException: Cannot create a session after the response has been committed

    2 answers




I am trying to create a login page for a java web app using MySQL. When I run the code I receive this exception; java.lang.IllegalStateException: Cannot create a session after the response has been committed



    protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
`String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();
Connection con = db.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select uid,email,pass from register where email = '"+email+"' and pass = '"+pass+"'");
if ((rs.next())) {

String uid = rs.getString("uid");
response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");


HttpSession session=request.getSession();
session.setAttribute("name",uid); }

else {
RequestDispatcher rd = request.getRequestDispatcher("/Incorrect.html");
rd.forward(request, response);


}

} catch (SQLException ex) {
Logger.getLogger(Logi.class.getName()).log(Level.SEVERE, null, ex);
}




This question already has an answer here:




  • java.lang.IllegalStateException: Cannot create a session after the response has been committed

    2 answers








java mysql jsp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 16:31









Laura CrowleyLaura Crowley

11




11




marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 1 '18 at 9:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 1 '18 at 9:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
    – Krease
    Nov 19 '18 at 16:39
















  • 1




    Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
    – Krease
    Nov 19 '18 at 16:39










1




1




Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
– Krease
Nov 19 '18 at 16:39






Side note - please don’t use String concatenation in your sql queries - it is incredibly unsafe.
– Krease
Nov 19 '18 at 16:39














1 Answer
1






active

oldest

votes


















0














your error is because you are redirecting before you set the session attribute:



 response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");  


HttpSession session=request.getSession();
session.setAttribute("name",uid); }


Whenever you redirect or forward the user to a different page, you need to make sure that you do any actions want to do before you forward or redirect. So just change it like this:



  HttpSession session=request.getSession();  
session.setAttribute("name",uid); }

response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp"); //put this last


Here's your code cleaned up, and with the stuff you don't need taken out (using preparedstatement here because manually building your statement isn't safe as a comment has mentioned already. )



protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();

String url = "Incorrect.html";
try(Connection con = db.getCon()){

PreparedStatement pst = con.prepareStatement("select uid,email,pass from register where email = ? and pass = ?;");
pst.setString(1, email); //set first '?'
pst.setString(2, pass); //set second '?'
ResultSet rs = pst.executeQuery();

while(rs.next()) {

url = "userprofile.jsp"; //override url string

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

HttpSession session=request.getSession();
session.setAttribute("name",uid);

}

} catch (SQLException e) {
e.printStackTrace();
}

RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);

}





share|improve this answer





















  • Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
    – Laura Crowley
    Nov 20 '18 at 15:17










  • It means your servlet doesn't have a doPost method.
    – Jonathan Laliberte
    Nov 20 '18 at 15:37










  • Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
    – Jonathan Laliberte
    Nov 20 '18 at 15:39


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














your error is because you are redirecting before you set the session attribute:



 response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");  


HttpSession session=request.getSession();
session.setAttribute("name",uid); }


Whenever you redirect or forward the user to a different page, you need to make sure that you do any actions want to do before you forward or redirect. So just change it like this:



  HttpSession session=request.getSession();  
session.setAttribute("name",uid); }

response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp"); //put this last


Here's your code cleaned up, and with the stuff you don't need taken out (using preparedstatement here because manually building your statement isn't safe as a comment has mentioned already. )



protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();

String url = "Incorrect.html";
try(Connection con = db.getCon()){

PreparedStatement pst = con.prepareStatement("select uid,email,pass from register where email = ? and pass = ?;");
pst.setString(1, email); //set first '?'
pst.setString(2, pass); //set second '?'
ResultSet rs = pst.executeQuery();

while(rs.next()) {

url = "userprofile.jsp"; //override url string

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

HttpSession session=request.getSession();
session.setAttribute("name",uid);

}

} catch (SQLException e) {
e.printStackTrace();
}

RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);

}





share|improve this answer





















  • Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
    – Laura Crowley
    Nov 20 '18 at 15:17










  • It means your servlet doesn't have a doPost method.
    – Jonathan Laliberte
    Nov 20 '18 at 15:37










  • Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
    – Jonathan Laliberte
    Nov 20 '18 at 15:39
















0














your error is because you are redirecting before you set the session attribute:



 response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");  


HttpSession session=request.getSession();
session.setAttribute("name",uid); }


Whenever you redirect or forward the user to a different page, you need to make sure that you do any actions want to do before you forward or redirect. So just change it like this:



  HttpSession session=request.getSession();  
session.setAttribute("name",uid); }

response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp"); //put this last


Here's your code cleaned up, and with the stuff you don't need taken out (using preparedstatement here because manually building your statement isn't safe as a comment has mentioned already. )



protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();

String url = "Incorrect.html";
try(Connection con = db.getCon()){

PreparedStatement pst = con.prepareStatement("select uid,email,pass from register where email = ? and pass = ?;");
pst.setString(1, email); //set first '?'
pst.setString(2, pass); //set second '?'
ResultSet rs = pst.executeQuery();

while(rs.next()) {

url = "userprofile.jsp"; //override url string

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

HttpSession session=request.getSession();
session.setAttribute("name",uid);

}

} catch (SQLException e) {
e.printStackTrace();
}

RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);

}





share|improve this answer





















  • Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
    – Laura Crowley
    Nov 20 '18 at 15:17










  • It means your servlet doesn't have a doPost method.
    – Jonathan Laliberte
    Nov 20 '18 at 15:37










  • Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
    – Jonathan Laliberte
    Nov 20 '18 at 15:39














0












0








0






your error is because you are redirecting before you set the session attribute:



 response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");  


HttpSession session=request.getSession();
session.setAttribute("name",uid); }


Whenever you redirect or forward the user to a different page, you need to make sure that you do any actions want to do before you forward or redirect. So just change it like this:



  HttpSession session=request.getSession();  
session.setAttribute("name",uid); }

response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp"); //put this last


Here's your code cleaned up, and with the stuff you don't need taken out (using preparedstatement here because manually building your statement isn't safe as a comment has mentioned already. )



protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();

String url = "Incorrect.html";
try(Connection con = db.getCon()){

PreparedStatement pst = con.prepareStatement("select uid,email,pass from register where email = ? and pass = ?;");
pst.setString(1, email); //set first '?'
pst.setString(2, pass); //set second '?'
ResultSet rs = pst.executeQuery();

while(rs.next()) {

url = "userprofile.jsp"; //override url string

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

HttpSession session=request.getSession();
session.setAttribute("name",uid);

}

} catch (SQLException e) {
e.printStackTrace();
}

RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);

}





share|improve this answer












your error is because you are redirecting before you set the session attribute:



 response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp");  


HttpSession session=request.getSession();
session.setAttribute("name",uid); }


Whenever you redirect or forward the user to a different page, you need to make sure that you do any actions want to do before you forward or redirect. So just change it like this:



  HttpSession session=request.getSession();  
session.setAttribute("name",uid); }

response.sendRedirect("http://localhost:8080/FinalYearProjec/userprofile.jsp"); //put this last


Here's your code cleaned up, and with the stuff you don't need taken out (using preparedstatement here because manually building your statement isn't safe as a comment has mentioned already. )



protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();

String url = "Incorrect.html";
try(Connection con = db.getCon()){

PreparedStatement pst = con.prepareStatement("select uid,email,pass from register where email = ? and pass = ?;");
pst.setString(1, email); //set first '?'
pst.setString(2, pass); //set second '?'
ResultSet rs = pst.executeQuery();

while(rs.next()) {

url = "userprofile.jsp"; //override url string

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

HttpSession session=request.getSession();
session.setAttribute("name",uid);

}

} catch (SQLException e) {
e.printStackTrace();
}

RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:36









Jonathan LaliberteJonathan Laliberte

1,6042824




1,6042824












  • Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
    – Laura Crowley
    Nov 20 '18 at 15:17










  • It means your servlet doesn't have a doPost method.
    – Jonathan Laliberte
    Nov 20 '18 at 15:37










  • Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
    – Jonathan Laliberte
    Nov 20 '18 at 15:39


















  • Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
    – Laura Crowley
    Nov 20 '18 at 15:17










  • It means your servlet doesn't have a doPost method.
    – Jonathan Laliberte
    Nov 20 '18 at 15:37










  • Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
    – Jonathan Laliberte
    Nov 20 '18 at 15:39
















Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
– Laura Crowley
Nov 20 '18 at 15:17




Thank you for your help. However when I run that code I receive this error; HTTP Status 405 - HTTP method POST is not supported by this URL. Would you know why this is occurring? Thanks
– Laura Crowley
Nov 20 '18 at 15:17












It means your servlet doesn't have a doPost method.
– Jonathan Laliberte
Nov 20 '18 at 15:37




It means your servlet doesn't have a doPost method.
– Jonathan Laliberte
Nov 20 '18 at 15:37












Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
– Jonathan Laliberte
Nov 20 '18 at 15:39




Create a doPost and call processRequest from your doPost or move this code into a doPost method.. or change the method on your form to a get and move the code to the doGet method
– Jonathan Laliberte
Nov 20 '18 at 15:39



Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith