Use JSP to obtain the webpage source file and capture the link address

Source: Internet
Author: User

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> ");
}
%>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.