马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是CookieLoginServlet,想要重定向至welcome.jsp;出来的结果却是404;报错消息说{文.件[/cookie_login.jsp] 未找到}
package com.example.demo1;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo1/cookielogin.jsp")
public class CookieLoginServlet extends HelloServlet{
private String myusername = "admin";
private String mypassword = "123456";
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username.equals(myusername) && password.equals(mypassword)){
System.out.println("equal");
Cookie cookie = new Cookie("name",username);
System.out.println(username + " " + password);
resp.addCookie(cookie);
resp.sendRedirect("/demo1/cookiewelcome.jsp");
}else {
resp.sendRedirect("/demo1/cookielogin.jsp");
}
}
}
下面是cookielogin.jsp代码<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/demo1/cookie_login.jsp" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
还有cookiewelcome.jsp的代码<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Cookie_welcome</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
for (Cookie cookie:cookies){
out.write(cookie.getName() + " : " + cookie.getValue());
}
%>
</body>
</html>
<form action="/demo1/cookie_login.jsp" method="post">
提交表单的路径改为为demo1/cookielogin.jsp
0.前面没有/,
1.你确实没有cookie_login这个文件
|