使用jsp首先的登录小案例,其中使用了cookie技术

使用cookie的好处就是可以长期保存,与session的区别就是:
这里写图片描述

Login.java代码

package com.oracle.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

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

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

        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取参数
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");
        String[] zt = request.getParameterValues("zt");

        //保存Cookie
        if(zt!=null && zt.length>0){

            //实例化Cookie
            Cookie c1 = new Cookie("username",URLEncoder.encode(userName,"utf-8"));
            Cookie c2 = new Cookie("pwd",pwd);

            //设置有效期
            c1.setMaxAge(60*60*24);
            c2.setMaxAge(60*60*24);

            //保存Cookie
            response.addCookie(c1);
            response.addCookie(c2);

        }else{
            //不记录密码
            Cookie[] cs = request.getCookies();
            if(cs!=null && cs.length>0){
                for (Cookie c : cs) {
                    if("username".equals(c.getName())){
                        c.setMaxAge(0);
                        response.addCookie(c);
                    }
                    if("pwd".equals(c.getName())){
                        c.setMaxAge(0);
                        response.addCookie(c);
                    }
                }
            }

        }


        PrintWriter out = response.getWriter();
        out.print("登录成功");

    }



}


index.jsp的代码

<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>

    <%
            String userName = "";
            String pwd = "";
            Cookie[] cs = request.getCookies();
            if(cs!=null && cs.length>0){
                for(Cookie c : cs){
                    if("username".equals(c.getName())){
                        userName = URLDecoder.decode(c.getValue(), "utf-8") ;
                    }

                    if("pwd".equals(c.getName())){
                        pwd = c.getValue();
                    }
                }

            }

     %>

    <form action="login" method="post">
        账号:<input type="text" name="userName" value="<%=userName%>"><br>
        密码:<input type="password" name="pwd" value="<%=pwd%>"><br>
        <input type="checkbox" name="zt" value="1">记住密码<br>
        <input type="submit" value="登录">

    </form>


  </body>
</html>

猜你喜欢

转载自blog.csdn.net/h_snow_1/article/details/78701868