Servlet_Cookie对象的特点
Servlet_Cookie对象的创建
Servlet_获取Cookie中的数据
Cookie 用来解决HTTP无状态的,当一个用户(这个会话)与服务器建立一次连接时,通过Cookie在http的头部来记录这个会话的内容,当该会话再次请求连接时这个Cookie会在HTTP头部传递给浏览器。?
Cookie实际上是一小段的文本信息。客户端请求服务器,如果服务器需要记录该用户状态,就使用response向客户端浏览器颁发一个Cookie。客户端浏览器会把Cookie保存起来。当浏览器再请求该网站时,浏览器把请求的网址连同该Cookie一同提交给服务器。服务器检查该Cookie,以此来辨认用户状态。服务器还可以根据需要修改Cookie的内容。
?
?
package Cookie04;
/**
* 创建一个Cookie对象
*/
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class CreateCookieServlet01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建Cookie
Cookie cookie = new Cookie("name","xiaoming");
resp.addCookie(cookie);
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
out.println("CreateCookie");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
?
package Cookie04;
/**
* 获取Cookie里面的数据
*/
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetCookieDateServlet02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//在请求中获取Cookie对象
Cookie[] cookies = req.getCookies();
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
for(int i =0;i<cookies.length;i++){
Cookie cookie = cookies[i];
out.println("Name:"+cookie.getName()+"Value:"+cookie.getValue());
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
?
?
?
?
package Cookie04;
/**
* 当客户端浏览器第一次访问servlet时,发出欢迎光临,当访问过以后 发出欢迎回来
*/
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
public class WelcomeServlet03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset:utf-8");
//访问Cookie对象
Cookie[] cookies = req.getCookies();
boolean flag = false; //设置个开关,意思是用来判断是否是第一次访问,如果是true则是第一次访问,要给他设置个cookie
if(cookies != null){
for(Cookie cookie:cookies){
if("first".equals(cookie.getName())){
flag = false;
}
}
}
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
if(flag){
out.println("welcome to back");
}else{
out.println("welcome to first");
Cookie cookie = new Cookie("first","first");
cookie.setMaxAge(60); //设置持久化响应60s
resp.addCookie(cookie);
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
?
?
?
|