利用JSP内置对象Session和Application实现登录注册信息保存

学习目标:

实验名称: JSP内置对象

■地点:周三2单元,10617综合一实验室,自带电脑

■目的:掌握各种内置对象的主要使用方法,能使用内置对象完成具体的功能。

■内容:

◆(1 )改进注册功能,注册成功将用户注册信息保存在application中。(2)改进登录功能,将用户登录信息跟application中存储的进行比对校验,若登录成功进入主页并将账号存入session ,若登录失败回到登录页面。

■要求:

实验课前着手实验

>实验课最后半小时组长组织小组演示验收,互评打分,组长将最终成绩发给老师

◆本次写实验报告,上传学习通

' 实验课上小组互评打分标准:

90-100分:完成实验,运行效果好。

80-89分:基本完成实验或有瑕疵,运行效果- -般。70-79分:实验进行了一半以上。

60-69 :实验进行了不到- -半。

60分以下:还在学习消化,没有开始实验。


学习内容:

用JSP代码编写sign_in.jsp注册功能,注册成功将用户注册信息保存在application中。编写log_in.jsp登录功能,将用户登录信息跟application中存储的进行比对校验,若登录成功进入index.jsp主页并将账号存入session,若登录失败回到登录页面log_in.jsp。


学习时间:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

学习产出:

例如:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇

代码: 

PageOne.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>注册</title>
</head>
<body>
<h1>注册</h1>
<form method="post" action="sign_in.jsp">
  <p>
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username">
  </p>
  <p>
    <label for="password">密码:  &nbsp</label>
    <input type="password" id="password" name="password">
  </p>
  <p>
    <button type="submit">注册</button>
  </p>
</form>
</body>
</html>

Log_in.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>

<%
  // 获取登录信息@author_Baizhimin
  String username = request.getParameter("username");
  String password = request.getParameter("password");

  // 从 application 中获取保存的用户信息
//  ServletContext application = getServletContext();
  Map<String, String> users = (Map<String, String>) application.getAttribute("users");

  // 检查用户名和密码是否匹配
  if (users != null && users.containsKey(username) && users.get(username).equals(password)) {
    // 登录成功,将账号存入 session,并跳转到主页
   // HttpSession session = request.getSession();
    session.setAttribute("username", username);
    response.sendRedirect("index.jsp");
  } else {
    // 登录失败,返回登录页面
    response.sendRedirect("log_in.jsp");
  }
%>

 sign_in.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>

<%
    // 获取注册信息@author_Baizhimin
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    // 将注册信息保存到 application 中

 //   String servletInfo = getServletInfo();
    Map<String, String> users = (Map<String, String>) application.getAttribute("users");
    if (users == null) {
        users = new HashMap<String, String>();
        application.setAttribute("users", users);
    }
    users.put(username, password);

    // 注册成功后跳转到登录页面
   // response.sendRedirect("log_in.jsp");
   request.getRequestDispatcher("log_in.jsp").forward(request, response);
%>

index.jsp 

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值