一、JSP内置对象简介

1、 request 对象:用户端请求,此请求会包含来自 GET/POST请求的参数。

a.获取表单传来的值:例:<% String userName = request.getParameter("userName");%>

如果(getParameter方法)报错:转https://www.cnblogs.com/lmbl/p/16273054.html解决。

如果表单提交中文乱码:转https://www.cnblogs.com/lmbl/p/16273231.html解决。

作用:代表请求对象,用来接收客户端通过http 协议连接传输到服务器端的数据。

 

2、response 对象

网页传回用户端的回应,代表响应对象,主要用于向客户端发送数据。

a.3秒后自动跳转指定网页

<%
//response对象,3秒后自动跳转指定网页
response.setHeader("refresh","3;URL=sencondPage.jsp");
%>

 

b.重新定向

 <%
            request.setCharacterEncoding("utf-8");
            String userName = request.getParameter("userName");
            if(userName ==null || userName.equals("")){
                //重新定向
                response.sendRedirect("firstPage.jsp");
            }
        %>

 

 

作用:代表响应对象,用来向客户端发送数据。

 

3、out 对象

网页的属性是在这里管理

作用:主要用于向客户端发送数据。其中JspWriter 是out 的基类;在脚本中,向网页中输出内容或变量。

输出:

<% String name = "张三";

  out.print(name);//输出变量

 或out.print("<p>+name+"</p>)//输出H5标签

 

4、session 对象

与请求有关的会话期

作用:主要用于来分别保存每个用户的个人信息,与请求关联的对话。

5、application 对象

表示整个服务器,所有客户端使用的application都是同一个;

作用:实现了用户间数据恭喜概念股,可存放全局变量。

6、PageContext 对象

网页的属性是在这里管理

作用:作用域page;可以访问其他八个对象。

 

 

 

计算网站访问次数(例子):

<%--
  Created by IntelliJ IDEA.
  User: likeyi
  Date: 2022/5/15
  Time: 17:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网站访问次数</title>
</head>
<body>
    <%
        if(pageContext.getAttribute("nums")==null || pageContext.getAttribute("nums").equals("")){
            //设置值
            pageContext.setAttribute("nums",1);
        }if(session.getAttribute("nums")==null || session.getAttribute("nums").equals("")){
            //设置值
            session.setAttribute("nums",1);
        }if(application.getAttribute("nums") == null || application.getAttribute("nums").equals("")){
            //设置值
            application.setAttribute("nums",1);
        }else {
            int numsByPageContext = (int)pageContext.getAttribute("nums") + 1;
            pageContext.setAttribute("nums",numsByPageContext);

            int numsBySession = (int)session.getAttribute("nums") + 1;
            session.setAttribute("nums",numsBySession);

            int numsByApplication = (int)application.getAttribute("nums") + 1;
            application.setAttribute("nums",numsByApplication);
            
        }
    %>
    <p>page...:<%= pageContext.getAttribute("nums")%></p>
    <p>session...:<%= session.getAttribute("nums")%></p>
    <p>app...:<%= application.getAttribute("nums")%></p>
</body>
</html>

 

 

7、Config 对象

获取服务器配置信息,去初始化参数,初始化参数在web.xml中配置

作用:代码片断配置对象,表示对servlet 的配置。

8、page对象

如同this一样,表示JSP整个页面。

作用:处理jsp 网页,是object 类的一个实例。

 

9、Exception对象

表示错误页面处理操作

作用:处理jsp文件执行时发生的错误和异常,只有在错误页面里才使用,前提是在页面指令里要有isErrorPage=true。

注:如果用户在网站遇到报错(505,404等)

可以建相对应的处理网页,让用户看不到报错的尴尬页面

 //例子:报错(505)

<%--
  Created by IntelliJ IDEA.
  User: lingfang
  Date: 2022/5/15
  Time: 17:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误异常页面</title>
</head>
<body>
    <%
        //例子:报错(505)
        String s = null;
        out.print(s.toString());
    %>
</body>
</html>

相对应新建500处理页面:在page处记得isErrorPage="true"

<%--
  Created by IntelliJ IDEA.
  User: lingfang
  Date: 2022/5/15
  Time: 17:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>505</title>
</head>
<body>
    <h3 style="text-align: center">系统异常,请联系管理员!</h3>
    <h4 style="text-align: center">错误信息:<%= exception.getClass().getName()%></h4>
</body>
</html>

在去web.xml处配置错误页面

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>

</web-app>

注:如果以后程序中出现500错误都会跳入该错误提示页面

  <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>

更多HTTP状态码:https://www.runoob.com/jsp/jsp-http-status-codes.html(菜鸟教程)

 

posted on 2022-05-15 18:57  黎三岁  阅读(342)  评论(0编辑  收藏  举报