cookie的使用

Posted ihatemilk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie的使用相关的知识,希望对你有一定的参考价值。

技术图片

 

package cookie;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class LoginCookie
 */
@WebServlet("/LoginCookie")
public class LoginCookie extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * Default constructor.
     */
    public LoginCookie() {
        // 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.setContentType("text/html;charset =utf-8" );
        PrintWriter out = response.getWriter();//创建输出流
        String UersName = request.getParameter("UersName");//获取输入的信息
        String Pswd = request.getParameter("PassWrd");
        String getNull = request.getParameter("checkBox");
        Cookie[] cookies = request.getCookies();
        if(cookies!=null)
        {
            for (Cookie cookie : cookies) {//遍历cookie
                String cookname = cookie.getName();
                if(cookname.equals("UersName"))
                {
                    UersName = cookie.getValue();
                     
                }
                if(cookname.equals("Pswd"))
                {
                    Pswd = cookie.getValue();
                }
                 
            }
            //response.sendRedirect("NewFile1.html");
        }
         
        if(UersName.equals("tom")&&Pswd.equals("123")&&getNull!=null)//判断是否正确和是否勾选自动登陆
        {
            Cookie cookie= new Cookie("UersName", UersName);//建立cookie
            Cookie cookie2 = new  Cookie("Pswd",Pswd);
            cookie.setMaxAge(2592000);//设置cookie的生存时间
            cookie2.setMaxAge(2592000);
            response.addCookie(cookie);
            response.addCookie(cookie2);//将建立的cookie存入
            response.sendRedirect("NewFile1.html");
        }
        else if(UersName.equals("tom")&&Pswd.equals("123")){
            //out.println("Weclome:"+ UersName);
            response.sendRedirect("NewFile1.html");
        }
        else {
            out.println("用户名或者密码错误");
            response.sendRedirect("NewFile.html");
        }
         
    }
 
    /**
     * @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 cookie;
 
import java.io.IOException;
import java.net.URLDecoder;
 
import javax.activation.URLDataSource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/**
 * Servlet Filter implementation class Fiter
 */
@WebFilter("/*")
public class Fiter implements Filter {
 
    /**
     * Default constructor.
     */
    public Fiter() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here
 
        // pass the request along the filter chain
        //将请求转化为httpservletRequest请求
        HttpServletRequest  httpServletRequest = (HttpServletRequest) request;
        //将请求转化为httpservletResponse请求
        HttpServletResponse httpServletResponse  =( HttpServletResponse) response;
        //截获用户请求的地址
        String from = httpServletRequest.getServletPath();
         
         if(from.equals("/NewFile.html"))
        {
            Cookie[] cookies = httpServletRequest.getCookies();
            //String uersname= null;
        //  String pswd = null;
            if(cookies!=null)
            {
                /*for (Cookie cookie : cookies) {//遍历cookie
                    if(cookie.getName().equals("UersName"))
                    {
                        uersname =URLDecoder.decode(cookie.getValue(),"UTF-8");
                    }
                    else if (cookie.getName().equals("Pswd"))
                    {
                        pswd =URLDecoder.decode(cookie.getValue(),"utf-8") ;
                    }
                }*/
                httpServletResponse.sendRedirect("LoginCookie");
            }
            //httpServletRequest.setAttribute("uersName", uersname);
            //httpServletRequest.setAttribute("password", pswd);
            chain.doFilter(request, response);//放行
        }
         else {
            chain.doFilter(request, response); 
            }
         
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
 
}

 

以上是关于cookie的使用的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript单行代码,也就是代码片段

XSS:如何从 C# 中的字符串中删除 JS 片段?

Django cookie 横幅:window.wpcc 未定义

根据 ASP.NET 中的角色设置身份验证 cookie 超时长度

最详细的cookie和浏览隐私之间的关系

c#如何采集需要登录的页面