本文介绍: ◆(1 )改进注册功能,注册成功将用户注册信息保存在application中。(2)改进登录功能,将用户登录信息跟application中存储的进行比对校验,若登录成功进入主页并将账号存入session ,若登录失败回到登录页面。
学习目标:
学习内容:
1、实验
■地点:周三2单元,10617综合一实验室,自带电脑
■目的:掌握各种内置对象的主要使用方法,能使用内置对象完成具体的功能。
■内容:
◆(1 )改进注册功能,注册成功将用户注册信息保存在application中。(2)改进登录功能,将用户登录信息跟application中存储的进行比对校验,若登录成功进入主页并将账号存入session ,若登录失败回到登录页面。
■要求:
实验课前着手实验
>实验课最后半小时组长组织小组演示验收,互评打分,组长将最终成绩发给老师
◆本次写实验报告,上传学习通
实验课上小组互评打分标准:
90-100分:完成实验,运行效果好。
80-89分:基本完成实验或有瑕疵,运行效果– -般。70-79分:实验进行了一半以上。
60-69 :实验进行了不到- -半。
60分以下:还在学习消化,没有开始实验。
学习时间:
- 周一至周五晚上 7 点—晚上9点
- 周六上午 9 点-上午 11 点
- 周日下午 3 点-下午 6 点
学习产出:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<body>
<h2>欢迎来到主页面!</h2>
<%
request.setCharacterEncoding("utf-8");
String username = request.getSession().getAttribute("username").toString();
%>
<h3 style="color: red">您好,<%=username %></h3>
</body>
</html>
login.jsp
<%--
Created by IntelliJ IDEA.
User: baizhimin
Date: 2023/3/23
Time: 22:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>登录页面</h1>
<form action="login_deal.jsp" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<br><br>
<input type="submit" value="登录">
</form>
<br>
<p>没有账户? <a href="register.jsp">点击注册</a></p>
</body>
</html>
login_deal.jsp
<%@ page import="java.util.HashMap" %><%--
Created by IntelliJ IDEA.
User: baizhimin
Date: 2023/3/23
Time: 22:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login_deal</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String username = request.getParameter("username");
String password = request.getParameter("password");
request.getSession().setAttribute("username",username);
HashMap<String,String> userMap = (HashMap<String,String>)application.getAttribute("userMap");
if(userMap==null){
out.print("登录失败! 即将返回登录界面");
response.setHeader("refresh","2;url="+path+"/login.jsp");
}else{
String pwd = userMap.get(username);
if(pwd!=null && pwd.equals(password)){
session.setAttribute("username", username);
out.print("登录成功! 即将返回主页面");
response.setHeader("refresh","2;url="+path+"/index.jsp");
}else{
out.print("登录失败! 即将返回登录界面");
response.setHeader("refresh","2;url="+path+"/login.jsp");
}
}
%>
</body>
</html>
register.jsp
<%--
Created by IntelliJ IDEA.
User: baizhimin
Date: 2023/3/23
Time: 22:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration Page</title>
</head>
<body>
<h1>注册页面</h1>
<form action="register_deal.jsp" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<br><br>
<input type="submit" value="注册" onclick="alert('注册成功')">
</form>
<br>
<p>已有账户? <a href="login.jsp">点击登录</a></p>
</body>
</html>
register_deal.jsp
<%@ page import="java.util.HashMap" %><%--
Created by IntelliJ IDEA.
User: baizhimin
Date: 2023/3/23
Time: 22:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>register_deal</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String username = request.getParameter("username");
String password = request.getParameter("password");
request.getSession().setAttribute("username",username);
HashMap<String,String> userMap = (HashMap<String,String>)application.getAttribute("userMap");
if(userMap==null){
userMap = new HashMap<String,String>();
userMap.put(username,password);
application.setAttribute("userMap",userMap);
out.print("注册成功! 即将返回主页面");
response.setHeader("refresh","2;url="+path+"/index.jsp");
}else{
if(userMap.containsKey(username)){
out.print("账号已存在! 即将返回注册页面");
response.setHeader("refresh","2;url="+path+"/register.jsp");
}else{
userMap.put(username, password);
out.print("注册成功! 即将返回主页面");
response.setHeader("refresh","2;url="+path+"/index.jsp");
}
}
%>
</body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>WebTest5</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<packaging>war</packaging>
<!--servlet-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path> <!--项目访问路径。当前配置的访问是localhost:9090/, 如果配置是/aa,则访问路径为localhost:9090/aa -->
<port>9090</port>
<!-- <url>http://localhost:9090/register.jsp</url>-->
<uriEncoding>UTF-8</uriEncoding><!-- 非必需项 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml(设置起始页)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
原文地址:https://blog.csdn.net/PKUnewlearner/article/details/129768119
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_21626.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
主题授权提示:请在后台主题设置-主题授权-激活主题的正版授权,授权购买:RiTheme官网
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。