[英]JSP - submit a form to servlet
也許這是一個簡單的問題,但是我對JAVA還是陌生的,我不明白為什么我的簡單登錄表單無法正常工作。 這是我的項目結構的屏幕截圖:
在index.jsp中,我具有以下形式:
<form action="/LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label>E-Mail Adresse</label>
<input class="au-input au-input--full" type="email" name="un" placeholder="E-Mail">
</div>
<div class="form-group">
<label>Passwort</label>
<input class="au-input au-input--full" type="password" name="pw" placeholder="Passwort">
</div>
<div class="login-checkbox">
<label>
<input type="checkbox" name="remember">Merken
</label>
<label>
<a href="#">Passwort vergessen?</a>
</label>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Anmelden</button>
</form>
我的LoginServlet.java在src文件夾中看起來像這樣:
package controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/** * Servlet implementation class LoginServlet */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
try {
UserBean user = new UserBean();
user.setUserName(request.getParameter("un"));
user.setPassword(request.getParameter("pw"));
user = UserDAO.login(user);
if (user.isValid()) {
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("/index.jsp"); //logged-in page
} else
response.sendRedirect("/index.jsp"); //error page
}
catch (Throwable theException) {
System.out.println(theException);
}
}
}
如您所見,我有一個帶有action="/LoginServlet"
的表單。 在我的LoginServlet.java中,我正在使用@WebServlet("/LoginServlet")
但是一旦提交表單,就會收到HTTP Status 404 – Not Found消息。
我真的不明白為什么? 有人知道我在做什么錯嗎? 我想念什么? 任何幫助將非常感激。
<form action="LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">
只需更新您的action =“ LoginServlet”刪除/,如果不起作用,請嘗試從窗體中刪除enctype =“ multipart / form-data”
確保WebContent文件夾中的index.jsp不在Web-INF中
<form action="/LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">
您的表單使用method =“ post”,因此您需要在LoginServlet上重寫doPost方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.