jsp中不能提取cookie!?
发布网友
发布时间:2023-07-13 07:37
我来回答
共3个回答
热心网友
时间:2024-10-22 14:37
//这是一个servlet的,你改成JSP的就可以用,你可以参照里面的逻辑去做一下,说不定可以帮你(这道题是记住用户自动登陆)
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
public Login() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
out.println("用户名:"+cookie.getName()+"<br />");
out.println("密码:"+cookie.getValue()+"<br />");
out.println(cookie.getName() + ",您好!欢迎来到登陆页面!!");
}
}else{
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>Login Servlet</TITLE></HEAD>");
out.println(" <BODY><form action='Login' method='post'>");
out.print("<p>用户名:<input name='name' type='text' /></p>" +
"<p>密码:<input name='pwd' type='password' /></p>" +
"<p><input name='jizhu' type='checkbox' />记住我</p>" +
"<p><input name='sub' type='submit' value='登录' /></p>");
out.println("</form> </BODY>");
out.println("</HTML>");
}
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String jizhu = request.getParameter("jizhu");
if(jizhu != null){
Cookie mycookie = new Cookie(name,pwd);
mycookie.setMaxAge(60*60*24*7);
mycookie.setPath("/");
response.addCookie(mycookie);
}
if(name.equals("haha") && pwd.equals("123")){
out.println(name + ",您好!欢迎来到登陆页面!!");
}else{
out.println("登陆失败!!");
}
out.flush();
out.close();
}
public void init() throws ServletException {
}
}
热心网友
时间:2024-10-22 14:37
读取的时候判断一下coocie是否为空,还有在用户退出的时候要保存cookie到本地。
热心网友
时间:2024-10-22 14:38
砸了它!!!