15 to 18 identity card numbers of JSP procedures
Abstract: 15 to 18 identity card numbers of JSP procedures
</ Td> </ tr> <tr> <td width="524" height="35" valign="top" class="ArticleTeitle"> identity card number from 17 yards and a digital body check code composition, the order from left to right: six-digit code,
Eight-digit date of birth code, the three-digit sequence code and a check digit code. Below are the 15 to 18 identity card numbers
The JSP procedures. Excerpt from "software" in 2005 the first three. Author: Chao Wei-wu
(1) ID15to18.html
<head>
</ Head>
<body>
<form Name="form1" method="post" action="ID15to18.jsp">
Please enter a 15 NRIC: <input type = "text" name = "id">
<input Type="submit" name="Submit" value="转æ¢">
</ Form>
</ Body>
(2) ID15to18.jsp procedures are as follows:
<% @ Page import = "java.util .*" contentType = "text / html; GBK charset ="%>
<%
String id = "" id18 = "";
Id = request.getParameter ( "id"). Trim ();
Id18 = changeID (id);
%>
You enter the ID number is: <% = id%>
After the switch to 18 is: <% = id18%>
<%!
Public static String changeID (String ID15) (
String ID18 = "";
If (ID15.length () == 18) (
ID18 = ID15.toUpperCase ();
)
If (ID15.length () == 15) (
Int [] 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1 w = ();
Char [] = ('1 A ','0', 'X','9 ','8','7 ','6','5 ','4','3 ','2') ;
String ID17 = ID15.substring (0,6) + "19" + ID15.substring (6,15);
Int [] ID17Array;
ID17Array = new int [17];
For (int i = 0; i <17; i + +) (
ID17Array [i] = Integer.parseInt (ID17.substring (i, i +1));
)
Int s = 0;
For (int i = 0; i <17; i + +) (
ID17Array s = s + w [i] * [i];
)
S% s = 11;
ID18 = + Character.toString ID17 (A [s]);
)
Return ID18;
)
%>
The result: Click watching the demonstrations.
You enter the ID number is: 110101710307405
After the switch to 18: 110101197103074053
</ Td> <td width="177" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
A check e-mail address is the effectiveness of the procedures for regular expression
Abstract: A check e-mail address the effectiveness of the procedures for the regular expression
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
This article is reproduced multiple Internet, such as China's software: http://dev.csdn.net/article/27/27762.shtm, but the text of a regular expression "\ \ w + @ (\ \ w +.) + [az] (2,3) "I think it should be changed to:" \ \ w + @ (\ \ w + \ \.) + [az] (2,3), "because". "can represent any character, is the yuan characters need to escape. Otherwise, like the following e-mail addresses so that the address is valid.
C: \ java> java Test zz3zcwb@sina.com.cn.net34523542ffrdasfafasfsaf
Valid e-mail addresses
Below is the text:
Import java.util.regex.Pattern;
Import java.util.regex.Matcher;
Public class Test (
Public static void main (String [] args) (
If (args.length! = 1) (
System.out.println ( "Please enter a parameter!");
System.exit (0);
)
/ / Here and I have to back up, which read: Pattern p = Pattern.compile ( "\ \ w + @ (\ \ w +.)+[ az] (2,3)");
Pattern p = Pattern.compile ( "\ \ w + @ (\ \ w + \ \.) + [Az] (2,3)");
Matcher m = p.matcher (args [0]);
Boolean b = m.matches ();
If (b) (
System.out.println ( "valid e-mail addresses");
Else ()
System.out.println ( "invalid email address");
)
)
)
<table Width="661" border="0"> <tr> <td width="330">
Implementation:
C: \ java> java Test abc@def.nujpt.edu.cn
Valid e-mail addresses
C: \ java> java Test abc @ def
Invalid e-mail address
C: \ java> java Test abc@de_f.com.cn
Valid e-mail addresses
C: \ java> java Test abc@de_f.com.cn
Valid e-mail addresses
C: \ java> java Test abc@de_f.com.cn3
Invalid e-mail address
C: \ java> java Test abc@de_f.com.cncom
Invalid e-mail address
</ Td> <td width="321"> </ td> </ tr> </ table>
C: \ java> java Test ab_c@de_f.com.cncom
Invalid e-mail address
C: \ java> java Test ab_c@de_f.com.cn
Valid e-mail addresses
C: \ java>
Notes:
"\ \ W + @ (\ \ w + \ \.) + [Az] (2,3)"
In the regular expression in the \ w characters that arbitrary words, the scope of az, AZ, 0-10-9 _ because of java \ is originally escaped symbols, if only for the write \ w Ambiguity may occur, or even wrong, So write to: \ \ w
+ Means that more than once, so \ \ w + on behalf of arbitrary length of the string, but does not include other special characters, such as _,-,$,&,*, huh, if the e-mail really wants to completely effective check it on a regular expression is not so long, huh, interested can read their own Write.
I would like to comment on the back of simple, @ must appear, but only once, @ written directly on the line.
\ \ W + \ \. Arbitrary strings behind the DOT, we all know that this is the characteristics of domain names, and I wrote (\ \ w + \ \.) + Why is it, because the mail server may be two domain names, three domain names, or if we do not bring…… () +, abc@sina.com effective, and abc@mail.sina.com is invalid, so this is necessary.
Finally, [az] (2,3), taking into account the general domain no final figures capital rarely see (I think generally the case should be ignored), and a minimum of not less than 2, such as cn, us, etc. up to more than three, such as com, org, etc., and so they wrote the above forms.
Just started learning the regular expression, Oh, a lot of ideas also quite naive, a little bit more complex on the hinder, and cattle were also looking to the guidance of a lot :-)
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back
WEB application reads configuration files
Abstract: WEB application reads configuration files
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="681" border="0"> <tr> <td width = " 397 ">
Suppose you Proxool.properties document attributes in the database configuration information:
Jdbc-0.proxool.alias = Access
Jdbc-0.proxool.driver-class = sun.jdbc.odbc.JdbcOdbcDriver
Jdbc-0.proxool.driver-url = jdbc: odbc: cwb
Jdbc-0.proxool.maximum-connection-count = 20
Jdbc-0.proxool.prototype-count = 4
Jdbc-0.proxool.house-keeping-test-sql = select CURRENT_DATE
Jdbc-0.proxool.verbose = true
Jdbc-0.proxool.statistics = 10s, 1m, 1d
Jdbc-0.proxool.statistics-log-level = ERROR
You attribute this document will be placed on your application's WEB web-info/classes directory, or in the bean category in the document to visit the traditional methods of document attributes Wuguo so:
</ Td> <td width="274">
</ Td> </ tr> </ table>
Package examples; import java.io. *; import java.util .*; public class EnvironmentConfig (static EnvironmentConfig ec; / / Create Object ec private static Hashtable register = new Hashtable ();// static object initialized [in other Before object] private EnvironmentConfig () (super ();) / ** * EnvironmentConfig made an example of * @ return ec * / public static EnvironmentConfig getInstance () (if (ec == null) ec = new EnvironmentConfig (); / / create EnvironmentConfig target return ec; / / return EnvironmentConfig target) / ** * * @ read configuration files * @ param java.lang.String fileName return Properties * / public Properties getProperties (String fileName) (/ / transmission configuration file path InputStream is = null; / / definition of input streams is Properties p = null; try (p = (Properties) register.get (fileName); / / fileName stored in a HashTable / ** * If it is empty Try entered into documents * / if (p == null) (try (is = new FileInputStream (fileName); / / create input streams) catch (Exception e) (if (fileName.startsWith ("/")) / / use getResourceAsStream () method used to locate and open external documents. EnvironmentConfig.class.getResourceAsStream is = (fileName); else is EnvironmentConfig.class.getResourceAsStream ("/"+ = fileName);) p = new Properties (); p.load (is); / / load the input flow register. put (fileName, p) / / stored in its cache is.close ();// HashTable closed input flow)) catch (Exception e) (e.printStackTrace (System.out);) return p; / / Properties returned to the object) / ** * insert description. * Create Date: (2003-8-10 12:30:09) * @ param fileName java.lang.String * @ param strKey java.lang.String * / public String getPropertyValue (String fileName, String strKey) (Properties p = getProperties (fileName); try (return (String) p.getProperty (strKey);) catch (Exception e) (e.printStackTrace (System.out);) return null;))
Below are the jsp test: testpro.jsp
<% @ Page contentType = "text / html; GBK charset ="%>
<% @ Page import = "examples.EnvironmentConfig"%>
<% @ Page import = "java.util .*"%>
Test configuration file
<%
EnvironmentConfig ec = EnvironmentConfig.getInstance ();
/ / Properties p = ec.getProperties ( "/ Proxool.properties");
String s = ec.getPropertyValue ( "/ Proxool.properties", "jdbc-0.proxool.driver-class");
Out.println ( "jdbc-0.proxool.driver-class =" + s);
%>
The result:
Test configuration file
Jdbc-0.proxool.driver-class = sun.jdbc.odbc.JdbcOdbcDriver
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back
In the web application configuration HSQL database
Abstract: In the web application configuration HSQL database
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> Thunder read some forum database, which is used HSQL database. (Please download my entire directory test)
First, download from http://hsqldb.sourceforge.net/ HSQL1.7.3, which will be copied to your hsqldb.jar web applications WEB-INF/lib directory. Write a document lightningboard.properties attributes, data will be stored in your web application TestHsql / hsqldb / lb_db under.
<table Width="681" border="0"> <tr> <td width="392"> # Database — —
DB.DRIVER = org.hsqldb.jdbcDriver
DB.URL = jdbc: hsqldb: webapps / TestHsql / hsqldb / lb_db
DB.USER sa =
DB.PASSWORD =
DB.MAX_CONNECTIONS = 30
Second, write configuration files used to read the above configuration database
Package lightningboard;
Import java.util.Properties;
Import java.io.InputStream;
Import java.io.IOException;
/ **
* LightningBoard configuration, the configuration file:
* "/ Lightningboard.properties."
* @ Version 0.3.5
* @ Author Xiaobo Liu
* /
</ Td> <td width="279" valign="top"> </ td> </ tr> </ table>
(Public class Configuration
Private Properties properties;
Private final static Configuration cfg = new Configuration ();
Private Configuration () (
Properties = new Properties ();
InputStream is = null;
Try (
Is = getClass (). GetResourceAsStream ( "lightningboard.properties");
Properties.load (is);
) Catch (Exception exception) (
System.out.println ( "Can't read the properties file.");
Finally ()
Try (
If (is! = Null)
Is.close ();
) Catch (IOException exception) (
/ / Ignored
)
)
)
Public static Configuration getInstance () (
Return cfg;
)
Public String getValue (String key) (
Return properties.getProperty (key);
)
)
Third, the database connection management, from a simple connection pool access connections.
Package lightningboard.db;
Import java.util.Vector;
Import java.sql.DriverManager;
Import java.sql.Connection;
Import java.sql.SQLException;
Import lightningboard.Configuration;
/ **
* Database Connection Manager
* @ Version 0.3.5
* @ Author Xiaobo Liu
* /
(Public class DBConnectionManager
Private final static DBConnectionManager instance = new DBConnectionManager ();
Private DBConnectionPool pool;
/ **
* Use singleton pattern, only return one instance of DBConnectionManager.
* @ Return DBConnectionManager
* /
Public static DBConnectionManager getInstance () (
Return instance;
)
/ **
* Get a connection
* @ Return Connection
* @ Throws SQLException this method
* /
Public Connection getConnection () (throws SQLException
Return pool.getConnection ();
)
/ **
* Free a connection
* @ Param con connection
* @ Throws SQLException this method
* / Public void freeConnection (Connection con) throws SQLException (
Pool.freeConnection (con);
)
Private DBConnectionManager () (
Init ();
)
Private void init () (
Configuration cfg = Configuration.getInstance ();
String db_driver = cfg.getValue ( "DB.DRIVER");
String db_url = cfg.getValue ( "DB.URL");
String db_user = cfg.getValue ( "DB.USER");
String db_password = cfg.getValue ( "DB.PASSWORD");
Int db_maxConn = Integer.parseInt (cfg.getValue ( "DB.MAX_CONNECTIONS"));
Try (
Class.forName (db_driver);
)
Catch (ClassNotFoundException ex) (
System.out.println (ex);
)
Pool = new DBConnectionPool (db_url, db_user, db_password, db_maxConn);
)
/ / Inner class
(Class DBConnectionPool
Private Vector freeConnections = new Vector ();
Private int maxConn;
Private int connNumb;
Private String URL;
Private String password;
Private String user;
Public DBConnectionPool (String URL, String user, String password, int maxConn) (
This.URL = URL;
This.user = user;
This.password = password;
This.maxConn = maxConn;
)
Public synchronized void freeConnection (Connection con) (
FreeConnections.addElement (con);
ConnNumb -;
NotifyAll ();
)
Public synchronized Connection getConnection () (throws SQLException
Connection con = null;
If (freeConnections.size ()> 0) (
Con = (Connection) freeConnections.firstElement ();
FreeConnections.removeElementAt (0);
Try (
If (con.isClosed ()) (
Con = getConnection ();
)
)
Catch (SQLException e) (
Con = getConnection ();
)
)
Else if (maxConn == 0 | | connNumb <maxConn) (
Con = newConnection ();
)
If (con! = Null) (
ConnNumb + +;
)
Return con;
)
Private Connection newConnection () (throws SQLException
Connection con = DriverManager.getConnection (URL, user, password);
Return con;
)
)
)
4, the following is the test jsp documents: Testhsql.jsp, Testhsql1.jsp (download)
<% @ Page contentType = "text / html; GBK charset ="%>
<% @ Page language = "java" import = "java.sql .*"%>
<% @ Page import = "lightningboard.db.DBConnectionManager"%>
<%
DBConnectionManager dbcm = DBConnectionManager.getInstance ();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
Try (
Conn = dbcm.getConnection ();
/ / If there Table user to delete
Sql = "DROP TABLE IF EXISTS user";
Ps = conn.prepareStatement (sql);
Ps.executeUpdate ();
/ / Create Table
Sql = "CREATE TABLE USER (ID INTEGER, NAME VARCHAR (50), MOBILE CHAR (12))";
Ps = conn.prepareStatement (sql);
Ps.executeUpdate ();
/ / Insert data
Sql = "INSERT INTO user VALUES (?,?,?)";
Ps = conn.prepareStatement (sql);
Ps.setInt (1, 1);
Ps.setString (2, "San");
Ps.setString (3, "13912345678");
Ps.executeUpdate ();
/ / Data enquiries
Ps = conn.prepareStatement ( "SELECT * FROM user");
Rs = ps.executeQuery ();
While (rs.next ()) (
Out.println ( "ID:" + rs.getInt ( "id") + "
");
Out.println ( "Name:" + rs.getString ( "name") + "
");
Out.println ( "mobile phone number:" + rs.getString ( "mobile"));
)
)
Finally (
If (rs! = Null)
Rs.close ();
If (ps! = Null)
Ps.close ();
If (conn! = Null)
Conn.close ();
)
%>
OK!! Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( " CommentXMLStore "); document.getElementById (ElementID). CommentsPersistDiv.getAttribute value = (" CommentContent ");) </ td> </ tr> <tr>
↑ Back
Jsp used for direct download files function
Abstract: The realization of direct download files jsp function
</ Td> </ tr> <tr> <td width="552" height="35" valign="top" class="ArticleTeitle"> <%
/ / Sometimes a user wants to download files, but this type of file in the system associated with the browser, the results will become open in IE.
/ / A common word, excel, such as pdf. Therefore the documents translated into data flow to its browser does not know the type of document to download purposes.
/ / Usage:
/ / Download image
String root = getServletContext (). GetRealPath ("/");
String path = request.getParameter ( "path");
String name = request.getParameter ( "name");
Response.setContentType ( "unknown");
Response.addHeader ( "Content-Disposition", "filename = \" "+ name +" \ "");
Try
(
Java.io.OutputStream os = response.getOutputStream ();
Java.io.FileInputStream fis = new java.io.FileInputStream (root path + + name);
Byte [] b = new byte [1024];
Int i = 0;
While ((i = fis.read (b))> 0)
(
Os.write (b, 0, i);
)
Fis.close ();
Os.flush ();
Os.close ();
)
Catch (Exception e)
(
)
%> </ Td> <td width="175" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
Information shows that the client code
Abstract: The information showed that the client code
</ Td> </ tr> <tr> <td width="437" height="35" valign="top" class="ArticleTeitle"> client information shows that the code, including client ip, region, operating system and browser types.
Jsp following documents: Other code download.
<% @ Page contentType = "text / html; GBK charset ="%>
<% @ Page language = "java" import = "ip .*"%>
<%
String ip = request.getRemoteAddr ();
String [] addr = IPLocalizer.search (request);
/ / String ua = request.getHeader ( "User-Agent");
/ / Out.println (ua + "
");
Out.println ( "Your IP is:" + ip + "from:" + addr [0] + addr [1 ]+","
+ OLUser.getUserSystem (request.getHeader ( "User-Agent "))+","
+ OLUser.getUserBrowser (request.getHeader ( "User-Agent ")));
%> </ Td> <td width="247" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
Simple Cart
Abstract: simple Cart
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
<table Width="740" border="0"> <tr> <td width="734"> </ td> </ tr> </ table>
DummyCart.java procedures in the definition of a shopping cart basic model, the use of Vector DummyCart type data structure to simulate the shopping cart function, there are three attributes DummyCart submit item v respectively, if submit to add value, then the means to Cart added commodities; if it is remove, then users will Cart said that the deletion of products, users need to add Item representatives of the goods or give up the name, v is a Vector types of data, it preserves the shopping cart all the information. Vector use of the method can be added to the shopping cart of goods or commodities to delete the operation, in the most important category DummyCart method is processRequest (), this is the value judgement submit, and then call addItem () method or removeItem () Method completed the basic operation of the cart.
Package test;
Import javax.servlet.http .*;
Import java.util.Vector;
Import java.util.Enumeration;
Public class DummyCart (/ / fictitious Cart
Vector v = new Vector ();// stored goods basket
String submit = null; / / submit = "add" added goods, submit = "remove" thrown away the goods
String item = null; / / name goods
Private void addItem (String name) (
V.addElement (name);
)
Private void removeItem (String name) (
V.removeElement (name);
)
Public void setItem (String name) (
Item = name;
)
Public void setSubmit (String s) (
Submit = s;
)
GetItems public String [] () (
String [] s = new String [v.size ()];
V.copyInto (s);
Return s;
)
Public void processRequest (HttpServletRequest request) (
/ / Null value for submit - user hit enter instead of clicking on
/ / "Add" or "remove"
If (submit == null)
AddItem (item);
If (submit.equals ( "add"))
AddItem (item);
Else if (submit.equals ( "remove"))
RemoveItem (item);
/ / Reset at the end of the request
Reset ();
)
/ / Reset
Private void reset () (
Submit = null;
Item = null;
)
)
The following code used to test:
Carts.html user opens first page selected and then click submit a commodity, and the data submitted to the button carts.jsp, carts.jsp the first to use
<%
Cart.processRequest (request);
%>
You have the following items in your cart:
<%
String [] = cart.getItems items ();
For (int i = 0; i
<%
)
%>
<% @ Include file = "carts.html"%>
Below are carts.html
</ Head>
<body Bgcolor="white">
<form Type=POST action=carts.jsp>
Please enter item to add or remove:
Add Item:
<select NAME="item">
<option> Beavis & Butt-head Video collection
<option> X-files movie
<option> Twin peaks tapes
<option> NIN CD
<option> JSP Book
<option> Concert tickets
<option> Love life
<option> Switch blade
<option> Rex Rugs & Rock n 'Roll
</ Select>
<input TYPE=submit name="submit" value="add">
<input TYPE=submit name="submit" value="remove">
</ Form>
</ Body>