JSP内置对象/EL/JSTL

本文介绍了MVC设计模式的三个核心层:模型层、视图层和控制层,详细讲解了JSP的内置对象、EL表达式语言的使用,包括访问不同属性范围、内置对象、参数、cookie和header,以及EL的运算符。此外,还阐述了JSTL的引入步骤和常见标签,如<c:if>、<c:forEach>和<c:choose>等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:MVC设计模式

MVC是一个设计模式,它强制的使应用层的输入,处理和输出分开。使用MVC设计模式被分为三个核心层:模型层,视图层,控制层。它们各自处理自己的任务。

显示层(View):此层主要是负责将内容显式给用户。比如:JSP

控制层(Controller):负责判断所有的用户请求参数是否合法,根据请求的类型调用模型层执行操作,再讲处理结果交给显示层显示。eg:servlet

模型层(Model):操作数据库的独立的操作组件,或使用lavaBean(POJO)保存数据。

二:JSP内置对象

JSP中提供了九个内置对象,这些内置对象由容器为用户进行实例化,用户直接使用即可。


 eg:

(1):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>response内置对象</title>
</head>
<body>
    <h3>3秒之后跳转到index页面</h3>
    <%
      //  response.setHeader("refresh","3"); 定时刷新
      response.setHeader("refresh","3;URL=index.jsp");   // 定时跳转
    %>
</body>
</html>
(2):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>index.jsp</title>
</head>
<body>
   <h3>跳转过来了</h3>
</body>
</html>
(3):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>处理错误的页面</title>
</head>
<body>
   <%
      String errorMsg=exception.getMessage();
      out.println("<h3>有点问题:"+errorMsg+"</h3>");
   %>
</body>
</html>

(4):<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JSPProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- context-param配置的是上下文参数,给应用设置参数,所有Servlet(JSP)共享这个参数 -->
  <context-param>
      <param-name>ctx</param-name>
      <param-value>ContextParamValue</param-value>
  </context-param>
  
  <jsp-config>
      <taglib>
          <taglib-uri>http://www.mycompany.com</taglib-uri>
          <taglib-location>/WEB-INF/c.tld</taglib-location>
      </taglib>
  </jsp-config>
</web-app>

三:EL表达式语言

使用表达式语言(EL)可以在JSP页面进行方便的输出内容:

EL语法:${表达式}

EL有自己的内置对象:

PageContext ,pageScope,requestScope,sessionScope,applicationScope,param,paramValues,header,headerValues,cookie,initParam

1. 使用EL访问不同的属性范围:

${pageScope.属性名}${requestScope.属性名},${sessionScope.属性名},${applicationScope.属性名},这四种属性访问范围由小到大。

Eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问不同属性范围的值</title>
</head>
<body>
   <%
       pageContext.setAttribute("info","page范围的属性值");
       request.setAttribute("info","request范围的属性值");
       session.setAttribute("info","session范围的属性值");
       application.setAttribute("info","application范围的属性值");
   %>
   
   <h3>page===>  ${pageScope.info}</h3>
   <h3>request===>  ${requestScope.info}</h3>
   <h3>session===>  ${sessionScope.info}</h3>
   <h3>application===>  ${applicationScope.info}</h3>
   <h3>${info}=====>  ${info}</h3>
</body>
</html>

2. EL访问JSP内置对象

使用ELpageContext内置对象访问JSP的内置对象:${pageContext.对应的jsp内置对象}

Eg:EL获取上下文路径:特殊:${pageContext.servletContext.contextPath}

EL访问sessionID:${pageContext.session.id}

Eg::

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问jsp内置对象</title>
</head>
<body>
   <%
       String contextPath=application.getContextPath();
   %>
   <h3>直接通过JSP内置对象获取上下文路径:<%=contextPath%></h3>
   <h3>通过EL获取上下文路径:${pageContext.servletContext.contextPath}</h3>
   <h3>通过EL获取sessionID: ${pageContext.session.id}</h3>
   <h3>通过EL判断当前的请求方式:  ${pageContext.request.method}</h3>
</body>
</html>

3. EL访问参数(访问客户端发送的参数。全局参数,一组参数)

用途1:使用ELparam内置对象访问客户端发送的参数${param.参数名}

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问客户端发送的请求参数</title>
</head>
<body>
   <h3>客户端发送来的username参数是:${param.username}</h3>
   <h3>客户端发送来的pwd参数是:${param.pwd}</h3>
</body>
</html>

用途2:使用ELinitParam内置对象访问上下文参数(全局参数),在web.xml中配置上下文参数:

<context-param>

<param-name>admin</ param-name >

<param-value>Obama</ param-value>

</context-param>

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问全局参数(上下文参数)</title>
</head>
<body>
   <h3>全局参数为ctx的值是:${initParam.ctx}</h3>
</body>
</html>

