servlet方式通过Cookie记住登录时的用户名和密码_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:34:32
原创
2128人浏览过

1.建立web工程

2.创建存放servlet的包

 

3右键包,新建servlet,路径将前面的servlet去掉,只需要doPost和doGet方法

立即学习前端免费学习笔记(深入)”;

编写servlet

CookieServlet.java代码如下:

package test1029;import java.io.IOException;import java.io.PrintWriter;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 CookieServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doPost(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");                String uname=request.getParameter("uname");        String password=request.getParameter("password");        String ck=request.getParameter("ck");                //被选中的状态是on 没有被选中的状态下是null        if("on".equals(ck)){        //构造Cookie对象        //添加到Cookie中        Cookie c=new Cookie("users", uname+"-"+password);                //设置过期时间        c.setMaxAge(600);                //存储        response.addCookie(c);    }    }}
登录后复制

 

配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>test1029</display-name>  <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>CookieServlet</servlet-name>    <servlet-class>test1029.CookieServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CookieServlet</servlet-name>    <url-pattern>/CookieServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>
登录后复制

index.jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%//el表达式String names="";String pwd="";//取出CookieCookie [] c=request.getCookies();for(int i=0;i<c.length;i++){    if(c[i].getName().equals("users")){        //存着数据(用户名+密码)        names=c[i].getValue().split("-")[0];        pwd=c[i].getValue().split("-")[1];                //再一次的存起来(备用)        request.setAttribute("xingming",names);        request.setAttribute("mima", pwd);    }} %><!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>    <form action="CookieServlet" method="post">        用户名:<input type="text" name="uname" id="uname" value="${xingming}"/><br>        密码:<input type="password" name="password" id="password" value="${mima }"/><br>        <input type="checkbox" name="ck">记住用户名和密码<br>        <input type="submit" value="登录">    </form>  </body></html>
登录后复制

效果图:

Dream Machine
Dream Machine

Dream Machine 是由 Luma AI 开发的一款 AI 视频生成工具,可以快速将文本和图像转换为高质量的视频内容。

Dream Machine 165
查看详情 Dream Machine

1、项目运行之后,先进入登录界面,填写用户名和密码,效果如下图

 

2.登录后:

 

 

3.重新访问登录页面,效果如下:

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号