Regular expressions are learning - from a URL in the access to all the hyperlinks
Abstract: Regular expressions are learning - from a URL in the access to all the hyperlinks
</ Td> </ tr> <tr> <td width="489" height="35" valign="top" class="ArticleTeitle"> The following example shows how to use a regular expression from a URL and find all similar output below hyperlink:
First, we enter the URL from the command line, open the input flow, and read the contents of URL into htmlString deposited in the string. Then "( ]*>)" Tectonic the regular expression, in the end find htmlString string matching string.
Import java.io. *;
Import java.net .*;
Import java.util.regex .*;
(Public class GetHref
Public static void main (String [] args) (
InputStream in = null;
PrintWriter out = null;
String htmlString = null;
Try (
/ / Check the arguments
If ((args.length! = 1) & (args.length! = 2))
Throw new IllegalArgumentException ( "Wrong number of args");
/ / Set up the streams
URL url = new URL (args [0]) / / Create the URL
Url.openStream in = () / / Open a stream to it
If (args.length == 2) / / Get an appropriate output stream
Out = new PrintWriter (new FileWriter (args [1]));
BufferedReader bin = new BufferedReader (new InputStreamReader (in));
String line;
StringBuffer sb = new StringBuffer ();
While ((bin.readLine ())!= line = null) (
If (out! = Null) out.println (line);
Sb = sb.append (line);
)
HtmlString = sb.toString ();
/ / System.out.println (sb.toString ());
)
/ / On exceptions, the print error message and usage message.
Catch (Exception e) (
System.err.println (e);
System.err.println ( "Usage: java GetURL
)
Finally (/ / Always close the streams, no matter what.
Try (in.close (); out.close ();) catch (Exception e) ()
)
Pattern p = Pattern.compile ( "( ]*>)");
Matcher m = p.matcher (htmlString);
Boolean result = m.find ();
While (result) (
For (int i = 1; i <= m.groupCount (); i + +) (
System.out.println (m.group (i));
)
Result = m.find ();
)
)
)
Running Results:
C: \ java> java GetHref http://127.0.0.1:8080/zz3zcwbwebhome/index.jsp
W.zzedu.gov.cn ',' java learning room ') ">
Page) 'this.setHomePage (' http://10.10.1.1/index.jsp');">
…………….
</ Td> <td width="186" align="center" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
Create directories in JSP
Abstract: to create directories in JSP
</ Td> </ tr> <tr> <td width="540" height="35" valign="top" class="ArticleTeitle"> <% –
Author: Hua Lian [hlian@alleasy.net]
Date: 2000-07-20-10-10
Version: 1.0
Features: JSP routines - to create directories in JSP
Call:
Paper trails path
Mkdir (String path)
Back to:
Air operations for the successful return
Operation failed to return to the reasons for failure.
If there is the question to invite feedback to hlian@alleasy.net.
–%>
<% @ Page contentType = "text / html; gb2312 charset ="%>
<% @ Page import = "java.io. *"%>
<%!
String Mkdir (String path) (
String msg = null;
Java.io.File dir;
/ / Object new document
Dir = new java.io.File (path);
If (dir == null) (
Msg = "wrong reasons:
Sorry, can not create space directory! ";
Return msg;
)
If (dir.isFile ()) (
Msg = "wrong reasons:
Have the same name document "+ dir.getAbsolutePath () +" there. ";
Return msg;
)
If (! Dir.exists ()) (
Boolean result = dir.mkdirs ();
If (result == false) (
Msg = "wrong reasons:
Contents "+ dir.getAbsolutePath () +" failure to create, for unknown reasons! ";
Return msg;
)
/ / Create directories, if successful, no output.
Msg = "successfully created directory:" + dir.getAbsolutePath () + "";
Return msg;
Else ()
Msg = "wrong reasons:
Contents "+ dir.getAbsolutePath () +" already exists. ";
)
Return msg;
)
%>
<%
String filepath = "usr / home / hoyi / html / dir";
String opmsg = Mkdir (filepath);
Out.println (opmsg);
%>
</ Td> <td width="173" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
How to avoid duplication
Abstract: How to avoid duplication
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> More Java Pitfalls This is the Chinese version of an example.
First, what is to repeat
Imagine a visit of a very big booking sites when booking tickets click on the submit button, the server may be slow processing speed, tickets still can not see the result that the failure of the request to buy their own, so once again and over click Submit button, and if not addressed, these requests will be dealt with the server, which lead to false results.
Second, to prevent repeat
Web.xml looked at (the examples of directories and files download)
<? Xml version = "1.0">
Web-app PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<table Width="732" border="0"> <tr> <td width="442"> Id Id
</ Td> <td width="280"> </ td> </ tr> </ table>
Look at the form ticketForm.jsp
<% @ Page contentType = "text / html; GBK charset ="%>
<%
/ / The first time, the target set in a session id attribute to the success will be deleted. After the submission of this test will be whether the attribute value is null judge to repeat.
If (session.isNew ()) (
Session.setAttribute ( "id", session.getId ());
)
%>
Ozzie concerts online booking
<form Method="get" action="/resubmit/ControllerServlet">
<table Border="1" width="50%">
<tr>
<td> The number of votes: </ td>
<td>
<select Name="numTickets">
<option Value="Please enter a Ticket #"> Please enter a Ticket # </ option>
<Option value = "1 Ticket" <% if (resubmit.getNumTickets () == "a Ticket") out.println ( "SELECTED");%>> 1 Ticket </ option>
<Option value = "2 Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 2 Tickets </ option>
<Option value = "Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 3 Tickets </ option>
<Option value = "Tickets" <% if (resubmit.getNumTickets () == "Tickets") out.println ( "SELECTED");%>> 4 Tickets </ option>
<Option value = "5 Tickets" <% if (resubmit.getNumTickets () == "5 Tickets") out.println ( "SELECTED");%>> 5 Tickets </ option>
<Option value = "6 Tickets" <% if (resubmit.getNumTickets () == "6 Tickets") out.println ( "SELECTED");%>> 6 Tickets </ option>
<Option value = "7 Tickets" <% if (resubmit.getNumTickets () == "7 Tickets") out.println ( "SELECTED");%>> 7 Tickets </ option>
<Option value = "8 Tickets" <% if (resubmit.getNumTickets () == "8 Tickets") out.println ( "SELECTED");%>> 8 Tickets </ option>
<Option value = "9 Tickets" <% if (resubmit.getNumTickets () == "Tickets 9") out.println ( "SELECTED");%>> 9 Tickets </ option>
</ Select>
</ Td>
</ Tr>
<tr>
<td> Stadium Level: </ td>
<td>
<select Name="stadiumTier">
<option Value="Please enter a Stadium Tier"> Please enter a Stadium Tier </ option>
<Option value = "Tier A" <% if (resubmit.getStadiumTier () == "Tier A") out.println ( "SELECTED");%>> Tier A </ option>
<Option value = "Tier B" <% if (resubmit.getStadiumTier () == "Tier B") out.println ( "SELECTED");%>> Tier B </ option>
<Option value = "Tier C" <% if (resubmit.getStadiumTier () == "Tier C") out.println ( "SELECTED");%>> Tier C </ option>
<Option value = "Tier D" <% if (resubmit.getStadiumTier () == "Tier D") out.println ( "SELECTED");%>> Tier D </ option>
<Option value = "Tier E" <% if (resubmit.getStadiumTier () == "Tier E") out.println ( "SELECTED");%>> Tier E </ option>
</ Select>
</ Td>
</ Tr>
<tr>
<td> Votes Price: </ td>
<td>
<select Name="ticketPrice">
<option Value="Please enter a Ticket Price"> Please enter a Ticket Price </ option>
<Option value = "$ 12" <% if (resubmit.getTicketPrice () == "$ 12") out.println ( "SELECTED");%>> $ 12 </ option>
<Option value = "$ 17" <% if (resubmit.getTicketPrice () == "$ 17") out.println ( "SELECTED");%>> $ 17 </ option>
<Option value = "$ 25" <% if (resubmit.getTicketPrice () == "$ 25") out.println ( "SELECTED");%>> $ 25 </ option>
<Option value = "$ 35" <% if (resubmit.getTicketPrice () == "$ 35") out.println ( "SELECTED");%>> $ 35 </ option>
<Option value = "$ 50" <% if (resubmit.getTicketPrice () == "$ 50") out.println ( "SELECTED");%>> $ 50 </ option>
</ Select>
</ Td>
</ Tr>
<tr>
<td Colspan="2" align="center"> <input TYPE="submit" VALUE="Submit"> </ td>
</ Tr>
</ Table>
</ Form>
</ Body>