实验四 JSP内置对象的应用

@TOC


前言

vbscript 复制代码
一、实验目的:
1.加深内置对象request,response进行请求响应的处理的理解。
2.掌握使用内置对象session,application实现会话跟踪。
3.掌握JSP的的指令元素,脚本元素,动作元素的使用
二、内容和主要步骤:
1.创建6个jsp页面,使用session实现一个简单的注册登录的示例。
2.页面命名如下:reg.jsp, reg-check.jsp, login.jsp, login-check.jsp, success.jsp, failure.jsp。
3.reg.jsp为注册页面,让用户输入用户名和密码,提交到reg-check.jsp
4.reg-check.jsp为注册信息存储页面,将用户的注册信息存储到session对象中。并添加到login.jsp页面的链接。
5.login.jsp为登录页面,让用户输入用户名和密码,提交到login-check.jsp
6.login-check.jsp为登录检查页面,使用request对象获取login.jsp中的用户名和密码,取出注册时session中存储的用户名和密码进行比较,当全部相同时,使用response的sendRedirect()方法转到success.jsp, 否则转到failure.jsp。
7.success.jsp显示登录成功文字。
failure.jsp显示登录失败文字。

三、要求:
1. 完成上述任务,编写完整代码调试并使之运行正确;
2. 写出实验报告(在实验系统完成)

具体操作

reg.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>reg</title>
</head>
<body>
<%--注册页面--%>
<form action="reg-check.jsp" method="post">
    用户名:<input type="text" name="username">
    <br>
    密码:<input type="password" name="password">
    <br>
    <input type="submit" value="注册">
</form>
</body>
</html>

reg-check.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>reg-check</title>
</head>
<body>
<%--存储信息--%>
<%
    //存储数据
    session.setAttribute("username", request.getParameter("username"));
    session.setAttribute("password", request.getParameter("password"));
    //重定向到login.jsp
    response.sendRedirect("login.jsp");
%>
</body>
</html>

login.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login</title>
</head>
<body>
<%--登录页面--%>
<form action="login-check.jsp" method="post">
    用户名:<input type="text" name="username">
    <br>
    密码:<input type="password" name="password">
    <br>
    <input type="submit" value="登录">
</form>
</body>
</html>

login-check.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login-check</title>
</head>
<body>
<%
    if ( request.getParameter("username").equals(session.getAttribute("username"))&&
            request.getParameter("password").equals(session.getAttribute("password"))) {
        //重定向到success.jsp
        response.sendRedirect("success.jsp");
    }else {
        //重定向到failure.jsp
        response.sendRedirect("failure.jsp");
    }
%>
</body>
</html>

success.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>

failure.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>failure</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>

运行截图 用户名和密码其中一个与注册时不相等,则跳转到失败页面


总结

vbscript 复制代码
1.加深内置对象request,response进行请求响应的处理的理解。
2.掌握使用内置对象session,application实现会话跟踪。
3.掌握JSP的的指令元素,脚本元素,动作元素的使用。

相关推荐
木易 士心21 分钟前
Spring Boot 微信公众号后端服务开发实战指南
spring boot·后端·微信
BingoGo37 分钟前
别再只用数组了,这 10 种 PHP 数据结构值得了解
后端·php
JaguarJack1 小时前
别再只用数组了,这 10 种 PHP 数据结构值得了解
后端·php·服务端
程序员海军9 小时前
用了两年多 GPT,我最近开始换模型了,还折腾了个求职避坑 Agent
前端·人工智能·后端
码士集团小青9 小时前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
excel11 小时前
IndexNow:让搜索引擎更快发现网站更新的主动通知协议
前端·后端·架构
Json____11 小时前
用 FastAPI + Vue3 打造一套完整的在线考试系统:从组卷到阅卷的业务闭环实践
后端·node.js·fastapi·it学习·wwwoop.com
小灰灰搞电子12 小时前
Rust+Slint 实现FTP客户端调试助手源码分享
开发语言·后端·rust
专业程序开发源13 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
卷无止境13 小时前
面向arm64桌面客户端开发全景:语言、框架与生态
后端·python