index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
JSP Page</title>
</head>
<body>
<center><h1>Fast Learn</h1></center>
<br/><br/>
<form method="POST" action="validate.do">
<center>
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td>Username</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Sign in"></td>
</tr>
</table>
</center>
</form>
</body>
</html>
validate
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class validate extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
List errorMsg=new LinkedList();
errorMsg.clear();
try {
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
if(username.isEmpty()&&password.isEmpty()){
errorMsg.add("Please enter username and password.");
}
else if(username.isEmpty()){
errorMsg.add("Please enter username.");
}
else if(password.isEmpty()){
errorMsg.add("Please enter password.");
}
if(!errorMsg.isEmpty()){
RequestDispatcher view=request.getRequestDispatcher("login.view");
view.forward(request, response);
}
HttpSession session = request.getSession(true);
session.setAttribute("username", username);
RequestDispatcher view=request.getRequestDispatcher("home.view");
view.forward(request, response);
} finally {
errorMsg.add("Please Re-Login.");
RequestDispatcher view=request.getRequestDispatcher("login.view");
view.forward(request, response);
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
login
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
List errorMsg=new LinkedList();
errorMsg=(List)request.getAttribute("errorMsg");
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Login</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><h1>Fast Learn</h1><center>");
out.println("<br/><br/>");
out.println("<form method=\"POST\" action=\"validate.do\">");
out.println("<center>");
out.println("<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\">");
out.println("<tr>");
out.println("<td>Username</td>");
out.println("<td><input type=\"text\" name=\"username\" size=\"30\"></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Password</td>");
out.println("<td><input type=\"password\" name=\"password\" size=\"30\"></td>");
out.println("</tr>");
if(errorMsg!=null)
{
out.println("<tr><td>");
out.println("<font color=\"red\"");
out.println("<p>");
Iterator items=errorMsg.iterator();
while(items.hasNext()){
out.println(items.next());
}
out.println("</p>");
out.println("</font>");
out.println("</td></tr>");
}
out.println("<tr>");
out.println("<td colspan=\"2\"><input type=\"submit\" value=\"Sign in\"></td>");
out.println("</tr>");
out.println("</table>");
out.println("</center>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
home
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class home extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>
Servlet home</title>");
out.println("</head>");
out.println("<body>");
out.println("<h4>Welcome "+session.getAttribute("username")+"</h4>");
out.println("<br/><br/><br/>");
out.println("<a href=\"logout.jsp\"><b>Logout</b></a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
Help me to correct the following errors:
WARNING: StandardWrapperValve[validate]: PWC1406: Servlet.service() for servlet validate threw exception
java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:370)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at Controller.validate.processRequest(validate.java:76)
at Controller.validate.doPost(validate.java:109)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)