Issue
I am trying to implement a simple login page example in JSP and servlet. Following is my servlet code;
package servlet;
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 javax.servlet.http.HttpSession;
import model.UserBean;
/**
* Servlet implementation class LoginController
*/
@WebServlet("/login")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(request, response);
System.out.println("in do post");
String page = "";
try {
UserBean user = new UserBean();
user.setUserName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
System.out.println(user.getUsername());
System.out.println(user.getPassword());
if (user.getUsername().equalsIgnoreCase("jsp") && user.getPassword().equalsIgnoreCase("1234")) {
// HttpSession session = request.getSession(true);
// session.setAttribute("currentSessionUser", user);
page = "cashtransactions.jsp";
} else {
page = "invalidLogin.jsp"; // error page
}
} catch (Exception e) {
System.out.println(e);
}
response.sendRedirect(page);
}
}
and following is my JSP code;
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome to Bank</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
</head>
<body>
<div class="container col-md-4 col-md-offset-3 align-middle"
style="overflow: auto; margin-top: 100px;">
<h2>Bank</h2>
<form action="<%=request.getContextPath()%>/login" method="post">
<div class="form-group">
<label for="uname">User Name:</label> <input type="text"
class="form-control" id="username" placeholder="User Name"
name="username" required>
</div>
<div class="form-group">
<label for="uname">Password:</label> <input type="password"
class="form-control" id="password" placeholder="Password"
name="password" required>
</div>
<button type="submit" value="Login" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
I am getting the following error;
INFO: Server startup in 653 ms
in do post
jsp
1234
Aug 24, 2020 9:39:52 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [servlet.LoginController] in context with path [/BankApplication] threw exception
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
at servlet.LoginController.doPost(LoginController.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I am able to retrieve the value from the username and password. I checked this link for reference but still no luck. What am I missing here?
Appreciate the help!
Solution
As the error tells you, you can not call sendRedirect() after the response has been committed.
The right way of displaying a jsp page via a servlet is the following :
this.getServletContext().getRequestDispatcher("your_page_url").forward( request, response );
Also remove this part :
super.doPost( request, response );
Answered By - Big Zed
Answer Checked By - Gilberto Lyons (JavaFixing Admin)