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    The creation of a new type of Session Scope JavaBean component object cart, the cart if the object has been in existence for it not to create a direct taken over by yourself.    Then use the procedures Carts.jsp    Operating instructions object to the carts assignment.    Carts.jsp further down procedures call processRequest () method according to submit it to complete the value of the attribute corresponding Cart operation.    Finally carts.jsp to use cart object getItems () method with cyclic structure for the contents of the cart to full output. 



<%
  Cart.processRequest (request); 
%>


  You have the following items in your cart: 


    <%
      String [] = cart.getItems items (); 
      For (int i = 0; i  %>
  1.   <% [I] = items%> 
    <%
      ) 
    %>






  <% @ 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> 
  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 

Java SE 6 scripting engine for the use in procedures [job is reposted]

  Abstract: Java SE 6 scripting engine for the use in procedures [job is reposted] 




  Java SE 6 of the procedures for the use in scripting engine 

  Source Development天极 



  ————————————————– —————————— 
  Java SE 5 now has been issued in the next Java SE 6 will also be released.    Java SE 6 more Java SE5 a big improvement, and its more powerful, and is designed specifically for Vista, which means that Java SE 6 Vista is the best choice.    The Java SE 6 provides the most numerous new features will become its biggest selling point. 

  Java SE 6 has attracted the most attention One of the new features is support for embedded script.    By default, Java SE 6 support JavaScript, but that does not mean that the Java SE 6 can only support JavaScript.    Java SE 6 in a number of interfaces to define a standard script, which is JSR223.    Through the realization of these interfaces can support Java SE 6 arbitrary scripting language (such as PHP or Ruby). 

  Running a script 

  In the use of Java SE 6 run scripts before, we must know that you support the Java SE 6 what scripting language.    Javax.script package in a lot of categories, but these categories is the main ScriptEngineManager.    Through this type can be present to support Java SE 6 all scripts.    As in the following example will list all can use the script engine factory. 

  Import javax.script .*; 
  Import java.io. *; 
  Import java.util .*; 
  Import static java.lang.System .*; 
  Public class ListScriptEngines 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  / / Get all the scripts engine plant List factories = manager.getEngineFactories (); 
  / / This is the Java SE 5 and the new Java SE 6 For statements for grammar (ScriptEngineFactory factory: factories) 
  ( 
  / / Print information out.printf script ( "Name:% s% n" + 
  "Version:% s% n" + 
  "Language name:% s% n" + 
  "Language version:% s% n" + 
  "Extensions:% s% n" + 
  "Mime types:% s% n" + 
  "Names:% n% s", 
  Factory.getEngineName (), 
  Factory.getEngineVersion (), 
  Factory.getLanguageName (), 
  Factory.getLanguageVersion (), 
  Factory.getExtensions (), 
  Factory.getMimeTypes (), 
  Factory.getNames ()); 
  / / Get the current script engine ScriptEngine engine = factory.getScriptEngine (); 
  ) 
  ) 
  ) 

  The above examples must be in the Java SE 6 compiler.    Import static java.lang.System .* which is a new grammar, the System will be all static members cited the future can be used directly out, or err in the. 

  By running java ListScriptEngines, will show the following information 

  Name: Mozilla Rhino 
  Version: 1.6 release 2 
  Language name: ECMAScript 
  Language version: 1.6 
  Extensions: [js] 
  Mime types: [application / javascript, application / ecmascript, text / javascript, text / ecmascript] 
  Names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] 

  Is the bottom line in the script aliases, is the arbitrary use them in a can.    Have a specific script engine are three kinds of methods. 

  According to extension by the script engine 

  ScriptEngine engine = manager.getEngineByExtension ( "js"); 

  GetEngineByExtension parameters is Extensions: [js] […] in part. 

  According to the script by Mime type engine 

  ScriptEngine engine = manager.getEngineByMimeType ( "text / javascript"); 

  GetEngineByMimeType parameters can be Mime types: [application / javascript, application / ecmascript, text / javascript, text / ecmascript] any one, can be text / javascript into text / ecmascript. 

  According to the script by name engine 

  ScriptEngine engine = manager.getEngineByName ( "javascript"); 

  GetEngineByName the parameters can be Names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] any one, as can be changed ecmascript javascript. 

  Above have been discussed for the first step in the implementation of the script is available with a script engine.    After the completion of the work on this script can be used to implement the corresponding engine of the script.    We can use the eval ScriptEngine approach to the implementation of the script.    Eval method is overloaded several times, but the most common is public Object eval (String script). 
  The following example demonstrates how to use eval javascript approach to the implementation of the script. 

  Import javax.script .*; 
  Import java.io. *; 
  Import static java.lang.System .*; 
  Public class FirstJavaScript 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  / / Get javascript scripting engine ScriptEngine engine = manager.getEngineByName ( "javascript"); 
  Try 
  ( 
  / / Running scripts, and return to the current hours Double hour = (Double) engine.eval ( "var date = new Date ();" + "date.getHours ();"); 
  String msg; 
  / / Hours will be converted to information if greetings (hour <10) 
  ( 
  Msg = "good morning"; 
  ) 
  Else if (hour <16) 
  ( 
  Msg = "good afternoon"; 
  ) 
  Else if (hour <20) 
  ( 
  Msg = "good evening"; 
  ) 
  Else 
  ( 
  Msg = "Good Night"; 
  ) 
  Out.printf ( "hour% s:% n% s", hour, msg); 
  ) 
  Catch (ScriptException e) 
  ( 
  Err.println (e); 
  ) 
  ) 
  ) 

  The above examples are by the current hours, and translate them into greeting.    The output of the above information: 

  9.0 hours: Good morning 

  The most noteworthy example is the implementation of the two scripts, the last sentence is date.getHours ().    Not assign a value to this variable javascript.    At this time, eval method will return this value.    This is somewhat similar to the C language (…) operators.    If (c = a + b, c + d), this expression is the return value of a + b + d. 
  And an interactive scripting language 

  Operation of the above example is a very simple script.    The script is isolated, not the Java script to the transfer of any value.    Although this script back to a value, but this is the way to return to the implicit. 

  In addition to these simple scripting engine features, but also provides us with a more powerful functions.    Even the Java scripting language in the transmission parameters, the script language can also be variable in the value of removed.    These functions depend on the two methods ScriptEngine put and get. 

  Put two parameters, the script is a variable name, and the other is variable, the value of Object Types, therefore, can deliver any value. 
  Get a parameter is the name of the script variables. 

  The following code will be adopted by a string javascript script flip (the string is passed through java javascript), and then through the java this was overturned by the characters, and then output. 

  Import javax.script .*; 
  Import java.io. *; 
  Import static java.lang.System .*; 
  Public class ReverseString 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  / / Create javascript scripting engine ScriptEngine engine = manager.getEngineByName ( "javascript"); 
  Try 
  ( 
  / / Variable name and value abcdefg variable transmission javascript scripting engine.put ( "name", "abcdefg"); 
  / / Beginning of the implementation of the script engine.eval ( "var output =" + 
  "For (i = 0; i <= name.length; i + +) (" + 
  "Name.charAt output = (i) + output" + 
"}");
  / / Output variables have the value of String name = (String) engine.get ( "output"); 
  Out.printf (the "flip after the string:% s", name); 
  ) 
  Catch (ScriptException e) 
  ( 
  Err.println (e); 
  ) 
  ) 
  ) 

  The output of the code above is: was overturned after the string: gfedcba 
  Let scripts run faster 

  As we all know, explain operation mode is the slowest operation mode.    A few examples of these are, without exception, to explain the running.    Because Java EE 6 engine can support script achieve any script engine interface language.    There are many such language compiler provides a function, that is, in the first run scripts before these scripts will be compiled (compiler general here will not generate executable files, but only in memory compilers more easily run into the way) , and then implementation.    If certain scripts to run several times at the turn of the case, the use of this method is very fast.    We can use the compile method ScriptEngine compiler.    Not all support script compiler engine, and only a scripting engine Compilable interface can be used only to compile compiler, or will throw a mistake.    The following examples will demonstrate how to use the method of compiling compile and run javascript script. 

  Import javax.script .*; 
  Import java.io. *; 
  Import static java.lang.System .*; 
  Public class CompileScript 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  ScriptEngine engine = manager.getEngineByName ( "javascript"); 
  Engine.put (the "counter", 0) / / javascript to send a parameter / / determine whether to support this script compiler engine function if (engine instanceof Compilable) 
  ( 
  Compilable compEngine = (Compilable) engine; 
  Try 
  ( 
  / / Compiler CompiledScript script = compEngine.compile ( "function count () (" + 
  "Counter counter +1 =" + 
  "Return counter;" + 
  "); Count ();"); 
  Out.printf ( "Counter:% n% s", script.eval ()); 
  Out.printf ( "Counter:% n% s", script.eval ()); 
  Out.printf ( "Counter:% n% s", script.eval ()); 
  ) 
  Catch (ScriptException e) 
  ( 
  Err.println (e); 
  ) 
  ) 
  Else 
  ( 
  Err.println ( "The engine does not support script compiler!"); 
  ) 
  ) 
  ) 

  The above code running after the display information is as follows: 

  Counter: 1.0 
  Counter: 2.0 
  Counter: 3.0 

  In this example, the first method will be adopted compile script compiler, and then through the eval method of repeated calls.    In this code there is only one function, and hence, the return to the eval function values. 

  Scripting language called dynamic method 

  There is only one example of the above functions can be carried out through the eval call and the value of its return.    But if the script to a number of function or through user input to determine which function calls, which require the use of dynamically invoke method calls.    And compiler, scripting engine Invocable interface must be achieved before we can call dynamic scripting language in the way.    The following examples will demonstrate how dynamic invocation way to run above the string javascript flip the script. 

  Import javax.script .*; 
  Import java.io. *; 
  Import static java.lang.System .*; 
  Public class InvocableTest 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  ScriptEngine engine = manager.getEngineByName ( "javascript"); 
  If (engine instanceof Invocable) 
  ( 
  Try 
  ( 
  Engine.eval ( "function reverse (name) (" + 
  "Var output =" + 
  "For (i = 0; i <= name.length; i + +) (" + 
  "Name.charAt output = (i) + output" + 
  ") Return output ;}"); 
  Invocable invokeEngine = (Invocable) engine; 
  Object o = invokeEngine.invoke ( "reverse", name); 
  Out.printf ( "flip after the string:% s", name); 
  ) 
  Catch (NoSuchMethodException e) 
  ( 
  Err.println (e); 
  ) 
  Catch (ScriptException e) 
  ( 
  Err.println (e); 
  ) 
  ) 
  Else 
  ( 
  Err.println ( "The script does not support dynamic invocation engine"); 
  ) 
  ) 


  Dynamic Interface Implementation 

  There is a scripting engine more attractive features, and that is to achieve dynamic interface.    If we want to implement asynchronous script, through to the implementation of multi-threaded, it InvokeEngine must implement the Runnable interface which can pass Thread launched multi-threading.    Therefore, it can be passed getInterface InvokeEngine dynamic approach to the realization of the Runnable interface.    This step can be divided into three general conduct. 

  1. Use javascript function of the preparation of a run 

  Engine.eval ( "function run () (print (asynchronous implementation );}"); 

  2. Through getInterface method Runnable interface 

  Runnable runner = invokeEngine.getInterface (Runnable.class); 

  3. Start of the use of multithreading Thread 

  Thread t = new Thread (runner); 
  T.start (); 

  Below is a detailed realization of this function code. 

  Import javax.script .*; 
  Import static java.lang.System .*; 

  Public class InterfaceTest 
  ( 
  Public static void main (String args []) 
  ( 
  ScriptEngineManager manager = new ScriptEngineManager (); 
  ScriptEngine engine = manager.getEngineByName ( "javascript"); 
  Try 
  ( 
  Engine.eval ( "function run () (print (asynchronous transfer );}"); 
  Invocable invokeEngine = (Invocable) engine; 
  Runnable runner = invokeEngine.getInterface (Runnable.class); 
  Thread t = new Thread (runner); 
  T.start (); 
  T.join (); 
  ) 
  Catch (InterruptedException e) 
  ( 
  Err.println (e); 
  ) 
  Catch (ScriptException e) 
  ( 
  System.err.println (e); 
  ) 
  ) 
  ) 

  In fact, the above code is achieved through javascript run Runnable interface methods. 





  ↑ Back 

