cookie Session
Posted xiaosuye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie Session相关的知识,希望对你有一定的参考价值。
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Myservlet
*/
public class Myservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Myservlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String username = request.getParameter("username");
String password = request.getParameter("password");
/*
* 使用cookie
* */
// Cookie cook = new Cookie("username", username);
// Cookie cok= new Cookie("password", password);
// System.out.println(username);
// System.out.println(password);
// response.addCookie(cook);
// response.addCookie(cok);
/*
* 使用session
* */
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("password", password);
// response.sendRedirect("Myservlet2");
/*
* 当cookie被禁用时
* */
response.sendRedirect(response.encodeUrl("Myservlet2?"+request.getSession().getId()));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Myservlet2
*/
public class Myservlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Myservlet2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String username = null;
String password = null;
/*
* 使用cookie
* */
// Cookie [] cook = request.getCookies();
// for(int i = 0;i<cook.length;i++) {
// if("username".equals(cook[i].getName())) {
//// System.out.println(username);
// username= cook[i].getValue();
//
// }
// if("password".equals(cook[i].getName())) {
//// System.out.println(password);
// password = cook[i].getValue();
//
// }
// }
/*
* 使用session
* */
username = (String) request.getSession().getAttribute("username");
password = (String) request.getSession().getAttribute("password");
response.getWriter().print( username);
response.getWriter().print( password);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
以上是关于cookie Session的主要内容,如果未能解决你的问题,请参考以下文章