分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

JSP和El表达式和JSTL标签库使用

发布时间:2023-09-06 01:17责任编辑:顾先生关键词:暂无标签

JSP 指令是通知JSP引擎如何处理该JSP页面,不是针对程序员的。

共有三种指令:

1.page指令:

1.1. language="java" 默认是java,只能写java

1.2.import="package.class" 引入源码包

1.3.session="true" 默认创建session,true表示自动创建,jsp和servlet能获取同样的HttpSession

1.4.buffer="8kb" JSP输出内容,不是一下到浏览器的,而是等缓存满了以后,才会输出到浏览器,该JSP缓存默认大小为8kb

1.5.errorPage="url" 表示该页面出错后跳转到哪个页面

isErrorPage="true" 显示出错信息页面必须声明为true,这样JSP就会创建exception对象。

以上2个属性都是处理局部异常处理的。

全局异常:

配置web.xml文件

[html]view plaincopy
  1. <error-page>
  2. <error-code>500</error-code>
  3. <location>/WEB-INF/sys500.jsp</location>
  4. </error-page>



也可以处理类型异常

[html]view plaincopy
  1. <error-page>
  2. <error-type>java.lang.ArithmeticException</error-type>
  3. <location>/WEB-INF/sys500.jsp</location>
  4. </error-page>



类型异常优先级高。局部异常优先级最高。

contentType="UTF-8"。对内:表示JSP保存的编码;对外:表示浏览器用什么方式解码

pageEncoding="UTF=8";和contentType作用相同

当两个属性同时存在的时候,对内有pageEncoding决定。

isELIgored="false"

是否忽略EL表达式,默认支持EL表达式

2.include指令:

<$@ include file="被包含的jsp页面"$>

在翻译成servlet是将被包含的jsp页面中的内容翻译成servlet中了,即多个jsp只会翻译成一个Servlet 我们通常将

include指令包含的jsp页面叫静态包含,就是说编译时包含。

3.taglib指令:

[html]view plaincopy
  1. <%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

可以将数据绑定到域对象中

<c:set var="name" value="靠谱" scope="request"/>

将“靠谱”字符串绑定到request域中,名称为name。

<c:out value="${NAME}" default="查无此人"/>

用EL表达式调用名字为name的数据,如果查不到,返回空字符串。

request.getAttribute("NAME"); 该方法如果查不到数据,返回的是null

JSP九大隐式对象:

JSP中叫法 Servlet中叫法

1.request HttpServletRequest

2.response HttpServletResponse

3.session HttpSession

4.application ServletContext

5.config ServletConfig

6.out JspWriter

7.exception 特殊情况下使用

8.page this 本jsp页面

9.pageContext 无


下面演示:request/session/application用法:

[html]view plaincopy
  1. <html>
  2. <body>
  3. 参数值:<%=request.getParameter("name")%><br/>
  4. <%
  5. session.setAttribute("name","哈哈");
  6. application.setAttribute("age","20");
  7. %>
  8. 姓名:<%=session.getAttribute("name")%><br/>
  9. 年龄:<%=application.getAttribute("name")%>
  10. </body>
  11. </html>


out对象和Servlet中PrintWriter的区别:


1,out内容先输入jspwriter缓存区,然后输给printwriter缓存区,然后在输给浏览器
2,pageContext是一个作用域,仅在当前jsp页面中有效,也能完成转发和包含功能。

3,pageContext也可以获取其他八个内置对象

4,pageContext能将值绑定到其他域对象中。

5,pageContext对象的findAttribute方法会依次从page->request -> session -> application域对象查找,找到即止。

映射JSP

web.xml文件:

[java]view plaincopy
  1. <servlet>
  2. <servlet-name>SimpleJspServlet</servlet-name>
  3. <jsp-file>/simple.jsp</jsp-file>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>SimpleJspServlet</servlet-name>
  7. <url-pattern>index.html</url-pattern>
  8. </servlet-mapping>



El表达式:

${} :可以自动获取域中对象,request,session,application,pageContext,EL找不到返回空白字符串。

EL表达式中11大内置对象

1,pageContext 获取页面context的Map对象

2,pageScope 获取页面域的Map对象

3,requestScope 获取request域的Map对象

4,sessionScope 获取session域Map对象

5,applicationScope 获取context的Map对象

6,param 获取一个请求参数

