标签: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 tor out setattr ++ ati value val mys
原文地址:https://www.cnblogs.com/xiaosuye/p/9657733.html