JSP中正则表达式用法实例
本文实例讲述了JSP中正则表达式用法。分享给大家供大家参考,具体如下:
<%@pagelanguage="java"import="java.util.*,cn.com.Person,cn.com.Adddress"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'El.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<%
Stringdata="assd";
request.setAttribute("data",data);
%>
<!--正则表达式,对于Request传过来的数据可以直接如下显示
相当于:pageContext.findAttribute("data");
这是一般的方式
-->
${data}
<br/>
<%
Personp=newPerson();
p.setName("name");
request.setAttribute("person",p);
%>
<!--反射name属性获取这个值输出
数据通过JAVABean里传过来的如下:
-->
${person.name}
<br/>
<%
Personp2=newPerson();
Adddressadd=newAdddress();
add.setCity("NewYork");
p2.setAddress(add);
request.setAttribute("p2",p2);
%>
<!--下面的是定义一个Person的类和一个Adddress的类
Person中私有属性:privateAdddressaddress;
要获取他的地址可以如下的方式来完成
数据复杂的bean里面带过来的如下:
-->
${p2.address.city}
<br/>
<%
ArrayListlist=newArrayList();
list.add(newPerson("wy"));
list.add(newPerson("wyy"));
list.add(newPerson("wyyy"));
request.setAttribute("list",list);
%>
<!--集合带过来的怎么获取数据呢?-->
${list[1].name}
<br/>
<%
Mapmap=newHashMap();
map.put("1",newPerson("aaaa"));
map.put("b",newPerson("bbbb"));
map.put("c",newPerson("cccc"));
map.put("d",newPerson("dddd"));
request.setAttribute("map",map);
%>
<!--
Map集合带过来的怎么获取数据呢?
map集合的数据存放的时候id一般不要用数字:会出现500错误
但是用数字的话获取方式如第二条
.号取不出来就用中括号[]
-->
${map.b.name}
${map['1'].name}
<br/>
<!--获取当前的web项目名-->
${pageContext.request.contextPath}
<ahref="${pageContext.request.contextPath}/demo1.jsp">点</a>
</body>
</html>
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述对大家JSP程序设计有所帮助。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短