用途3:使用ELparamValues内置对象访问一组参数

           ${paramValues.参数名[0]}访问一组参数值

           ${paramValues.参数名[n]}访问n+1组参数值

eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>兴趣爱好页面</title>
</head>
<body>
   <form action="paramValues.jsp" method="post">
            运动:   <input type="checkbox" name="hobby" value="sport"/>  <br/>
            电影:  <input type="checkbox" name="hobby" value="movie"/>  <br/>
            读书:  <input type="checkbox" name="hobby" value="reading"/>  <br/>
            音乐:  <input type="checkbox" name="hobby" value="music"/>  <br/>
            <input type="submit" value="提交"/>
   </form>
</body>
</html>

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问一组参数</title>
</head>
<body>
   <h3>爱好是:</h3>
   <h3> ${paramValues.hobby[0]} </h3>
   <h3> ${paramValues.hobby[1]} </h3>
   <h3> ${paramValues.hobby[2]} </h3>
   <h3> ${paramValues.hobby[3]} </h3>
</body>
</html>

4. 访问cookie

通过ELcookie内置对象访问JSESSIONID名称的cookie的语法:

${cookie[“JSESSIONID”].name},${cookie[“JSESSIONID”].value}

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL的cookie内置对象访问Cookie</title>
</head>
<body>
   <h3>Cookie名为JSESSIONID的值为: ${cookie["JSESSIONID"].value}</h3>
   <h3>sessionID为:${pageContext.session.id}</h3>
</body>
</html>

5. 访问header

${header[“cookie”] }

 EL的运算符

1. 算数运算符
      + - * /(div)  %(mod)
      eg: ${param.num1+param.num2}   参数相加


 2. 关系运算符
       < (lt)  > (gt)  == (eq)  !=(ne)  >=(ge) <=(le)


 3. 逻辑运算符

       &&(and)   ||(or)  !(not)

4. empty运算符
   ${empty 表达式}  判断是否为null或空字符串””


5. 三目运算符
       ${返回true或false的表达式 ? "为true时输出的内容":"为false时输出的内容"}


6. 括号运算符
       ()用来改变运算顺序的


eg:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL运算符</title>
</head>
<body>
   <h3>num1+num2= ${param.num1+param.num2}</h3>
   <h3>num1/num2= ${param.num1 / param.num2} 也可以div ${param.num1 div param.num2}</h3>
   <h3>num1是否等于num2: ${param.num1 == param.num2}</h3>
   <h3>true && true: ${true  && true}</h3>
   <h3>true || false: ${true  || false}</h3>
   <h3>${empty param.abc}</h3>
   <h3>三目运算符:${param.num1>param.num2 ? "num1真的大于num2":"num1不大于num2" }</h3>
</body>
</html>

四:JSTL(JSP Standard Tag Libraries)

引入JSTL的步骤:

   第一步:将jstl-xx.jar导入/WEB-INF/lib文件夹下。




   第二步:将jstl-xx.jar解压后的META-INF文件夹下的xxx.tld文件拷贝到/WEB-INF/的某目录下


   第三步:在JSP页面使用taglib指令引入xxx.tld文件。

  

 JSP页面使用taglib指令引入xxx.tld文件

  <%@ taglib uri="指向xxx.tld文件的路径"     prefix="JSTL标签前缀"%>
注意:uri中指向xxx.tld文件的路径有两种写法:
          第一种:直接指向xxx.tld文件的路径
第二种:在web.xml中配置taglib的uri
        <jsp-config>
                 <taglib>
                       <taglib-uri>http://www.xxx.com</taglib-uri>
                       <taglib-location>/WEB-INF/c.tld</taglib-location>
                   </taglib>
         </jsp-config>
1.输出标签
    <c:out value="输出的内容,支持EL"  default=" 默认值,支持EL"/>
2.设置标签
      设置属性范围的属性值
      <c:set var="属性名" value="属性值,支持EL" scope="属性范围"/>

设置对象的属性值
   <c:set target=“${perObj}” property=“对象的属性名” value=“支持EL"/>
3.捕获异常标签
      <c:catch var="保存异常信息的属性名">
           // 有可能发生异常的代码

      </c:catch>

4.判断标签
     <c:if test="判断表达式,支持EL">
            // 判断结果为true,执行此处
     </c:if>
5.forEach标签(遍历list集合或Map集合)
      <c:forEach  var="当前正在遍历的对象,作为属性用(msg)"  items="要遍历的集合,支持EL">
            ${msg}

      </c:forEach>

6.choose标签
      <c:choose>
          <c:when test="${param.score>=90}">
               <h3>优秀</h3>
          </c:when>
          <c:when test="${param.score>=80}">
               <h3>良好</h3>
          </c:when>
          <c:otherwise>
               <h3>不及格,要加油!</h3>
          </c:otherwise>
      </c:choose>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值