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

session技术与cookie技术的区别:
这里写图片描述
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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值