我正在尝试创建登录服务,但我的网页没有正确重定向。我有以下内容:
的login.jsp
<form action="login" method="post">
User Name
<br>
<input type="text" name="userId"/>
<br><br>
Password
<br>
<input type="password" name="password"/>
<br><br>
<input type="submit"/>
</form>
LoginServlet.java
package org.sohail.javabrains;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId, password;
userId=request.getParameter("userId");
password=request.getParameter("password");
LoginService loginService = new LoginService();
boolean result = loginService.authenticate(userId, password);
if (result) {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/success.jsp");
dispatcher.forward(request, response);
return;
}
else {
response.sendRedirect("login.jsp");
return;
}
}
}
LoginService.java - 有一个authenticate(userId,password)方法连接数据库,验证userId并传递并返回一个布尔值。
的web.xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.sohail.javabrains.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
来自login.jsp页面,不管我说的是什么,我得到以下错误:
HTTP状态404 - / LoginApp / login
如果authenticate()reutrns为真,它应该将页面重定向到success.jsp。
我对此很陌生,所以请随时提供任何其他建议。
答案 0 :(得分:0)
感谢Birgit Martinelle的回答:
将web.xml servlet映射更改为
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
并从重定向网址中删除WEB-INF部分:
RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");
- Birgit Martinelle 50分钟前