//doGet是页面表单请求的方式为get
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取当前的会话Session
HttpSession session = request.getSession();
//输出查看当前会话id
System.out.println("Demo1"+session.getId());
//新建一个Cookie对象,是以JSESSIONID为Key,session.getId()为Value
Cookie cookie = new Cookie("JSESSIONID", session.getId());
//设置改Cookie的存活路径
cookie.setPath(request.getContextPath());
//设置Cookie的最大的存活时间,意思就是说,如果超过了改时间值,改Cookie就会不存在
cookie.setMaxAge(Integer.MAX_VALUE);
//将用户创建的Cookie存放到response域中,等待访问结束,返回给客户端浏览器
response.addCookie(cookie);
//从request域获取用户的请求参数username对应的值
String param = request.getParameter("usename");
//将获得到username对应的值存放到session域中,key为字符串param,value为变量param
session.setAttribute("param", param);
//向客户端会写数据
response.getOutputStream().write("fangle".getBytes());
}
//doPost是页面表单请求的方式为post
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
希望对楼主有所帮助,谢谢 |