7,paramValues 获取一个请求参数数组

8, header 获取一个请求域对象

9,headerValues

10,cookies

11,initParam 获取web.xml中的参数

[html]view plaincopy
  1. <context-param>
  2. <param-name></param-name>
  3. <param-value></param-value>
  4. </context-param>


<init-param>只有在本servlet中有效

El表达式中可以直接获取域中的数据:


[html]view plaincopy
  1. <%
  2. pageContext.setAttribute("NAME","哈哈");
  3. %>
  4. 姓名:${NAME}<br/>
  5. <hr/>
  6. <%
  7. Useruser=newUser(2015,"呵呵",10000D);
  8. request.setAttribute("USER",user);
  9. %>
  10. 编号:${USER.id}<br/>
  11. 姓名:${USER.name}<br/><!--自动调用getter方法-->
  12. 薪水:${USER.sal}<br/>
  13. <hr/>
  14. <%
  15. List<String>nameList=newArrayList<String>();
  16. nameList.add("A");
  17. nameList.add("B");
  18. nameList.add("C");
  19. session.setAttribute("NAMELIST",nameList);
  20. %>
  21. 第二个元素是:${NAMELIST[1]}<br/>
  22. <hr/>
  23. <%
  24. Map<String,Integer>map=newLinkedHashMap<String,Integer>();
  25. map.put("jack",10000);
  26. map.put("marry",12000);
  27. map.put("sisi",14000);
  28. application.setAttribute("MAP",map);
  29. %>
  30. SISI的工资是:${MAP[‘sisi‘]}<br/>
  31. <hr/>
  32. <%
  33. String[]strArray={"北京","上海","广州","深圳"};
  34. pageContext.setAttribute("STRARRAY",strArray);
  35. %>
  36. 你目前所在的城市是:${STRARRAY[2]}<br/>
  37. <hr/>
  38. 姓名:${NAMEE}<br/>





EL表达式中的运算符:

[html]view plaincopy
  1. <%@pagelanguage="java"pageEncoding="UTF-8"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <body>
  5. 10+3=${10+3}<br/>
  6. 10-3=${10-3}<br/>
  7. 10*3=${10*3}<br/>
  8. 10/3=${10/3}<br/>
  9. 10%3=${10%3}<br/>
  10. <hr/>
  11. true&&false=${true&&false}<br/>
  12. true||false=${true||false}<br/>
  13. !false=${!false}<br/>
  14. <hr/>
  15. 10>3=${10>3}<br/>
  16. 10!=3=${10ne3}<br/>
  17. 10==3=${10eq3}<br/>
  18. </body>
  19. </html>





EL表达式中的三木运算符:

[html]view plaincopy
  1. <%@pagelanguage="java"pageEncoding="UTF-8"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <body>
  5. <%
  6. pageContext.setAttribute("city","深圳");
  7. %>
  8. 城市:
  9. <selectname="city">
  10. <option>选择城市</option>
  11. <option${city==‘北京‘?‘selected‘:‘‘}>北京</option>
  12. <option${city==‘上海‘?‘selected‘:‘‘}>上海</option>
  13. <option${city==‘深圳‘?‘selected‘:‘‘}>深圳</option>
  14. <option${city==‘广州‘?‘selected‘:‘‘}>广州</option>
  15. </select>
  16. </body>
  17. </html>



[objc]view plaincopy
  1. <%@pagelanguage="java"pageEncoding="UTF-8"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <body>
  5. 当前web应用的根目录名:${pageContext.request.contextPath}<br/>
  6. <ahref="${pageContext.request.contextPath}/el/03_el.jsp">点点</a><br/>
  7. <%
  8. session.setAttribute("NAME","哈哈");
  9. %>
  10. 姓名:${sessionScope.NAME}<br/>
  11. 参数值:${param.name}<br/>
  12. 第三个爱好是:${paramValues.like[2]}<br/>
  13. 请求头1:${header.host}<br/>
  14. 请求头2:${headerValues["Accept-Encoding"][0]}<br/>
  15. cookie的名:${cookie.PASS.name}<br/>
  16. cookie的值:${cookie.PASS.value}<br/>
  17. <hr/>
  18. web初始化参数之driver为:${initParam.driver}<br/>
  19. web初始化参数之url为:${initParam.url}<br/>
  20. </body>
  21. </html>

JSP-JSTL标签库----函数fn

