码迷,mamicode.com
首页 > 其他好文 > 详细

Servle之Cookie

时间:2017-02-18 16:46:37      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:port   读取   har   pre   html 4.01   attribute   param   setattr   xtend   

技术分享

技术分享

技术分享

技术分享

<!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

标签:port   读取   har   pre   html 4.01   attribute   param   setattr   xtend   

原文地址:http://www.cnblogs.com/HJL085/p/6413377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!