当我尝试点击我的jsp文件中的按钮时,我的许多servlet之一出现了一个奇怪的错误。关于HTTP状态404和Java Servlet的其他帖子似乎与我遇到的任何内容都没有关系。
HTTP状态404 - 未找到
类型状态报告
消息 / ProjectName / pages / LogoutServlet
说明原始服务器未找到目标资源的当前表示形式,或者不愿透露该目标资源是否存在。
注意:点击按钮后,控制台中没有出现警告或异常。
这是一个在Apache Tomcat v9.0上运行的动态Web项目。
JSP文件(名称: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>
上面JSP底部的表单给出了错误。
Java Servlet(名称: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);
}
}
请注意,我使用 @WebServlet 注释声明servlet,而不是在web.xml文件中添加servlet和servlet映射。我在这个应用程序中有大约20个其他servlet具有类似的设置。这个servlet是我遇到这个问题的唯一一个。
当我在调试模式下运行时,应用程序永远不会到达LogoutServlet。
Java Servlet(名称:LoginServlet.java) *这是我的servlet,引导用户访问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文件(名称: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>