程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP中正則表達式用法實例

JSP中正則表達式用法實例

編輯:關於JSP

本文實例講述了JSP中正則表達式用法。分享給大家供大家參考,具體如下:

<%@ page language="java" import="java.util.*,cn.com.Person,cn.com.Adddress" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">
 <title>My JSP 'El.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>
 <body>
 <%
  String data="assd";
  request.setAttribute("data", data);
 %>
 <!-- 正則表達式,對於 Request傳過來的數據可以直接如下顯示
   相當於:pageContext.findAttribute("data");
   這是一般的方式
  -->
 ${data }
 <br/>
 <%
  Person p=new Person();
  p.setName("name");
  request.setAttribute("person", p); 
 %>
 <!-- 反射name屬性獲取這個值輸出 
  數據通過JAVABean裡傳過來的如下:
 -->
 ${person.name}
 <br/>
 <%
  Person p2=new Person();
  Adddress add=new Adddress();
  add.setCity("NewYork");
  p2.setAddress(add);
  request.setAttribute("p2", p2);
 %>
 <!-- 下面的是定義一個Person的類和一個Adddress的類
  Person中私有屬性:private Adddress address;
  要獲取他的地址可以如下的方式來完成
  數據復雜的bean裡面帶過來的如下:
 -->
 ${p2.address.city}
 <br/>
 <%
  ArrayList list=new ArrayList();
  list.add(new Person("wy"));
  list.add(new Person("wyy"));
  list.add(new Person("wyyy"));
  request.setAttribute("list", list);
 %>
 <!-- 集合帶過來的怎麼獲取數據呢? -->
 ${list[1].name }
 <br/>
 <%
  Map map=new HashMap();
  map.put("1", new Person("aaaa"));
  map.put("b", new Person("bbbb"));
  map.put("c", new Person("cccc"));
  map.put("d", new Person("dddd"));
  request.setAttribute("map", map);
 %>
 <!-- 
   Map集合帶過來的怎麼獲取數據呢?
   map集合的數據存放的時候id一般不要用數字:會出現500錯誤
   但是用數字的話獲取方式如第二條
   .號取不出來就用中括號[]
 -->
 ${map.b.name}
 ${map['1'].name }
 <br/>
 <!-- 獲取當前的web項目名 -->
 ${pageContext.request.contextPath}
  <a href="${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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved