JavaEE(1)——常用内置对象

版权声明:[email protected] https://blog.csdn.net/zhaoxuyang1997/article/details/82931372

1 实验目的

  1. 掌握如何获得请求参数
  2. 了解如何通过请求对象获得客户信息;
  3. 重点掌握request对象、session对象、application对象的用法;
  4. 重点掌握使用请求对象存储数据请求转发

2 实验原理

2.1 request 对象

request对象封装了:

  • 服务器信息(如端口号、真实路径及访问协议等)。
  • 客户端用户请求信息。

通过request对象可以获取服务器信息客户端请求信息等。

2.2 session 对象

在Web中,session对象有两个含义:

  • 代表一种生命周期。指用户在浏览某个网站时,从进入网站到浏览器关闭这段时间,也就是用户浏览网站所花费的这段时间。
  • 容器性的内置对象。有服务器为用户自动创建,为用户都享,常用来存放session生命周期中的有关信息。

2.3 application 对象

  • application对象保存着Web应用运行期间的全局数据和信息。
  • 从Web应用开始运行,application对象就被创建。
  • 在整个Web应用运行期间可以在任何JSP页面中访问application对象。

如果要保存整个Web应用运行期间都可以访问的数据,可以使用application对象。

3 实验内容与步骤

3.1 表单传参

问题描述:

  • 编写用户表单JSP程序,显示用户表单传递结果。

3.1.1 编写reqForm.jsp

%WEBROOT%/jsp/exp01/content01/reqForm.jsp 的内容如下:

<%-- 
    Document   : reqForm
    Created on : 2018-10-3, 17:51:31
    Author     : zhaoxuyang
--%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>表单页面</title>
    </head>
    <body>
        <form action="reqRecv.jsp" method="post">
            <p>
                <input type="text" name="name" placeholder="请输入名称" />
            </p>
            <p>
                <input type="checkbox" name="hobby" value="骑车" />骑自行车
                <input type="checkbox" name="hobby" value="驾车" />驾驶小汽车
            </p>        
            <p>
                <input type="submit" value="提交" />
                <input type="reset" value="重置" />
            </p>
        </form>
    </body>
</html>

3.1.2 编写reqRecv.jsp

%WEBROOT%/jsp/exp01/content01/reqRecv.jsp 的内容如下:

<%-- 
    Document   : reqRecv
    Created on : 2018-10-3, 17:57:56
    Author     : zhaoxuyang
--%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>request对象测试</title>
    </head> 
    <body>
        <%
            request.setCharacterEncoding("UTF-8");
            String parameterName = null;
            String[] parameterValue = null;
        %>
        <p>使用request.getParameter("name")获取表单所有参数:</p>
        <%
            out.println(request.getParameter("name"));
        %>
        
        <p>使用request.getParameterNames("name")获取表单所有参数:</p>
        <%
            Enumeration en = request.getParameterNames();
            int j = 0;
            while (en.hasMoreElements()) {
                parameterName = (String) en.nextElement();
                parameterValue = request.getParameterValues(parameterName);
                out.println("表单参数名称:" + parameterName + "=");
        %>
        <%
            for (int i = 0; i < parameterValue.length; i++) {
        %>
        <%=parameterValue[i]%>
        <% } %><br/>
        <% } %>
    </body>
</html>

3.1.3 运行测试

  1. 在浏览器地址栏输入以下URL访问之,如 图1 所示:
    http://localhost:8081/Study/jsp/exp01/content01/reqForm.jsp
    图1 - reqForm.jsp
    图1 - reqForm.jsp

  1. 单击提交按钮,弹出图2所示界面:
    图2 - regRecv.jsp
    图2 - regRecv.jsp

3.2 模拟用户登录

问题描述:

  • 模拟一个用户登录动作,不对用户提交信息作登录验证,只要用户名和密码不为空即可登录。
  • 登录时将用户信息保存在session对象中(这样处理只是方便说明session对象的用法,具体开发中必须对登录信息进行数据库验证)。

