码迷,mamicode.com
首页 > Web开发 > 详细

JSP和Servlet[2]

时间:2017-01-30 22:45:20      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:生成   and   nal   parse   程序   对象   页面   rto   add   

JavaBean组件

JavaBean 是使用 Java 语言开发的一个可重用的组件,在 JSP 开发中可以使用 JavaBean 减少重复代码,使整个JSP 代码的开发更简洁。

1.jsp:useBean创建javaBean

<jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称"/>
Scope,一共有 page,request,session 和 application4 个属性范围,默认是 page;

2.jsp:setProperty设置javabean属性值

<jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有

3.jsp:getProperty获取javabean属性值

<jsp:getProperty property="属性名称" name="实例化对象的名称"/>

4.javabean的保存范围

Javabean 的保存范围有 page,request,session.application,默认是 page;

5.javabean删除

Page 范围:pageContext.removeAttribute(“javaBean Name”);
request 范围:request.removeAttribute(“javaBean Name”);
session 范围:session.removeAttribute(“javaBean Name”);
application 范围:application.removeAttribute(“javaBean Name”);
 

EL表达式

作用:使JSP写起来更加简单

1.语法结构

${expression}

2.EL表达式访问4种范围属性

寻找值的顺序:page->request->session->application


JSP自定义标签

(以后整理)
 

JSP标准标签库

1.JSTL简介

JSP 标准标签库(JSP Standard Tag Library,JSTL)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问。
使用JSTL标签库前需要导入两个包,分别是jstl.jar和standard.jar。
技术分享

2.JSTL核心标签库

c:out 内容输出标签;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     pageContext.setAttribute("people","张三");
13 %>
14 <h2><c:out value="${people}"></c:out></h2>
15 <h2><c:out value="${people2}" default="某人"></c:out></h2>
16 </body>
17 </html>
 
c:set 用来设置 4 中属性范围值的标签;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:set var="people" value="张三" scope="request"></c:set>
12 <h2><c:out value="${people}"></c:out></h2>
13 <jsp:useBean id="people2" class="cn.cslg.model.People" scope="page"></jsp:useBean>
14 <c:set property="id" target="${people2 }" value="007"></c:set>
15 <c:set property="name" target="${people2 }" value="王二小"></c:set>
16 <c:set property="age" target="${people2 }" value="16"></c:set>
17 <h2>编号:${people2.id }</h2>
18 <h2>姓名:${people2.name }</h2>
19 <h2>年龄:${people2.age }</h2>
20 </body>
21 </html>
 
c:remove 用来删除指定范围中的属性;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:set var="people" value="张三" scope="request"></c:set>
12 <h2><c:out value="${people}" default="没人啊"></c:out></h2>
13 <c:remove var="people" scope="request"/>
14 <h2><c:out value="${people}" default="没人啊"></c:out></h2>
15 </body>
16 </html>
 
c:catch 用来处理程序中产生的异常;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:catch var="errMsg">
12     <%
13         int a=1/0;
14     %>
15 </c:catch>
16 <h2>异常信息:${errMsg }</h2>
17 </body>
18 </html>
 
c:if 用来条件判断;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <jsp:useBean id="people" class="cn.cslg.model.People" scope="page"></jsp:useBean>
12 <c:set property="id" target="${people }" value="007"></c:set>
13 <c:set property="name" target="${people }" value="王二小"></c:set>
14 <c:set property="age" target="${people }" value="16"></c:set>
15 <c:if test="${people.name==‘王二小‘ }" var="r" scope="page">
16     <h2>是王二小</h2>
17 </c:if>
18 <c:if test="${people.age<18 }">
19     <h2>是未成年</h2>
20 </c:if>
21 </body>
22 </html>
 
c:choose、c:when、c:otherwise 用来多条件判断;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <jsp:useBean id="people" class="cn.cslg.model.People" scope="page"></jsp:useBean>
12 <c:set property="id" target="${people }" value="007"></c:set>
13 <c:set property="name" target="${people }" value="王二小"></c:set>
14 <c:set property="age" target="${people }" value="19"></c:set>
15 
16 <c:choose>
17     <c:when test="${people.age<18 }">
18         <h2>小于18</h2>
19     </c:when>
20     <c:when test="${people.age==18 }">
21         <h2>等于18</h2>
22     </c:when>
23     <c:otherwise>
24         <h2>大于18</h2>
25     </c:otherwise>
26 </c:choose>
27 </body>
28 </html>
 
