The java net package is used to obtain the webpage source file and the regular expression is used to capture the link address. Because the regular expression is not skillful in learning, the following example cannot capture the link address in the href attribute in all cases.
Test. jsp (preferred for SUN Enterprise applications)
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. util. regex. *" errorPage = "" %>
<%
String sCurrentLine;
String sTotalString;
SCurrentLine = "";
STotalString = "";
Java. io. InputStream l_urlStream;
Java.net. URL l_url = new java.net. URL ("http://www.5ja.net ");
Java.net. HttpURLConnection l_connection = (java.net. HttpURLConnection) l_url.openConnection ();
Rochelle connection.connect ();
Rochelle urlstream = Rochelle connection.getinputstream ();
Java. io. BufferedReader l_reader = new java. io. BufferedReader (new java. io. InputStreamReader (l_urlStream ));
While (sCurrentLine = l_reader.readLine ())! = Null)
{
STotalString + = sCurrentLine;
}
// String regEx = "href = ([^"] *)> ";
String regEx = "href =" ([^ "] *)" "; // find the href =" ***** "Link
Pattern p = Pattern. compile (regEx, Pattern. CASE_INSENSITIVE );
Matcher m = p. matcher (sTotalString );
Int j = 0;
While (m. find ()){
J ++;
Out. println ("m. group (" + j + "):" + m. group (0) + "<br> ");
}
RegEx = "href = ([^] *)"; // find the href = *** Link
P = Pattern. compile (regEx, Pattern. CASE_INSENSITIVE); // Pattern. CASE_INSENSITIVE searches case-insensitive
M = p. matcher (sTotalString );
While (m. find ()){
J ++;
Out. println ("m. group (" + j + "):" + m. group (0) + "<br> ");
}
%>