我正在开发一个基于 Tomcat 7 的 Web 应用程序,但遇到了一个我不知道发生了什么以及为什么发生的情况。在下面的 JSP 页面中输入我的登录凭据后:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<div id="form">
<form method=POST name=login_form action=Login>
Usuário: <input type=text name=username> <br/>
Senha: <input type=password name=password> <br/>
<input type="submit" value="Entrar">
<span class="error">${error}</span>
</form>
</div>
</body>
</html>
无论我输入了正确还是错误的凭据,我都会收到以下消息:
HTTP Status 404 - /WebApp/Login
type Status report
message /WebApp/Login
description The requested resource is not available.
Apache Tomcat/7.0.42
servlet 类的方法 doPost 的实现如下:
String username = request.getParameter("username");
String password = request.getParameter("password");
Date data_login = new Date(System.currentTimeMillis());
java.sql.Timestamp nova_data = new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0);
nova_data.setDate(data_login.getDay());
nova_data.setMonth(data_login.getMonth());
nova_data.setYear(data_login.getYear());
nova_data.setHours(data_login.getHours());
nova_data.setMinutes(data_login.getMinutes());
nova_data.setSeconds(nova_data.getSeconds());
DataHelper dhelper = new DataHelper();
try {
System.out.println("Registrando login...");
user temp = dhelper.doProcessaLogin(username, password);
if (temp != null) {
String fname = temp.getFirstName();
String lname = temp.getLastName();
int id = temp.getId();
System.out.println("Dados corretos");
request.getSession().setAttribute("user", fname+" "+lname);
dhelper.doRegistraSessao(fname, lname, nova_data, id);
Cookie ck = new Cookie("User",fname+" "+lname);
ck.setMaxAge(7);
response.addCookie(ck);
request.getRequestDispatcher("/WEB-INF/menu.jsp")
.forward(request,response);
}
else {
System.out.println("Dados incorretos");
request.getSession().setAttribute("erro",
"Não foi possivel efetuar o login");
System.out.println("Direcionando de volta para a pagina de login");
request.getRequestDispatcher("/login").forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我也有一个方法 doGet 的实现:
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
我的 web.xml 如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.webapp.servlet.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我真的不知道系统正在寻找的原因而/WebApp/Login
不是. 有人可以看到上面的代码有什么问题吗?/WebApp/login
web.xml