Servle之Cookie

Posted

tags:

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

技术分享

技术分享

技术分享

技术分享

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
       "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录网页</title>
</head>
    <body>
        <form action="login.do" method="post">
            名称:<input type="text" name="user"><br>
            密码:<input type="password" name="passwd"><br>
            自动登录:<input type="checkbox" name="login" value="auto"><br>
            <input type="submit" value="发送">
        </form>
    </body>
</html>

 

 

 

 

package cc.openhome;

import java.io.*;

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;
//直接访问index.do  如果cookie中的确有信息  则可以直接登录
@WebServlet("/index.do")
public class Index extends HttpServlet {
    protected void processRequest(HttpServletRequest request,
                                HttpServletResponse response)
                       throws ServletException, IOException {
     
        Cookie[]cookies=request.getCookies();
        if(cookies!=null)
        {
            for(Cookie cookie:cookies)
            {
                String name=cookie.getName();
                String value=cookie.getValue();
                
                if("user".equals(name)&&"caterpillar".equals(value))
                {
                    request.setAttribute(name, value);
                    request.getRequestDispatcher("/user.view").forward(request, response);
                    return;
                }
            }
        }

        response.sendRedirect("login.html");
    }

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
                             throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
                            throws ServletException, IOException {
        processRequest(request, response);
    }
}

 

 

 


import java.io.IOException;
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;
//第一次登录  如果有使用登录会保存cookie
@WebServlet("/login.do")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request,
                         HttpServletResponse response)
                      throws ServletException, IOException {
        String user = request.getParameter("user");
        String passwd = request.getParameter("passwd");
        if("caterpillar".equals(user) && "123456".equals(passwd)) {
            String login = request.getParameter("login");
         
            if("auto".equals(login))
            {
            	Cookie cookie=new Cookie("user", "caterpillar");
            	cookie.setMaxAge(7*24*60*60);
            	response.addCookie(cookie);
            }
            request.setAttribute("user", user);
            request.getRequestDispatcher("user.view")
                    .forward(request, response);
        }
        else {
            response.sendRedirect("login.html");
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	doPost(req, resp);
    }
} 

  

package cc.openhome;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//这里先不使用jsp @WebServlet("/user.view") public class User extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); if(request.getAttribute("user") == null) { response.sendRedirect("login.html"); } PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.01" + " Transitional//EN‘>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet User</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>" + request.getAttribute("user") + "已登录</h1>"); out.println("</body>"); out.println("</html>"); out.close(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }

  

 当然这里的账号密码是事先设计好的 实际开发中应该学会变通 从数据库读取信息


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

js代码片段: utils/lcoalStorage/cookie

会话技术Cookie技术 案例:访问时间

会话-cookie

cookie的使用

Servlet设置Cookie

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段