编写3个JSP程序 login.jsploginCheck.jspmain.jsp,程序功能分别是:

  1. 进行用户登录
  2. 登录信息处理
  3. 登录后工作页面中获取保存在session中的用户信息。

3.2.1 编写login.jsp

%WEBROOT%/jsp/exp01/content02/login.jsp 的内容如下:

<%--
    Document   : login
    Created on : 2018-10-3, 18:28:40
    Author     : zhaoxuyang
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>用户登录界面</title>
    </head>
    <body>
        <form action="loginCheck.jsp" method="post">
            <p>
                账号:<input type="text" name="userName" />
            </p>
            <p>
                密码:<input type="password" name="passWord" />
            </p>
            <p>
                <input type="submit" value="提交" />
                <small>(提交后,用户名将被存入session中)</small>
            </p>
        </form>
    </body>
</html>

3.2.2 编写loginCheck.jsp

%WEBROOT%/jsp/exp01/content02/loginCheck.jsp 的内容如下:

<%--
    Document   : loginCheck
    Created on : 2018-10-3, 18:33:10
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    String userName = request.getParameter("userName");
    String passWord = request.getParameter("passWord");
    if (userName.length() > 0 && passWord.length() > 0) {
        session.setAttribute("uname", userName);
        response.sendRedirect("main.jsp");
    } else {
        response.sendRedirect("login.jsp");
    }
%>

3.2.3 编写main.jsp

%WEBROOT%/jsp/exp01/content02/main.jsp 的内容如下:

<%--
    Document   : main
    Created on : 2018-10-3, 18:35:13
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>系统主界面</title>
    </head>
    <body>
        <%
            String uname = (String) session.getAttribute("uname");
            if (uname != null) {
                out.print("登录成功!欢迎" + uname + "浏览站点!");
            } else {
                response.sendRedirect("login.jsp");
            }
        %>
        <p>上述名称<strong><%=uname%></strong>是从session中取出的</p>
    </body>
</html>

3.2.4 测试运行

在浏览器地址栏输入以下URL访问之,体会session的作用和用法,如 图3图4所示:
http://localhost:8081/Study/jsp/exp01/content02/login.jsp
图3 - login.jsp
图3 - login.jsp


图4 - main.jsp
图4 - main.jsp


3.3 利用application对象实现网站访问计数

3.3.1 编写visitPageCount.jsp

%WEBROOT%/jsp/exp01/content03/visitPageCount.jsp 的内容如下:

<%--
    Document   : visitPageCount
    Created on : 2018-10-3, 18:46:32
    Author     : zhaoxuyang
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>application测试</title>
    </head>
    <body>
        <%
            int count = 0;
            if (application.getAttribute("count") == null) {
                count = count + 1;
                application.setAttribute("count", count);
            } else {
                count = Integer.parseInt(application.getAttribute("count").toString());
                count++;
                application.setAttribute("count", count);
            }
            out.println("您是本系统的第" + count + "个访问者。");
        %>
        <hr>
        <%
            int count2 = 0;
            if (application.getAttribute("count2") == null) {
                count2++;
                application.setAttribute("count2", count2);
            } else {
                count2 = Integer.parseInt(application.getAttribute("count2").toString());
                if(session.isNew()){
                    count2++;
                }   
                application.setAttribute("count2", count2);
            }
            out.println("您是本系统的第" + count2 + "个访问者。");
        %>        
    </body>
</html>

3.3.2 运行测试

在浏览器地址栏输入以下URL访问之:
http://localhost:8081/Study/jsp/exp01/content03/visitPageCount.jsp
运行结果如 图5 所示:
图5 - visitPageCount.jsp
图5 - visitPageCount.jsp


4 思考题

  • 如何获得请求参数?
  • 如何在请求对象中共享数据?

5 实验总结

这个实验中,实现了request对象、session对象、application对象的用法,实现数据的传递和共享。

猜你喜欢

转载自blog.csdn.net/zhaoxuyang1997/article/details/82931372
今日推荐