I am getting a strange error with one of my many servlets when I try to click on a button from my jsp file. None of the other post regarding HTTP Status 404 and Java Servlets seem to having any relevance with what I am encountering.
HTTP Status 404 - Not Found
Type Status Report
Message /ProjectName/pages/LogoutServlet
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Note: No warnings or exceptions are appearing in the console when I click on the button.
This is a Dynamic Web Project being run on Apache Tomcat v9.0.
JSP File (Name: loginSuccess.jsp)
<body>
<%
String user = null;
if(session.getAttribute("user") == null){
response.sendRedirect("pages/index.jsp");
} else {
user = (String) session.getAttribute("user");
}
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null) {
for(Cookie cookie : cookies){
if(cookie.getName().equals("user")) {
userName = cookie.getValue();
}
if(cookie.getName().equals("JSESSIONID")) {
sessionID = cookie.getValue();
}
}
} else {
sessionID = session.getId();
}
%>
<h3>Hi <%=userName %>, Login successful. Your Session ID=<%=sessionID %></h3>
<br>
<form action="LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
</body>
The form at the bottom of the JSP above is the giving me the error.
Java Servlet (Name: LogoutServlet.java)
package com.main;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("JSESSIONID")){
System.out.println("JSESSIONID="+cookie.getValue());
}
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
//invalidate the session if exists
HttpSession session = request.getSession(false);
System.out.println("User="+session.getAttribute("user"));
if(session != null){
session.invalidate();
}
response.sendRedirect("pages/index.jsp");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
Please note that i am declaring the servlet with the @WebServlet annotation instead of adding the servlet and the servlet mapping in the web.xml file. I have around 20 other servlets in this application with a similar setup. This servlet is the only one that I am having this issue with.
The application never reaches the LogoutServlet when I run in Debug mode.
Java Servlet (Name: LoginServlet.java) *This is my servlet that leads the user to the loginSuccess.jsp
package com.main;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con = null;
boolean validated = false;
response.setContentType("text/html");
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/?autoReconnect=true&useSSL=false&user=root";
Properties dbInfo = new Properties();
dbInfo.put("user", "root");
dbInfo.put("password", "root");
con = DriverManager.getConnection(url, dbInfo);
String sql = "SELECT uname, pword FROM bankofme.accounts";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
if(user.equals(rs.getString("uname")) && pwd.equals(rs.getString("pword"))) {
validated = true;
}
}
rs.close();
} catch(Exception e) {
e.printStackTrace();
}
if(validated) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
session.setMaxInactiveInterval(1800);
Cookie userName = new Cookie("user", user);
userName.setMaxAge(1800);
response.addCookie(userName);
String encodedURL = response.encodeRedirectUrl("pages/loginSuccess.jsp");
response.sendRedirect(encodedURL);
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("pages/index.jsp");
PrintWriter out= response.getWriter();
out.println("<font color=red>Either user name or password is wrong.</font>");
rd.include(request, response);
out.close();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
JSP File (Name: index.jsp)
<body>
<div id="header">
<form action="LoginServlet" method="post">
Name: <input type="text" name="user"><br>
Password: <input type="password" name="pwd"><br>
<input type="submit" value="login">
</form>
</div><!-- end of header -->
</body>