1,导入相关的jstl包

2,要在使用jstl的页面中用taglib指令引入相关包

[html]view plaincopy
  1. <%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn"%>



fn:toLowerCase("str") 字符串变成小写

fn:toUpperCase("str") 字符串变成大写

fn:trim("str") 去掉字符串两端空白

fn:split("String","regex") 切割字符串

fn:join("array","#") 用#把array中每个元素连接

fn:indexOf("str","s") 返回第二个参数在第一个参数首次出现的位置

fn:contains("s1","s2") 返回第二个参数是否包含在第一个参数

fn:startsWith("s1","s2") 第一个参数是否以第二个参数开头

fn:endsWith("s1","s2") 第一个参数是否以第二个参数结尾

fn:replace("s1","s2","s3") 把s1中的所有s2用s3替换

fn:substring("s1",a,b) 把s1中的第a个字符到b-1个字符截取出来

fn:substringAfter() ${fn:substringAfter("www@163@com","@")}

fn:substringBefore() ${fn:substringBefore("www@163@com","@")}

JSP-JSTL标签库---核心core

<c:out>标签 -----把内容输出到浏览器

[html]view plaincopy
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
  3. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  4. <html>
  5. <head>
  6. <%
  7. pageContext.setAttribute("script","<script>alert(‘哈哈‘);</script>");
  8. %>
  9. </head>
  10. <body>
  11. <!--
  12. escapeXml="false"表示不转义js代码
  13. -->
  14. <c:outvalue="${script}}"escapeXml="false"/>
  15. </body>
  16. </html>




<c:set>标签 ---- 把数据绑定到域中

[html]view plaincopy
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@pageimport="itcast.util.User"%>
  3. <%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
  4. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  5. <html>
  6. <head>
  7. <title>MyJSP‘02_c_set.jsp‘startingpage</title>
  8. <metahttp-equiv="pragma"content="no-cache">
  9. <metahttp-equiv="cache-control"content="no-cache">
  10. <metahttp-equiv="expires"content="0">
  11. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  12. <metahttp-equiv="description"content="Thisismypage">
  13. </head>
  14. <body>
  15. <!--
  16. classUser{
  17. privateStringid;
  18. privateStringname;
  19. publicvoidsetId(Stringid){
  20. this.id=id;
  21. }
  22. publicvoidsetName(Stringname){
  23. this.name=name;
  24. }
  25. publicStringgetId(){
  26. returnthis.id;
  27. }
  28. publicStringgetName(){
  29. returnthis.name;
  30. }
  31. }
  32. -->
  33. <%
  34. Useruser=newUser();
  35. pageContext.setAttribute("USER",user);
  36. %>
  37. <c:settarget="${pageScope.USER}"property="id"value="2015"/>
  38. <c:settarget="${pageScope.USER}"property="name"value="笨笨"/>
  39. 编号:${pageScope.USER.id}<br/>
  40. 姓名:${pageScope.USER.name}
  41. </body>
  42. </html>







<c:remove>标签

<c:remove var="NAME" scope="page"/> 移除pageContext域中的名为NAME的数据




<c:catch>标签

<c:catch var="myError">

<%

int i = 10/0;

%>

</c:catch>

原因为:${myError.message}<br/>






<c:if>标签

<c:if test="判断条件">

</c:if>


<c:choose>和<c:when><c:otherwise>标签



<c:choose>
<c:when test="">

</c:when>
<c:when test="">

</c:when>
<c:when test="">

</c:when>
<c:otherwise>

</c:otherwise>
</c:choose>




<c:forEach var items/>标签

[html]view plaincopy
  1. <%@pagelanguage="java"pageEncoding="UTF-8"%>
  2. <%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
  3. <%@pageimport="java.util.*"%>
  4. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  5. <html>
  6. <body>
  7. <%
  8. List<String>nameList=newArrayList<String>();
  9. nameList.add("小德子");
  10. nameList.add("小格子");
  11. nameList.add("小桌子");
  12. nameList.add("小羊子");
  13. nameList.add("小凳子");
  14. pageContext.setAttribute("NAMELIST",nameList);
  15. %>
  16. <tableborder="2"align="center">
  17. <tr>
  18. <th>索引</th>
  19. <th>编号</th>
  20. <th>姓名</th>
  21. <th>是第一个元素吗</th>
  22. &n
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved