首页 > 代码库 > Servle之Cookie

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="http://www.mamicode.com/auto"><br>
            <input type="submit" value="http://www.mamicode.com/发送">
        </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