[英]Java Servlet and Apache Tomcat 404 when redirecting to Servlet
數天來,我一直在努力尋找解決問題的方法。 我正在嘗試使用Tomcat開發Java中的Web應用程序。
這是我的項目結構:
根據我的閱讀,我知道.jsp文件應該放在Web目錄中,但是當我這樣做時,我得到了:
HTTP狀態[404] – [未找到]
如果我的所有.jsp文件都直接放置在“ simpleLogin”內部,則不會發生這種情況。
此外,這是我的index.jsp :
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="post" action="LoginCheck"> <%--Action shows you where to go, we want to go from jsp to a servlet--%>
<table>
<tr>
<td>
UserName:
</td>
<td>
<input type="text" name="username"/>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="text" name="password"/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="login"/></td>
</tr>
</table>
</form>
</body>
</html>
這是我的LoginCheck servlet類:
package example;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
/**
* Created by AGDS on 5/23/2017.
*/
@WebServlet("/LoginCheck")
public class LoginCheck extends HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
//It will check weither username or password is correct
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(System.getProperty("user.dir"));
try {
if (username.equals("java") && password.equals("1234")) {
//Go to member's page
response.sendRedirect("member.jsp");
} else {
//Go to error page
response.sendRedirect("error.jsp");
}
} catch (Exception e) {
}
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
}
這是我的web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>LoginCheck</servlet-name>
<servlet-class>example.LoginCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCheck</servlet-name>
<url-pattern>/LoginCheck</url-pattern>
</servlet-mapping>
</web-app>
現在,如果我嘗試點擊“提交”按鈕,則會再次導致:
HTTP狀態[404] – [未找到]
我想我的路徑和.jsp文件所在的位置有問題,或者我的web.xml中的映射有問題。
任何建議/提議將不勝感激!
無需將html / jsp內容放入名為web的目錄中。 為方便起見,IDE會為您執行此操作。 例如,出於相同的目的,eclipse將創建一個名為WebContent的目錄。
因此,如果要使用Intelij管理項目,請確保正確配置InteliJ以將其作為Java Web應用程序進行處理。
有關詳細信息,請參見: https ://www.jetbrains.com/help/idea/2017.1/creating-and-running-your-first-web-application.html
404錯誤表示文件不存在,或者您未正確提供文件路徑。單擊“提交”按鈕后,URL應該類似於localhost:8085/simpleLogin/LoginCheck
而不是localhost:/LoginCheck
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.