jsp九大内置对象(9.16)

jsp九大内置对象

1.request对象

1.使用getParammeter()获取请求参数
2.获取网页请求参数

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>requset</title>
  </head>
  <body>
      <center>
             郑雅妮
          <%--获取name和address请求参数--%>
        <%

          String name=request.getParameter("name");
          String address=request.getParameter("address");

        %>
        <p>
             欢迎<%=name%>来到   <%=address%>
        </p>

        </center>
  </body>
</html>

地址栏输入:http://localhost:8080/web_war_exploded/运行结果

2.response对象

1.reponse对象实现页面重定向

<%@ page contentType="text/html;charset=UTF-8" language="java"
         pageEncoding="utf-8"%>
<html>
<head>
    <title>response</title>
</head>
<body>
    <center>
        跳转页面到百度主页
        <%
            response.sendRedirect("https://www.baidu.com");
        %>
    </center>
</body>
</html>

运行结果

3.application对象

1.使用application保存网站访问人数,使得多个JSP,Servlet共享数据
2.代码如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  Integer count=(Integer) application.getAttribute("count");
  if(count==null)
  {count=1;}
  else{count++;}
  application.setAttribute("count",count);
%>
<html>
  <head>
    <title>application</title>
  </head>
  <body>
  <center>
    欢迎访问本网站,您是第<%=count%>位访问客户!
  </center>
  </body>
</html>

3.地址栏输入:http://localhost:8080/web_war_exploded/
运行结果

4.out对象

1.输出表格
2.代码如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>out</title>
</head>
<body>
<center>
      <hr>
    <h4>表格如下</h4>
    <%
        out.println("<table border='2'>");
        out.println("<tr>");
        out.println("<td width='60'>"+"姓名"+"</td>");
        out.println("<td width='40'>"+"性别"+"</td>");
        out.println("<td width='80'>"+"姓出生年月"+"</td>");
        out.println("<td width='60'>"+"城市"+"</td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td width='60'>"+"zyn"+"</td>");
        out.println("<td width='60'>"+"famale"+"</td>");
        out.println("<td width='60'>"+"1984.8"+"</td>");
        out.println("<td width='60'>"+"China"+"</td>");
        out.println("</tr>");
        out.println("</table>");
    %>
</center>
</body>
</html>

3.地址栏输入:http://localhost:8080/web_war_exploded/index2.jsp
4.运行结果

5.config对象

1.代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>
            jspconfigdemo
        </servlet-name>
        <jsp-file>/index.jsp</jsp-file>
        <init-param>
            <param-name>url</param-name>
            <param-value>http://www.baidu.com</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>
            jspconfigdemo
        </servlet-name>
        <url-pattern>/index.jsp</url-pattern>
    </servlet-mapping>

</web-app>
----------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>config</title>
</head>

<body>
<%
    String url = config.getInitParameter("url");
    String str = config.toString();
    out.print("page对象的initParameter方法:"+url+"</br>");
    out.print("page对象的toString方法:"+str);
%>
</body>
</html>

2.运行结果

6.page对象

1.代码如下

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" %>
<html>
    <head>
        <title>page</title>
    </head>
    <body>
        <center>
            <%
                int code=page.hashCode();
                String str=page.toString();
                out.println("page对象的hash码"+code+"&nbsp;&nbsp;</br>");
                out.println("page对象的值:"+str);
            %>
        </center>
    </body>
</html>

2.地址栏输入:http://localhost:8080/web_war_exploded/page.jsp运行结果

7.exception对象

1.代码如下:
第一个jsp文件,命名为exception.jsp。

<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %>
<html>
<head>
    <title>exception</title>
</head>
<body>
        <%
            int a=8;
            int b=0;
            int c=a/b;
        %>
    </body>
</html>

第二个jsp文件,命名为error.jsp文件。

<%@ page contentType="text/html;charset=UTF-8" language="java"
         pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
    <title>异常处理页面</title>
</head>
<body>
<hr>
异常类型:<%=exception.getClass()%><br/><br/>
异常信息:<%=exception.getMessage()%>
</hr>
</body>
</html>

2.运行结果

8.session对象

1.session1.jsp文件代码如下

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <base href="<%=basePath%>">

    <title>session1</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<form id="form1" name="form1" method="post" action="session2.jsp">
    <div align="center">
        <table width="40%" border="0">
            <tr>
                <td width="36%"><div align="center">您的名字是:</div></td>
                <td width="64%">
                    <label>
                        <div align="center">
                            <input type="text" name="name" />
                        </div>
                    </label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <label>
                        <div align="center">
                            <input type="submit" name="Submit" value="提交" />
                        </div>
                    </label>
                </td>
            </tr>
        </table>
    </div>
</form>
</body>
</html>

运行结果
2.session2.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
    <base href="<%=basePath%>">

    <title>session2</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<%
    String name = request.getParameter("name");

    session.setAttribute("name",name);
%>
<div align="center">
    <form id="form1" name="form1" method="post" action="sessionShow.jsp">
        <table width="28%" border="0">
            <tr>
                <td>您的名字是:</td>
                <td><%=name%></td>
            </tr>
            <tr>
                <td>您最喜欢去的地方是:</td>
                <td><label>
                    <input type="text" name="address" />
                </label></td>
            </tr>
            <tr>
                <td colspan="2"><label>
                    <div align="center">
                        <input type="submit" name="Submit" value="提交" />
                    </div>
                </label></td>
            </tr>
        </table>
    </form>
    <p>&nbsp;</p>
</div>
</body>
</html>

运行结果
3.sessionShow.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
    <base href="<%=basePath%>">

    <title>session3</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<div align="center">
    <%
        Cookie[] ck=request.getCookies();

        for(Cookie coo:ck){
            String ID=coo.getValue();
            System.out.println("ID:"+ID);
        }

        String sessionID=request.getSession(true).getId();

        String name = (String)session.getAttribute("name");

        String solution = request.getParameter("address");
    %>
    <form id="form1" name="form1" method="post" action="">
        <table width="28%" border="0">
            <tr>
                <td colspan="2"><div align="center"><strong>显示答案</strong></div>          </td>
            </tr>
            <tr>
                <td width="49%"><div align="left">您的名字是:</div></td>
                <td width="51%"><label>
                    <div align="left"><%=name%></div>
                </label></td>
            </tr>
            <tr>
                <td><label>
                    <div align="left">您最喜欢去的地方是:</div>
                </label></td>
                <td><div align="left"><%=solution%></div></td>
            </tr>

            <td><label>
                <div align="left">sessionID:</div>
            </label></td>
            <td><div align="left"><%=sessionID%></div></td>
            </tr>



        </table>
    </form>
    <p>&nbsp;</p>
</div>
</body>
</html>

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值