c:forEach 用来遍历数组或者集合;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="cn.cslg.model.*"%>
 4 <%@ page import="java.util.*"%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13 <%
14     String dogs[]={"小黑","小黄","小白","小小"};
15     pageContext.setAttribute("dogs",dogs);
16 %>
17 <c:forEach var="dog" items="${dogs }">
18     ${dog }
19 </c:forEach>
20 <hr/>
21 <c:forEach var="dog" items="${dogs }" step="2">
22     ${dog }
23 </c:forEach>
24 <hr/>
25 <c:forEach var="dog" items="${dogs }" begin="1" end="2">
26     ${dog }
27 </c:forEach>
28 <hr/>
29 <%
30     List<People> pList=new ArrayList<People>();
31     pList.add(new People(1,"张三",10));
32     pList.add(new People(2,"李四",20));
33     pList.add(new People(3,"王五",30));
34     pageContext.setAttribute("pList",pList);
35 %>
36 <table>
37     <tr>
38         <th>编号</th>
39         <th>姓名</th>
40         <th>年龄</th>
41     </tr>
42     <c:forEach var="p" items="${pList }">
43         <tr>
44             <td>${p.id }</td>
45             <td>${p.name }</td>
46             <td>${p.age }</td>
47         </tr>
48     </c:forEach>
49 </table>
50 </body>
51 </html>
 
c:fortokens 分隔输出;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     String str1="www.google.com";
13     String str2="张三,李四,王五";
14     pageContext.setAttribute("str1",str1);
15     pageContext.setAttribute("str2",str2);
16 %>
17 <c:forTokens items="${str1 }" delims="." var="s1">
18     ${s1 }
19 </c:forTokens>
20 <hr/>
21 <c:forTokens items="${str2 }" delims="," var="s2">
22     ${s2 }
23 </c:forTokens>
24 </body>
25 </html>
 
c:import 导入页面;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:import url="c_forEach.jsp"></c:import>
12 <c:import url="c_if.jsp"></c:import>
13 </body>
14 </html>
 
c:url 生成一个 url 地址;
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:url value="http://www.google.com" var="url">
12     <c:param name="name" value="admin"></c:param>
13     <c:param name="age" value="3"></c:param>
14 </c:url>
15 <a href="${url }">CBA</a>
16 </body>
17 </html>
 
c:redirect 客户端跳转
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <c:redirect url="target.jsp">
12     <c:param name="name" value="abc"></c:param>
13     <c:param name="age" value="26"></c:param>
14 </c:redirect>
15 </body>
16 </html>

3.JSTL国际化标签库

fmt:setLocale 设定用户所在的区域;
fmt:formatDate 对日期进行格式化;
fmt:requestEncoding 设置所有的请求编码;
fmt:bundle fmt:message 读取国际化资源;
fmt:formatNumber 格式化数字;
fmt:formatDate 格式化日期;
fmt:timeZone 设置临时时区;

4.JSTL SQL标签库

Sql:setDataDource 设置 JDBC 连接;
sql:query 数据库查询操作;
Sql:update 数据库添加,修改,删除操作;
Sql:transaction 数据库事务;

5.JSTL XML标签库

x:parse 解析 xml;
x:out 输出 xml 文件的内容;
x:set 把 xml 读取的内容保存到指定的属性范围;
x:if 判断指定路径的内容是否符合判断的条件;
x:choose x:when x:otherwise 多条件判断;
x:forEach 遍历

6.JSTL函数标签库

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     pageContext.setAttribute("info","www.google.com");
13 %>
14 <h2>查找google位置:${fn:indexOf(info,"google")}</h2>
15 <h2>判断google是否存在:${fn:contains(info,"google")}</h2>
16 <h2>截取:${fn:substring(info,0,5)}</h2>
17 <h2>拆分:${fn:split(info,".")[1]}</h2>
18 </body>
19 </html>

JSP和Servlet[2]

标签:生成   and   nal   parse   程序   对象   页面   rto   add   

原文地址:http://www.cnblogs.com/ShawnYuki/p/6358420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!