码迷,mamicode.com
首页 > 其他好文 > 详细

cookie Session

时间:2018-09-16 22:31:01      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:cookie   tor   out   setattr   ++   ati   value   val   mys   

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

标签:cookie   tor   out   setattr   ++   ati   value   val   mys   

原文地址:https://www.cnblogs.com/xiaosuye/p/9657733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!