zoukankan      html  css  js  c++  java
  • EL中的内置对象

    EL  内置对象


      就像 JSP 的 Java 代码块及表达式块中可以使用九个内置对象一样,EL 表达式中,同样也存在有内置对象,并且存在 11 个内置对象。

      常用的内置对象,除了前面使用过的四个域属性空间相关的内置对象外,还有如下几个。


    (5 ) pageContext
      该 pageContext 与 JSP 内置对象中的 pageContext 是同一个对象。

      通过该对象,可以获取到 request、response、session、servletContext、servletConfig 等对象。

      注意这些对象在 EL中不是内置对象,这些对象只能通过 pageContext 获取。

      在 EL 中直接${pageContext.request}即可获取 request 对象。当然,其底层实际调用的是pageContext.getRequest()方法。

      同理,也可以通过类似方式获取到其它对象。
      在这些获取的对象中,有一个是实际工程中最常用的:${pageContext.request.contextPath} ,用于获取当前项目的发布到服务器的名称。一般会用在 JSP 页面的路径前.

      

      先写一个 

      

      index.jsp

     1     <form action="${pageContext.request.contextPath }/RegisterServlet" method="POST">
     2         姓名:<input type="text" name="name"/><br>
     3         年龄:<input type="text" name="age"/><br>
     4         爱好:
     5         <input type="checkbox" name="hobby" value="swimming"/> 游泳
     6         <input type="checkbox" name="hobby" value="climbing"/> 登山
     7         <input type="checkbox" name="hobby" value="reading"/> 读书
     8         <br>
     9         <input type="submit" value="注册"/>
    10     </form>

       RegisterServlet.java

     1 package com.bjpowernode.servlet;
     2 
     3 import java.io.IOException;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.http.HttpServlet;
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 
     9 public class RegisterServlet extends HttpServlet {
    10     protected void doPost(HttpServletRequest request, HttpServletResponse response)
    11             throws ServletException, IOException {
    12         System.out.println("=================");
    13     }
    14 
    15 }

      在 EL 的 11 个内置对象中,除了 pageContext 外,其它 10 个内置对象,其类型均为java.util.Map 类型


    (6 ) param
      在 EL 中通过 ${param.参数名}可获取到请求中指定参数名的值。 例如,提交的请求为:

      

       在 JSP 页面中通过如下方式,可获取到 name 参数的值为 abc

      


    (7 ) paramValues
      若提交的请求中同一参数具有多个值,则可通过 ${paramValues.参数名[索引]} 获取到指定索引号的该参数值。例如,提交的请求为:

      

      在 JSP 页面中获取方式如下:

      

       在浏览器中显示内容如下

      


     (8 ) initParam
        在EL中通过使用 ${initParam.初始化参数名} 可以获取到指定的初始化参数的值。

        例如,在 web.xml 中定义了初始化参数 xxxName。

        

      在 JSP 的 EL 中可访问该初始化参数:

        


    示例:

      index.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="${pageContext.request.contextPath }/show.jsp" method="POST">
    11         姓名:<input type="text" name="name"/><br>
    12         年龄:<input type="text" name="age"/><br>
    13         爱好:
    14         <input type="checkbox" name="hobby" value="swimming"/> 游泳
    15         <input type="checkbox" name="hobby" value="climbing"/> 登山
    16         <input type="checkbox" name="hobby" value="reading"/> 读书
    17         <br>
    18         <input type="submit" value="注册"/>
    19     </form>
    20 </body>
    21 </html>

    web.xml

    1     <context-param>
    2         <param-name>company</param-name>
    3         <param-value>bjpowernode</param-value>
    4     </context-param>
    5     <context-param>
    6         <param-name>address</param-name>
    7         <param-value>中国北京</param-value>
    8     </context-param>

    show.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <!-- 获取请求中的指定参数值,其底层实际调用的是:
    11             request.getParameter()
    12      -->
    13     name = ${param.name } <br>
    14     age = ${param.age } <br>
    15     
    16     <!-- 获取请求中的指定参数的所有值,其底层实际调用的是:
    17             request.getParameterValues()
    18      -->
    19     hobby[0] = ${paramValues.hobby[0] } <br>
    20     hobby[1] = ${paramValues.hobby[1] } <br>
    21     hobby[2] = ${paramValues.hobby[2] } <br>
    22     
    23     <!-- 获取初始化参数,其底层实际调用的是:
    24             servletContext.getInitParameter()
    25      -->
    26     company = ${initParam.company } <br>
    27     address = ${initParam.address } <br>
    28     
    29 </body>
    30 </html>

      

      


  • 相关阅读:
    0127 date dateformat calebdar
    0126 字符串缓冲区StringBuffer类 正则表达式
    0126 String类
    0125 java API object
    0125 匿名对象 内部类 包 代码块
    0123 final关键字,static 关键字
    0123 this关键字 super关键字
    0122面向对象 构造方法
    0122面向对象3 多态
    0120 面向对象2
  • 原文地址:https://www.cnblogs.com/penguin1024/p/12174120.html
Copyright © 2011-2022 走看看