cookie登录

cookie登录_第1张图片

 

cookie登录_第2张图片

cookie登录_第3张图片

cookie登录_第4张图片

cookie登录_第5张图片

cookie登录_第6张图片

具体登录实现代码:

cookie登录_第7张图片

前端代码:

Insert title here



 

 

后台代码:

loginservlet:

package denglu.cookie;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.URLEncoder;

 

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;

@WebServlet("/logincookie")

public class loginservlet extends HttpServlet{

 

    /**

     *

     */

    private static final long serialVersionUID = 1L;

    @Override

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO Auto-generated method stub

        req.setCharacterEncoding("UTF-8");

        String username=req.getParameter("username");

        //System.out.println(name);

        //String password=req.getParameter("userpad");

        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter out=resp.getWriter();

        System.out.println(URLEncoder.encode(username,"UTF-8"));

        Cookie cook=new Cookie("username", URLEncoder.encode(username,"UTF-8"));

        resp.addCookie(cook);

        

        

        

        out.print("欢迎:'"+ username +"'使用
");

        out.print("您有邮件");

        

    }

}

 

 

chaxunservlet:

package denglu.cookie;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.URLDecoder;

import java.net.URLEncoder;

 

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;

@WebServlet("/chaxuncookie")

public class chaxunservlet extends HttpServlet{

 

    /**

     *

     */

    private static final long serialVersionUID = 1L;

    @Override

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO Auto-generated method stub

        req.setCharacterEncoding("UTF-8");

        String username=null;

        resp.setContentType("text/html;charset=UTF-8");

        Cookie[] cookies=req.getCookies();

        for (Cookie cook : cookies) {

            if("username".equals(cook.getName()))

            {

                String name=cook.getValue();

                username=URLDecoder.decode(name,"UTF-8");

                System.out.println(username);

            }

        }

        PrintWriter out=resp.getWriter();

        out.print("欢迎:'"+ username +"'使用
");

        out.print("第一封邮件
");

        out.print("第二封邮件
");

        out.print("第三封邮件
");

    }

}

 

 

contentservlet:

package denglu.cookie;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.URLDecoder;

import java.net.URLEncoder;

 

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;

@WebServlet("/contentcookie")

public class contentservlet extends HttpServlet{

 

    /**

     *

     */

    private static final long serialVersionUID = 1L;

    @Override

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO Auto-generated method stub

        req.setCharacterEncoding("UTF-8");

        String username=null;

        resp.setContentType("text/html;charset=UTF-8");

        Cookie[] cookie=req.getCookies();

        for (Cookie cook : cookie) {

            

            if("username".equals(cook.getName()))

            {

                String value=cook.getValue();

                username=URLDecoder.decode(value,"UTF-8");

                System.out.println(username);

            }

        }

        PrintWriter out=resp.getWriter();

        out.print("欢迎:'"+ username +"'使用
");

        out.print("所谓的焦虑就是书读得太少,而想得又太多了");

    }

}

cookie登录_第8张图片

cookie登录_第9张图片

cookie登录_第10张图片

 

 

你可能感兴趣的:(cookie登录)