字符 | 轉(zhuǎn)義后的字符 | ||
---|---|---|---|
HTML字符 | 字符編碼 | ||
and(和) | & | & | & |
單引號 | ' | ' | ' |
雙引號 | " | " | " |
大于號 | > | > | > |
小于號 | < | < | < |
web.xml <xwork> import com.opensymphony.xwork.ActionSupport; /**
<?xml version="1.0" encoding="GB18030"?>
<web-app version="2.4"
xmlns=" xmlns:xsi=" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"
<include file="webwork-default.xml"></include>
<package name="default" extends="webwork-default">
<action name="helloWorld" class="cn.javaworker.yeming.action.HelloWorld">
<result name="success">index.jsp</result>
<result name="input">hello.jsp</result>
</action>
</package>
</xwork>
hello.jsp
<%@ taglib prefix="ww" uri="/webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Enter you name</title>
</head>
<body>
<ww:form action="helloWorld">
<ww:textfield label="please enter user name" name="name"></ww:textfield>
<input type="submit"/>
</ww:form>
<form action="helloWorld.action">
please enter you name:<input type="text" name="name" value="<ww:property value="name"/>"/>
<input type="submit">
</form>
</body>
</html>
index.jsp
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head>
<title>hello page</title>
</head>
<body>
The message generated by my first action is :<ww:property value="message"/>
</body>
</html>
HelloWorld.java
package cn.javaworker.yeming.action;
* @author $author
*/
@SuppressWarnings("serial")
public class HelloWorld extends ActionSupport{
private String message;
private String name;
/**
* @see com.opensymphony.xwork.Action#execute()
*/
public String execute() throws Exception
{
if(name==null||name.equals("")||name.equals("world")){
addFieldError("name","blank names or names of 'world' are not allowed!");
return INPUT;
}
message ="hello "+name+"!\n";
message+="this is the time is: "+System.currentTimeMillis();
return SUCCESS;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
}
String s = new String("The Java platform is the ideal platform for network computing");
StringTokenizer st = new StringTokenizer(s);
System.out.println( "Token Total: " + st.countTokens() );
while( st.hasMoreElements() ){
System.out.println( st.nextToken() );
}
結(jié)果為:
Token Total: 10
The
Java
platform
is
the
ideal
platform
for
network
computing
String s = new String("The=Java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer st = new StringTokenizer(s,"=",true);
System.out.println( "Token Total: " + st.countTokens() );
while( st.hasMoreElements() ){
System.out.println( st.nextToken() );
}
結(jié)果為:
Token Total: 19
The
=
Java
=
platform
=
is
=
the
=
ideal
=
platform
=
for
=
network
=
computing
String question = new String("1+1=");
int answer = 3;
boolean result = (1+1==3);
StringBuffer sb = new StringBuffer();
sb.append(question);
sb.append(answer);
sb.append('\t');
sb.append(result);
System.out.println(sb);
結(jié)果為:
1+1=3 false
StringBuffer sb1 = new StringBuffer(5);
StringBuffer sb2 = new StringBuffer(5);
sb1.ensureCapacity(6);
sb2.ensureCapacity(100);
System.out.println( "sb1.Capacity: " + sb1.capacity() );
System.out.println( "sb2.Capacity: " + sb2.capacity() );
結(jié)果為:
sb1.Capacity: 12
sb2.Capacity: 100
StringBuffer sb = new StringBuffer("I love her!");
char[] i = {'I',' ','l','o','v','e',' ','y','o','u'};
sb.getChars(7,10,i,7);
System.out.println( "sb: " + sb );
結(jié)果為:sb: I love her!
StringBuffer sb = new StringBuffer("0123456789");
System.out.println( "sb.reverse(): " + sb.reverse() );
結(jié)果為:sb.reverse(): 9876543210
StringBuffer sb = new StringBuffer("0123456789");
sb.setLength(5);
System.out.println( "sb: " + sb );
結(jié)果為:sb: 01234
log4j.rootLogger=INFO, A1,A4
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p [%t] %c{2} (%M:%L) - %m%n
log4j.appender.A4=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A4.file=D:\\Java\\logs\\yeming.log
log4j.appender.A4.Encoding=GBK
log4j.appender.A4.DatePattern='.'yyyy-MM-dd
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
使用代碼:
package com.javawoker.yeming.jiemie.database;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
/**
?*@author 葉明 ---guming123416@gmail.com
?*@version $Id: v 1.01 2006/06/38 16:09:14 teodord Exp $
?*/
public class Pubconn {
?
?/*
? * 創(chuàng)建私有變量conn為數(shù)據(jù)庫連接對象中Connection
? * 創(chuàng)建私有變量dsye為數(shù)據(jù)庫連接池的DataSource
? */
?
?private Connection conn;
?private DataSource dsye;
?private static Logger log=Logger.getLogger(Pubconn.class);
?/*
? * 創(chuàng)建構(gòu)造函數(shù)Pubconn,在建立class中加載數(shù)據(jù)源的InitialContext
? */
?public Pubconn()
?{
??try{
???Context initCtx=new InitialContext();
???if(initCtx==null)
???{
????throw new Exception("不能加載文件Context");
???}
???dsye=(javax.sql.DataSource)initCtx.lookup("jdbc/yeming");
??}catch(Exception ex)
??{
???ex.printStackTrace();
???log.error("在加栽數(shù)據(jù)庫連接池時間發(fā)生錯誤"+ex.getMessage());
??}
?}
?/*
? * 從連接池中取出一條連接變量
? *
? */
?public Connection getConn()
?{
??try{
???conn=dsye.getConnection();
??}catch(SQLException ex)
??{
???ex.printStackTrace();
???log.error("獲得連接對象CONN時間發(fā)生錯誤"+ex.getMessage());
??}
??return conn;
?}
?/*
? * 關(guān)閉數(shù)據(jù)庫連接,釋放資源
? */
?public void closeConn(Connection conn)
?{
??try{
???if(conn!=null)
???{
????conn.close();
????conn=null;
???}
??}catch(SQLException ex)
??{
???ex.printStackTrace();
???log.error("關(guān)閉CONN時間發(fā)生錯誤"+ex.getMessage());
??}
?}
}
<script language="javascript">
var checkSubmitFlg = false;
function checkSubmit() {
if (checkSubmitFlg == true) {
return false;
}
checkSubmitFlg = true;
return true;
}
document.ondblclick = function docondblclick() {
window.event.returnValue = false;
}
document.onclick = function doconclick() {
if (checkSubmitFlg) {
window.event.returnValue = false;
}
}
</script>
<html:form action="myAction.do" method="post" onsubmit="return checkSubmit();">
2 還是javascript,將提交按鈕或者image置為disable
<html:form action="myAction.do" method="post"
onsubmit="getElById('submitInput').disabled = true; return true;">
<html:image styleId="submitInput" src="images/ok_b.gif" border="0" />
</html:form>
3 利用struts的同步令牌機制
利用同步令牌(Token)機制來解決Web應(yīng)用中重復(fù)提交的問題,Struts也給出了一個參考實現(xiàn)。
基本原理:
服務(wù)器端在處理到達的請求之前,會將請求中包含的令牌值與保存在當前用戶會話中的令牌值進行比較,看是否匹配。在處理完該請求后,且在答復(fù)發(fā)送給客戶端之前,將會產(chǎn)生一個新的令牌,該令牌除傳給客戶端以外,也會將用戶會話中保存的舊的令牌進行替換。這樣如果用戶回退到剛才的提交頁面并再次提交的話,客戶端傳過來的令牌就和服務(wù)器端的令牌不一致,從而有效地防止了重復(fù)提交的發(fā)生。
if (isTokenValid(request, true)) {
// your code here
return mapping.findForward("success");
} else {
saveToken(request);
return mapping.findForward("submitagain");
}
Struts根據(jù)用戶會話ID和當前系統(tǒng)時間來生成一個唯一(對于每個會話)令牌的,具體實現(xiàn)可以參考TokenProcessor類中的generateToken()方法。
1. //驗證事務(wù)控制令牌,<html:form >會自動根據(jù)session中標識生成一個隱含input代表令牌,防止兩次提交
2. 在action中:
//<input type="hidden" name="org.apache.struts.taglib.html.TOKEN"
// value="6aa35341f25184fd996c4c918255c3ae">
if (!isTokenValid(request))
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.transaction.token"));
resetToken(request); //刪除session中的令牌
3. action有這樣的一個方法生成令牌
protected String generateToken(HttpServletRequest request) {
HttpSession session = request.getSession();
try {
byte id[] = session.getId().getBytes();
byte now[] =
new Long(System.currentTimeMillis()).toString().getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(id);
md.update(now);
return (toHex(md.digest()));
} catch (IllegalStateException e) {
return (null);
} catch (NoSuchAlgorithmException e) {
return (null);
}
}
In this series of articles, web-tier proponents at Sun introduce the new concepts that every web-application developer should be familiar with to get the most out of the Java EE 5 web-tier technologies. This first article in the series gives an overview of the new features introduced in version 2.1 of JSP technology.
![]() |
Under the auspices of JSR 245 at the Java Community Process (JCP), the main focus of JSP 2.1 technology has been to provide a better alignment with the next release of JavaServer Faces technology, version 1.2.
The misalignment between the two technologies originated with the fact that version 1.0 of JavaServer Faces technology depended on JSP 1.2 technology. The reason is that the JSP 1.2 software was already widely available at the time, and the intention was to make the JavaServer Faces 1.0 interface more accessible to a broader audience. A consequence of this requirement was that JavaServer Faces technology could not take advantage of the EL introduced in the subsequent version of JSP technology, version 2.0. In addition, JSP 2.0 technology could not be modified to accommodate the needs of JavaServer Faces technology. And JSP 1.2 technology does not support an EL. Therefore, JavaServer Faces technology introduced an EL that was suited to its needs as a user interface (UI) component framework. As a result, page authors using JavaServer Faces technology tags with JSP technology code encountered some incompatibilities between the two technologies.
The expert groups have worked together on the upcoming releases of JSP 2.1 and JavaServer Faces 1.2 technologies in Java EE 5 to fix these integration issues and make sure that the two technologies work together seamlessly. One result is that all of the web-tier technologies now share a unified EL, allowing you to mix code from all of these technologies freely and without worry. This article provides an overview of the work that was done to improve the alignment of these technologies. It also explains the other minor improvements that made it into JSP 2.1 technology.
![]() |
The simple EL included in JSP 2.0 technology offers many advantages to the page author. Using simple expressions, page authors can easily access external data objects from their pages. The JSP technology container evaluates and resolves these expressions as it encounters them. It then immediately returns a response because the JSP request-processing model has only one phase, the render
phase. However, because the request-processing model does not support a postback
, all JSP expressions are read-only.
Unlike JSP technology, JavaServer Faces technology supports a multiphase life cycle. When a user enters values into the JavaServer Faces UI components and submits the page, those values are converted, validated, and propagated to server-side data objects, after which component events are processed. In order to perform all these tasks in an orderly fashion, the JavaServer Faces life cycle is split into separate phases. Therefore, JavaServer Faces technology evaluates expressions at different phases of the life cycle rather than immediately, as JSP technology would do. Additionally, the expressions can be used to set as well as get data, so that the values a user enters into the UI components are propagated to server-side objects during a postback
. Finally, some JavaServer Faces technology expressions can invoke methods on server-side objects during various stages of the life cycle in order to validate data and handle component events.
Each EL suited the needs of its respective technology very well. When using both JSP technology tags or template text and JavaServer Faces technology tags, however, page authors would expose incompatibilities between the ELs. One example involves using JavaServer Faces components inside the c:forEach
tag, as in the following example:
<c:forEach var="location" items="${handler.locations}"> |
The problem with this code stems from the fact that the iteration variable location
is visible only within the boundaries of its iteration tag. This means that on a postback
, the JavaServer Faces request life cycle has no way to access the value associated with the variable location
.
To solve problems such as these, the expert groups for JavaServer Faces and JSP technologies decided to unify the two ELs. They created a more powerful EL, the unified EL, that supports the following features:
With the addition of these features into a unified EL, the incompatibility problems such as that illustrated by the preceding c:forEach
code example can be solved. Now, the iteration variable can be a deferred expression that refers to the proper object within the collection being iterated over -- for example, locations[0]
. This way, the deferred expression can be evaluated later on during the JavaServer Faces request life cycle, as shown in the following code:
<c:forEach var="location" items="#{locations}"> |
For more details on changes made to the c:forEach
tag and other JSTL tags, please see the upcoming article of this series, which will describe new features in JSTL. If you are a page author, make sure to read another upcoming article in the series, which describes the differences between the two EL syntaxes now supported in JSP technology. The article "Unified Expression Language (EL)" provides a much more detailed description. It also explains how to create a custom resolver, which is one of the new features of the unified EL.
It is also important to note that the EL is useful beyond all of the web technology specifications. This is why the EL is agnostic of the technology hosting it and is currently defined through its own independent document within the JSP specification. This makes it clear that the EL is not dependent on the JSP specification and might therefore have its own JSR in the future.
![]() |
Prior to the Java EE 5 platform, accessing data sources, web services, environment entries, and Enterprise JavaBeans (EJB) bean references required the use of the Java Naming and Directory Interface (JNDI) API along with the declaration of entries into a deployment descriptor. Thanks to injection annotations, it is now possible to inject container-managed objects without having to write the traditional boilerplate code and deal with the deployment descriptor.
For example, a tag handler could access a database through the following code:
@Resource(name="jdbc/myDB") |
The field myDB
of type javax.sql.DataSource
is annotated with @Resource
and is injected by the container prior to the tag handler being made available to the application. The data source JNDI mapping is inferred from the field name catalogDS
and type javax.sql.DataSource
. Moreover, the myDB
resource no longer needs to be defined in the deployment descriptor. An upcoming article in this series will provide more detail on how to perform injection with web-tier applications.
TrimWhiteSpace
![]() |
A frequent complaint in user forums concerns the large number of blank lines, or white spaces, that often clutter the output of a JSP technology page. For example, consider this page:
<%@page contentType="text/html"%> |
Prior to JSP 2.1 technology, this page would produce the following output in the response (where ?
represents the end of a blank line):
? |
Although these extra blank lines do not change the way a browser displays the output page, they do add extra payload to the response and make the HTML source code less legible. To solve this issue, JSP 2.1 technology now offers the configuration parameter trimDirectiveWhitespaces
, which can be specified as a page directive or as a property-group configuration parameter that applies to a group of pages.
With trimDirectiveWhitespaces
enabled, template text containing only blank lines, or white space, is removed from the response output. In the code example above, the lines containing only blank lines would be removed, yielding the following output:
<html> |
![]() |
Maintaining compatibility with earlier versions is critical for any Java EE platform release. For JSP 2.1 technology, the only backward compatibility issue involves the EL's new support of the #{
syntax that refers to deferred expressions. The #{
character sequence can create backward compatibility problems in two situations: when the sequence is included in template text or in attribute values.
In JSP 2.1 technology, a deferred expression does not make sense in the context of template text because evaluation of an expression is always done immediately by the JSP container. Because no entity other than the JSP container processes template text, a deferred expression would always be left unevaluated if it were included in template text. Therefore, any page authors who have included the #{
character sequence in template text probably meant to use the ${}
syntax instead. Acknowledging this fact, the JSP specification authors have mandated that the inclusion of #{
in template text triggers a translation error in JSP 2.1 technology.
With respect to tag attribute values, it is important to note that tag libraries based on JSP versions prior to 2.1 (such as the 1.1 version of the JavaServer Faces component tag library) made extensive use of the #{
syntax to specify deferred expressions. Because these tag libraries must be responsible for the parsing of these deferred expressions, they must execute in an environment in which the #{
character sequence is processed as a string literal.
To determine whether or not to treat the #{
character sequence as a string literal, the web container relies on the JSP version indicated by the tag library descriptor (TLD) associated with the tag library. The JSP version specified in the TLD tells the web container which version of the JSP specification the tag library is written for. JSP versions previous to 2.1 processed #{
as a string literal, as did applications based on those earlier versions. With version 2.1 or later, the character sequence #{
represents a deferred expression, assuming the attribute has been declared to support deferred expressions in the TLD. If the attribute does not support deferred expressions, then the presence of the #{
character sequence results in a translation error.
Web-application developers who have developed applications for a version prior to JSP 2.1 technology and want to deploy and run them on a JSP 2.1 container must take the following actions to ensure that the container handles any instances of the #{
character sequence correctly:
#{
characters using a backslash: \#{
.#{
character sequence as a string literal by setting the JSP property groups element deferred-syntax-allowed-as-literal
to true
or by setting the page/tag-file
directive attribute deferredSyntaxAllowedAsStringLiteral
to true
. Aside from the incompatibilities relating to the #{
character sequence, all JSP 2.0 technology applications will run as is on JSP 2.1 containers. However, if a web application uses third-party tag libraries that are based on JavaServer Faces 1.1 technology or earlier, some new features provided by JSP 2.1 and JavaServer Faces 1.2 technologies will not be available with these libraries. For example, although EL functions may now be used in the JavaServer Faces 1.2 core and HTML tag libraries, they cannot be used with third-party tag libraries that are based on JavaServer Faces 1.1 technology and earlier, because these libraries run on a version of the JavaServer Faces EL that did not support EL functions.
Taglib
Order of Precedence![]() |
Now that JavaServer Faces technology and JSTL are part of the Java EE 5 platform, they take precedence over any other version of these libraries that could be bundled with a web application. Some people may ask: "But what if I want to replace the container's implementation of these technologies with another implementation?" This is no longer possible. This would be like replacing the implementation of the java.util package
in the Java Platform, Standard Edition (Java SE, formerly referred to as J2SE), a fruitless task. The same applies from now on with platform tag libraries.
Essentially, applications should not replace the APIs that the Java EE 5 platform requires. If the platform architects really wanted to make this possible, they would have designed the APIs with service-provider interfaces to allow applications to easily swap in their own implementations. However, this was not their intention, and applications will always have to rely on the platform tag libraries bundled with a container.
![]() |
Let's look at a few other minor additions and clarifications to the JSP 2.1 specification. For a detailed list of all changes between JSP 2.0 and JSP 2.1, please consult Appendix E of the specification. Most notable are the following:
IIS6.0與Resin_3.0.8的整合困擾了我好幾天。現(xiàn)在寫出來跟大家交流一下。有什么錯誤請大家指出來。
ResultSet WSrc = WSc.getColumns (... "UnknownTable" ...); // getColumns()會發(fā)出一個查詢給數(shù)據(jù)庫系統(tǒng) . . . WSrc.next(); string Cname = getString(4); . . . // 用戶必須從反復(fù)從服務(wù)器獲取N行數(shù)據(jù) // N = UnknownTable的列數(shù) |
// 準備'啞元'查詢 PreparedStatement WSps = WSc.prepareStatement ("SELECT * from UnknownTable WHERE 1 = 0"); // 查詢從來沒有被執(zhí)行,只是被預(yù)儲 ResultSetMetaData WSsmd=WSps.getMetaData(); int numcols = WSrsmd.getColumnCount(); ... int ctype = WSrsmd.getColumnType(n) ... // 獲得了列的完整信息 |
字符 | 含義 |
---|---|
\cx | 匹配由x指明的控制字符。例如, \cM 匹配一個 Control-M 或回車符。x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一個原義的 'c' 字符。 |
\f | 匹配一個換頁符。等價于 \x0c 和 \cL。 |
\n | 匹配一個換行符。等價于 \x0a 和 \cJ。 |
\r | 匹配一個回車符。等價于 \x0d 和 \cM。 |
\s | 匹配任何空白字符,包括空格、制表符、換頁符等等。等價于 [ \f\n\r\t\v]。 |
\S | 匹配任何非空白字符。等價于 [^ \f\n\r\t\v]。 |
\t | 匹配一個制表符。等價于 \x09 和 \cI。 |
\v | 匹配一個垂直制表符。等價于 \x0b 和 \cK。 |
特別字符 | 說明 |
---|---|
$ | 匹配輸入字符串的結(jié)尾位置。如果設(shè)置了 RegExp 對象的 Multiline 屬性,則 $ 也匹配 '\n' 或 '\r'。要匹配 $ 字符本身,請使用 \$。 |
( ) | 標記一個子表達式的開始和結(jié)束位置。子表達式可以獲取供以后使用。要匹配這些字符,請使用 \( 和 \)。 |
* | 匹配前面的子表達式零次或多次。要匹配 * 字符,請使用 \*。 |
+ | 匹配前面的子表達式一次或多次。要匹配 + 字符,請使用 \+。 |
. | 匹配除換行符 \n之外的任何單字符。要匹配 .,請使用 \。 |
[ | 標記一個中括號表達式的開始。要匹配 [,請使用 \[。 |
? | 匹配前面的子表達式零次或一次,或指明一個非貪婪限定符。要匹配 ? 字符,請使用 \?。 |
\ | 將下一個字符標記為或特殊字符、或原義字符、或向后引用、或八進制轉(zhuǎn)義符。例如, 'n' 匹配字符 'n'。'\n' 匹配換行符。序列 '\\' 匹配 "\",而 '\(' 則匹配 "("。 |
^ | 匹配輸入字符串的開始位置,除非在方括號表達式中使用,此時它表示不接受該字符集合。要匹配 ^ 字符本身,請使用 \^。 |
{ | 標記限定符表達式的開始。要匹配 {,請使用 \{。 |
| | 指明兩項之間的一個選擇。要匹配 |,請使用 \|。 |
字符 | 描述 |
---|---|
* | 匹配前面的子表達式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等價于{0,}。 |
+ | 匹配前面的子表達式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等價于 {1,}。 |
? | 匹配前面的子表達式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等價于 {0,1}。 |
{n} | n 是一個非負整數(shù)。匹配確定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的兩個 o。 |
{n,} | n 是一個非負整數(shù)。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等價于 'o+'。'o{0,}' 則等價于 'o*'。 |
{n,m} | m 和 n 均為非負整數(shù),其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 將匹配 "fooooood" 中的前三個 o。'o{0,1}' 等價于 'o?'。請注意在逗號和兩個數(shù)之間不能有空格。 |
操作符 | 描述 |
---|---|
\ | 轉(zhuǎn)義符 |
(), (?:), (?=), [] | 圓括號和方括號 |
*, +, ?, {n}, {n,}, {n,m} | 限定符 |
^, $, \anymetacharacter | 位置和順序 |
| | “或”操作 |
字符 | 描述 |
---|---|
\ | 將下一個字符標記為一個特殊字符、或一個原義字符、或一個 向后引用、或一個八進制轉(zhuǎn)義符。例如,'n' 匹配字符 "n"。'\n' 匹配一個換行符。序列 '\\' 匹配 "\" 而 "\(" 則匹配 "("。 |
^ | 匹配輸入字符串的開始位置。如果設(shè)置了 RegExp 對象的 Multiline 屬性,^ 也匹配 '\n' 或 '\r' 之后的位置。 |
$ | 匹配輸入字符串的結(jié)束位置。如果設(shè)置了RegExp 對象的 Multiline 屬性,$ 也匹配 '\n' 或 '\r' 之前的位置。 |
* | 匹配前面的子表達式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等價于{0,}。 |
+ | 匹配前面的子表達式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等價于 {1,}。 |
? | 匹配前面的子表達式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等價于 {0,1}。 |
{n} | n 是一個非負整數(shù)。匹配確定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的兩個 o。 |
{n,} | n 是一個非負整數(shù)。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等價于 'o+'。'o{0,}' 則等價于 'o*'。 |
{n,m} | m 和 n 均為非負整數(shù),其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 將匹配 "fooooood" 中的前三個 o。'o{0,1}' 等價于 'o?'。請注意在逗號和兩個數(shù)之間不能有空格。 |
? | 當該字符緊跟在任何一個其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面時,匹配模式是非貪婪的。非貪婪模式盡可能少的匹配所搜索的字符串,而默認的貪婪模式則盡可能多的匹配所搜索的字符串。例如,對于字符串 "oooo",'o+?' 將匹配單個 "o",而 'o+' 將匹配所有 'o'。 |
. | 匹配除 "\n" 之外的任何單個字符。要匹配包括 '\n' 在內(nèi)的任何字符,請使用象 '[.\n]' 的模式。 |
(pattern) | 匹配 pattern 并獲取這一匹配。所獲取的匹配可以從產(chǎn)生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中則使用 $0…$9 屬性。要匹配圓括號字符,請使用 '\(' 或 '\)'。 |
(?:pattern) | 匹配 pattern 但不獲取匹配結(jié)果,也就是說這是一個非獲取匹配,不進行存儲供以后使用。這在使用 "或" 字符 (|) 來組合一個模式的各個部分是很有用。例如, 'industr(?:y|ies) 就是一個比 'industry|industries' 更簡略的表達式。 |
(?=pattern) | 正向預(yù)查,在任何匹配 pattern 的字符串開始處匹配查找字符串。這是一個非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。預(yù)查不消耗字符,也就是說,在一個匹配發(fā)生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預(yù)查的字符之后開始。 |
(?!pattern) | 負向預(yù)查,在任何不匹配 pattern 的字符串開始處匹配查找字符串。這是一個非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。預(yù)查不消耗字符,也就是說,在一個匹配發(fā)生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預(yù)查的字符之后開始 |
x|y | 匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 則匹配 "zood" 或 "food"。 |
[xyz] | 字符集合。匹配所包含的任意一個字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。 |
[^xyz] | 負值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。 |
[a-z] | 字符范圍。匹配指定范圍內(nèi)的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范圍內(nèi)的任意小寫字母字符。 |
[^a-z] | 負值字符范圍。匹配任何不在指定范圍內(nèi)的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范圍內(nèi)的任意字符。 |
\b | 匹配一個單詞邊界,也就是指單詞和空格間的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。 |
\B | 匹配非單詞邊界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。 |
\cx | 匹配由 x 指明的控制字符。例如, \cM 匹配一個 Control-M 或回車符。x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一個原義的 'c' 字符。 |
\d | 匹配一個數(shù)字字符。等價于 [0-9]。 |
\D | 匹配一個非數(shù)字字符。等價于 [^0-9]。 |
\f | 匹配一個換頁符。等價于 \x0c 和 \cL。 |
\n | 匹配一個換行符。等價于 \x0a 和 \cJ。 |
\r | 匹配一個回車符。等價于 \x0d 和 \cM。 |
\s | 匹配任何空白字符,包括空格、制表符、換頁符等等。等價于 [ \f\n\r\t\v]。 |
\S | 匹配任何非空白字符。等價于 [^ \f\n\r\t\v]。 |
\t | 匹配一個制表符。等價于 \x09 和 \cI。 |
\v | 匹配一個垂直制表符。等價于 \x0b 和 \cK。 |
\w | 匹配包括下劃線的任何單詞字符。等價于'[A-Za-z0-9_]'。 |
\W | 匹配任何非單詞字符。等價于 '[^A-Za-z0-9_]'。 |
\xn | 匹配 n,其中 n 為十六進制轉(zhuǎn)義值。十六進制轉(zhuǎn)義值必須為確定的兩個數(shù)字長。例如,'\x41' 匹配 "A"。'\x041' 則等價于 '\x04' & "1"。正則表達式中可以使用 ASCII 編碼。. |
\num | 匹配 num,其中 num 是一個正整數(shù)。對所獲取的匹配的引用。例如,'(.)\1' 匹配兩個連續(xù)的相同字符。 |
\n | 標識一個八進制轉(zhuǎn)義值或一個向后引用。如果 \n 之前至少 n 個獲取的子表達式,則 n 為向后引用。否則,如果 n 為八進制數(shù)字 (0-7),則 n 為一個八進制轉(zhuǎn)義值。 |
\nm | 標識一個八進制轉(zhuǎn)義值或一個向后引用。如果 \nm 之前至少有 nm 個獲得子表達式,則 nm 為向后引用。如果 \nm 之前至少有 n 個獲取,則 n 為一個后跟文字 m 的向后引用。如果前面的條件都不滿足,若 n 和 m 均為八進制數(shù)字 (0-7),則 \nm 將匹配八進制轉(zhuǎn)義值 nm。 |
\nml | 如果 n 為八進制數(shù)字 (0-3),且 m 和 l 均為八進制數(shù)字 (0-7),則匹配八進制轉(zhuǎn)義值 nml。 |
\un | 匹配 n,其中 n 是一個用四個十六進制數(shù)字表示的 Unicode 字符。例如, \u00A9 匹配版權(quán)符號 (?)。 |
正則表達式 | 說明 |
---|---|
/\b([a-z]+) \1\b/gi | 一個單詞連續(xù)出現(xiàn)的位置 |
/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/ | 將一個URL解析為協(xié)議、域、端口及相對路徑 |
/^(?:Chapter|Section) [1-9][0-9]{0,1}$/ | 定位章節(jié)的位置 |
/[-a-z]/ | A至z共26個字母再加一個-號。 |
/ter\b/ | 可匹配chapter,而不能terminal |
/\Bapt/ | 可匹配chapter,而不能aptitude |
/Windows(?=95 |98 |NT )/ | 可匹配Windows95或Windows98或WindowsNT,當找到一個匹配后,從Windows后面開始進行下一次的檢索匹配。 |
一 準備 當然,在生產(chǎn)環(huán)境中這樣做是不行的,需要的在 DNS 上做相應(yīng)的域名解析。 將 tomcat 目錄下的 webapps 目錄在同一目錄復(fù)制一份,目錄名分為 webapps2 ,然后將 最后,寫一個簡單 html 文件用于測試,文件名為 test.html ,文件內(nèi)容如下: <HTML> <BODY> 至此,前期的準備工作做完了,全是一些體力活。 配置 www.sentom1.net 虛擬主機 打開 tomcat/conf/server.xml 文件,將 Host <Logger className="org.apache.catalina.logger.FileLogger" </Host> 將下面內(nèi)容追加到 Host 元素后面,注意 Host 元素中 name 屬性和 appBase 屬性的值的變化。 <Logger className="org.apache.catalina.logger.FileLogger" </Host> |