jsp实现cookie的应用 登录功能李老师作业8-1 - 小浣熊博客

jsp实现cookie的应用 登录功能李老师作业8-1

发布者: 小浣熊

全网最全的网络资源分享网站

手机扫码查看

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

这篇文章总字数为:5891 字,有 5 张图存于本站服务器

问题已经解决!借鉴请谨慎雷同,最近查的严,尽量改出自己的特色!!!

前言:

这是一次作业,教学视频里有提供代码,没什么好说的,做个记录,方便将来查看。

效果图:

QQ图片20200522205423.pngQQ图片20200522205427.pngQQ图片20200522205430.pngQQ图片20200522205434.pngQQ图片20200522205444.png

完整代码:

login.jsp文件(位置:webroot根目录下)

<%@ page language="java" import="java.util.*,java.net.*" 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>17180074平帅</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>
  <%
       request.setCharacterEncoding("utf-8");
       String username ="";
       String password ="";
        Cookie[] cookies = request.getCookies();
          if(cookies != null && cookies.length>0)
          {
            for(Cookie c:cookies)
            {
                if(c.getName().equals("username"))
                {
                      username = URLDecoder.decode(c.getValue(),"utf-8");
                }
                 if(c.getName().equals("password"))
                {
                     password = URLDecoder.decode(c.getValue(),"utf-8");
                }
        
            }
          
          }
     %>
    <form name="loginForm" action="error.jsp" method="POST">
        <table align="center">
            <caption>
                <h1>用户登录</h1>
            </caption>
            <tr>
                <th></th>
                <th></th>
            </tr>
            <tr>
                <td>账号:</td>
                <td><input type="text" name="username" value="<%=username %>"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password" value="<%=password %>"></td>

            </tr>
            <tr>
                <td colspan="2"><input type="checkbox" checked="checked" name="isUseCookie">记住账号</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="登录"></td>
            </tr>
        </table>
    </form>
    <%=(String)request.getAttribute("usertxt") %>
  </body>
</html>

error.jsp文件(位置:webroot根目录下)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>17180074平帅</title>
</head>
<body>
<% 
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password"); 


if(username.equals(password)){
if(username.equals("")){
   request.setAttribute("usertxt", "输入错误!请重新输入!");
   request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
request.getRequestDispatcher("dologin.jsp").forward(request, response);
}
}
else{
   request.setAttribute("usertxt", "输入错误!请重新输入!");
   request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>

</body>
</html>

dologin.jsp文件(位置:webroot根目录下)

<%@ page language="java" import="java.util.*,java.net.*" 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>17180074平帅</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>
    <h1>登录成功!</h1>

    <%
    request.setCharacterEncoding("utf-8");
    String[] isUseCookies = request.getParameterValues("isUseCookie");
    if(isUseCookies != null && isUseCookies.length>0) {
       String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
       String password = URLEncoder.encode(request.getParameter("password"), "utf-8");
       Cookie usernameCookie = new Cookie("username",username);
       Cookie passwordCookie = new Cookie("password",password);
       usernameCookie.setMaxAge(10*24*3600);
       passwordCookie.setMaxAge(10*24*3600);
       response.addCookie(usernameCookie);
       response.addCookie(passwordCookie);
       }
       else
       {
          Cookie[] cookies = request.getCookies();
          if(cookies != null && cookies.length>0)
          {
            for(Cookie c:cookies)
            {
                if(c.getName().equals("username")||c.getName().equals("password"))
                {
                     c.setMaxAge(0);
                     response.addCookie(c);
                }
        
            }
          
          }
          
       }
       
        
     %>
    <a href="users.jsp" target="_blank">查看用户信息</a>
  </body>
</html>

users.jsp文件(位置:webroot根目录下)

<%@ page language="java" import="java.util.*,java.net.*" 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>17180074平帅</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>
    <h1>用户信息</h1>
    <hr>
    <%
       request.setCharacterEncoding("utf-8");
       String username ="";
       String password ="";
        Cookie[] cookies = request.getCookies();
          if(cookies != null && cookies.length>0)
          {
            for(Cookie c:cookies)
            {
                if(c.getName().equals("username"))
                {
                     username = URLDecoder.decode(c.getValue(),"utf-8");
                }
                 if(c.getName().equals("password"))
                {
                     password = URLDecoder.decode(c.getValue(),"utf-8");
                }
        
            }
          
          }
     %>

    账号:<%=username %><br>
    密码:<%=password %><br>
  </body>
</html>
文件下载

附件:jsp实现cookie的应用登录功能李老师作业8-1

文件大小:18.8k

更新时间:2020-5-19

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有,若为付费资源,请在下载后24小时之内自觉删除,若作商业用途,请到原网站购买,由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!

分享到:
打赏
-版权声明-

阅读时间:  发布于:2020-5-19
文章标题:《jsp实现cookie的应用 登录功能李老师作业8-1》
本文链接:https://www.mua222.cn/153.html
本文编辑: 小浣熊,转载请注明超链接和出处小浣熊博客
收录状态:[百度已收录][360已收录][搜狗已收录]

评论

     快速回复: 支持 感谢 学习 不错 高兴 给力 加油 惊喜
切换注册

登录

忘记密码?

您也可以使用第三方帐号快捷登录

切换登录

注册

jsp实现cookie的应用 登录功能李老师作业8-1

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