Sudoku solving procedures automatic version of JSP

  Abstract: Sudoku solving procedures automatic version of JSP 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="513" height="86" align="left" valign="top"> 

  Sudoku solving procedures JSP automatic version.    Author: Xu (zx4517@eyou.com) 

  A few independence from the late 18th century Switzerland, after the United States, Japan, the popular math puzzle Games.    Puzzles is five direction pad (3 × 3 lattice grid wide high), a square-shaped, each broken down into a grid of five direction pad.    In each of the five direction pad, respectively, to fill 1-9 figures, the whole series of five direction pad each, the number of each row are not repeated.    Figure: 

  This is what I wrote an automatic disposal of small game alone procedures.    Principle is very simple, basically problem-solving methods used traversal. 

  The interface is written using jsp have the chance to change the swing.    Hope that the interest in the peer friends, study together and discussions.    My E-mail: zhangx@calis.edu.cn 

  Below is the chart: (please download the source code) 

  In the left input appropriate figures, click solution button (or top100 solution), the right answer output. 

  Click check button can be obtained calibration solution. 

  </ Td> <td width="171" valign="top"> </ td> </ tr> </ table> </ td> </ tr> <tr> 

  ↑ Back 

Chinese MySQL reference manual

  Abstract: Chinese MySQL reference manual 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> MySQL 3.23.7-alpha Chinese reference manual.    Web format.    </ Td> </ tr> <tr> 

  ↑ Back 

keep looking »