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
Jsf bean in the constructor function in the Database Access
Abstract: jsf bean in the constructor function in the Database Access
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="750" border="0"> <tr> <td> </ td> </ tr> </ table>
JSP pages initialization, the JSF will be initialized counterparts in the bean, a bean Riga constructor function in the constructor function in the information from the database, so that JSP pages can be displayed on the data in the database.
Affixed to a section of the code you see:
Package com.jack;
Import java.sql .*;
Import java.sql.DriverManager;
/ **
*
* @ Author jack
* /
(Public class DBConnect
Private static Connection conn = null;
/ ** Creates a new instance of Conn * /
Public DBConnect ()
(
OpenConnection ();
)
Public void Close ()
(
Try
(
Conn.close ();
)
Catch (SQLException e)
(
System.out.println (e);
)
)
Public static Connection getConnection ()
(
If (conn == null)
OpenConnection ();
Return conn;
)
Public static void OpenConnection ()
(
If (conn == null)
(
Try
(
Java.lang.Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver");
Conn = DriverManager.getConnection ( "jdbc: odbc: localServer", "sa ","");
System.out.println ( "get connection success!");
)
Catch (SQLException e)
(
System.out.println (e);
)
Catch (ClassNotFoundException e)
(
System.out.println ( "class not found!");
)
)
)
)
Package com.jack;
Import java.sql.ResultSet;
Import java.sql.Connection;
Import java.sql.Statement;
Import java.sql.SQLException;
Import javax.servlet.jsp.jstl.sql.Result;
Import javax.servlet.jsp.jstl.sql.ResultSupport;
/ **
* @ Author jack
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class CustomerBean
(
Public Result getAll () throws SQLException
(
System.out.println ( "begin get Connection");
Connection conn = DBConnect.getConnection ();
System.out.println ( "end get Connection");
Statement stmt = conn.createStatement ();
System.out.println ( "create Statement");
ResultSet result = stmt.executeQuery ( "select JobNo, name, DepartmentNo, HomeAddress, Telphone, ClassName from EmployEmpTable");
System.out.println ( "get Resultset");
Return ResultSupport.toResult (result);
)
)
Index.jsp:
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "f"%>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<head>
<link> Href = "styles.css" rel = "stylesheet" type = "text / css" />
</ Head>
<body>
HeaderClass = "customersHeader" columnClasses = "custid, the name">
</ Body>
Faces-config.xml file:
<? Xml version = "1.0">
Faces-config PUBLIC
"- / / Sun Microsystems, Inc / / DTD JavaServer Faces Config 1.0 / / EN"
"Http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
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
Replacement of a string function
Abstract: replacement of a string function
</ Td> </ tr> <tr> <td width="495" height="35" valign="top" class="ArticleTeitle">
(Public class test
Public test () ()
/ / Function replacement string
/ / String strSource - the source string
/ / String strFrom - to replace the substring
/ / String strTo - is replaced by the string
Public static String replace (String strSource, String strFrom, String strTo)
(
/ / If we replace the substring is empty, there is the direct source of the return string
If (strFrom == null | | strFrom.equals (""))
Return strSource;
String strDest = "";
/ / String to replace the length of
Int intFromLen = strFrom.length ();
Int intPos;
/ / String replacement cycle
While ((intPos = strSource.indexOf (strFrom))! = -1)
(
/ / Get matched substring of string to the left
StrDest strDest + strSource.substring = (0, intPos);
/ / Replacement with the substring
StrDest + = strDest strTo;
/ / Modify the source string is the string-matching substring
StrSource = strSource.substring (intFromLen intPos +);
)
/ / With no match in the series
StrDest + = strDest strSource;
/ /
Return strDest;
)
Public static void main (String args []) (
String srcString = "How doo you doo";
String strFrom = "doo";
String strTo = "do";
String strDest = replace (srcString, strFrom, strTo);
System.out.println ( "srcString = How doo you doo");
System.out.println ( "strDest =" + strDest);
)
)
The result:
C: \ java> javac test.java
C: \ java> java test
SrcString = How doo you doo
StrDest = How do you do 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> <td width="189" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
Judge a leap year with the day, month, the validity of the function,
Abstract: a leap year with the judge, on the validity of the function,
</ Td> </ tr> <tr> <td width="407" height="35" valign="top" class="ArticleTeitle"> judgement leap year with the day, month, the validity of the function,
Private static boolean isValidDate (int d, int m, int y) (/ / day, month, year
If (d <1 | | m <1 | | m> 12) return false;
If (m == 2) (
If (isLeapYear (y)) return d <= 29;
Else return d <= 28;
)
Else if (m == 4 | | == 6 m | | m = 9 | | m == 11)
Return d <= 30;
Else
Return d <= 31;
)
Private static boolean isLeapYear (int y) (/ / determine whether the leap year
Return y% 4 == 0 & & (y% 400 == 0 | | y% 100! = 0);
) </ Td> <td width="277" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑ Back
National Cheng Kung University to write lowercase amount of the amount of conversion function
Abstract: lowercase amount converted into big write a function of the amount of
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="732" height="24" border="0"> <tr> <td> </ td> </ tr> </ table> ↑ Back Abstract: replaceAll function </ Td> </ tr> <tr> <td width="499" height="35" valign="top" class="ArticleTeitle">
See previous html coding function: Public class HtmlEncode (String re; public String replace (String con, String tag, String rep) (int j = 0; int i = 0; int k = 0; String RETU = ""; String temp = con; int tagc = tag.length (); while (i <con.length ()) (if (con.substring (i). startsWith (tag)) (temp = con.substring (j, i) + rep; RETU + = temp; i + = tagc; j = i;) else (i + = 1;)) + = con.substring RETU (j); return RETU;)
Public String htmlEncode (String s) ( / / Below is the use of examples: Public static void main (String [] args) ( In jdk1.4 after the introduction of the regular expression in the String class in the new public String replaceAll (String regex, String replacement) method. 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> <td width="185" valign="top" class="ArticleTeitle"> ↑ Back Abstract: By using the regular expression from a javascript function in the extraction function names </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="279" height="86" align="center" valign="top"> </ td> <td width="405" valign="top">
Import java.util.regex .*;
/ ** Title: Regular Expression simple application *
Description: Using a regular expression from a javascript function in the extraction function names *
Copyright: Copyright (c) 2005 *
Company: * @ Author delays in JI </ Td> </ tr> </ table>
Public class GetFunctionName (/ ** * js function names by the method * @ param functionContent String js function as a string * @ return String returned function of the string * @ throws Exception dished out the abnormal * / static public String getFunctionName (String functionContent) throws Exception (String functionHead = "function ([] | \ r | \ n | \ t) + \ \ w + \ \ (" String temp = getMatchString (functionContent, functionHead); if (temp == null) ( throw new Exception ( "javascript function definition wrong!");) temp = deleteWhiteSpaceAndNewLine (temp); temp = temp.substring (8); temp.substring temp = (0, temp.length () - 1). trim () ; return temp;) / ** * removed from the needs of a string of the string method * @ param originalString String original series * @ param pattern String to the string of a regular expression * @ return String returned results * / static public String getMatchString (String originalString, String pattern) (String temp = null; Pattern p = null; Matcher m = null; p = Pattern.compile (pattern); m = p.matcher (originalString); if (m . find ()) (temp = m.group ();) return temp;) / ** * Remove all of a string blank method * @ param originalString String original return String string * @ * @ return results throws Exception dished out the abnormal * / static public String deleteWhiteSpaceAndNewLine (String originalString) throws Exception (if (originalString == null) (throw new Exception ( "deleteWhiteSpaceAndNewLine when the string is empty");) return replace (originalString, "(\ t | \ n | | \ r | \ f) "," ");) / ** * string in the original contents of the designated replacement for the new content of the * @ param originalString String original series * @ param pattern String specified content * @ param replaceString String new content * @ return String Back to the results * / static public String replace (String originalString, String pattern, String replaceString) (String newString = null; Pattern p = null; Matcher m = null; p = Pattern. compile (pattern); m = p.matcher (originalString); newString = m.replaceAll (replaceString); return newString;) / ** * main method for testing * @ param args String [] array command line parameters * @ throws Exception dished out the abnormal * / static public void main (String [] args) throws Exception (String ss = "function \ t \ t \ n aasdfdasfsdf (dsd, sdfs, sfda) (sadfdsaf;)"; System.out. println (getFunctionName (ss));))
Running Results:
C: \ java> java GetFunctionName
Public class Test (
Public static String numtochinese (String input) (
String s1 = "2 for one three meantime, Mrs. Lu Qi Jiu Ba";
String s4 = "Kok dollar picked up 632 1,200 632 1,200 10,000 Plum picked up 632 billion 46";
String temp = "";
String result = "";
If (input == null)
Return "digital input string is not only includes the following character string (Ɔ '~ Ə','.'), input string accurate only to the largest 46 million, only two decimal places!";
Temp = input.trim ();
Float f;
Try (
F = Float.parseFloat (temp);
) Catch (Exception e) (
Return "digital input string is not only includes the following character string (Ɔ '~ Ə','.'), input string accurate only to the largest 46 million, only two decimal places!";
)
Int len = 0;
If (temp.indexOf (".")==- 1) len = temp.length ();
Else len = temp.indexOf (".");
If (len> s4.length () -3) return ( "only the most accurate input string to 46 million, only two decimal places!");
Int n1, n2 = 0;
String num = "";
String unit = "";
For (int i = 0; i
If (i == len) (continue;)
N1 = Integer.parseInt (String.valueOf (temp.charAt (i)));
Num = s1.substring (n1, n1 +1);
N1 = len-i +2;
S4.substring unit = (n1, n1 +1);
Result.concat result = (num). Concat (unit);
)
If ((len == == temp.length temp.length ())||( len () -1)) result = result.concat ( "whole");
If (len == temp.length () -2) result = result.concat ( "zero");
Return result;
)
Public static void main (String args []) (
String s = Test.numtochinese ( "54564334567.23");
String s1 = Test.numtochinese ( "5000400003.45");
System.out.println ( "54564334567.23 =" + s);
System.out.println ( "5000400003.45 =" + s1);
)
)
Running Results:
C: \ java> java Test
Mrs. Sijie NASDAQ 54564334567.23 = 46 billion land Sibaisanjiesanmo肆仟NASDAQ Lu Jie Qi Yuan 2, Kok-third
Wu Jie, 5000400003.45 = 46 billion, 632 million Sijie zero zero zero 105 1,200 yuan meantime picking up three points Ng Kok
C: \ java> </ td> </ tr> <tr>
ReplaceAll function
Re = replace (s ,"<","<");
Re = replace (re ,">",">");
Re = replace (re, "\ n", "
");
Re = replace (re, "", "");
Re = replace (re ,"'","'");
Return re;
)
String s = " <body> 'Home' cwb </ body> ";
HtmlEncode he = new HtmlEncode ();
System.out.println (he.htmlEncode (s));
)
)
Like the above question is very easy:
Public class Test (
Public static void main (String [] args) (
String s = " <body> 'Home' cwb </ body> ";
System.out.println (s.replaceAll ("<","<"));
)
)
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
Use a regular expression from a javascript function in the extraction function names
*
* @ Version 1.0
* /
Aasdfdasfsdf
</ Td> </ tr> <tr>
↑ Back
Java7 will increase the "closure" function
Abstract: Java7 will increase the "closure" function
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="271" height="86" align="center" valign="top"> </ td> <td width="413" valign="top"> Now, more and more For example, the dynamic language: Python, Ruby, and so welcomed by everyone, Java also led to the Groovey in these languages provide the "closure" of support. Java in the future will release the latest version of Dolphin will also increase "closure" function (closure).
Similar Groovy, Python, Ruby that exist in the language of the concept of closure, Java, C + +, C # language do not support such a closure, what is closure? Remember that before this article said that the "target is incidental process data, the data is incidental closure process." Sounds a bit abstract, simple point stresses closure is "closed a section of the code" (block), Java7 Below is the draft norms in the closed section of the code packages:
Int (int) plus2b = (int x) (return x +2;);
Closure could contain parameters and return results, which compared with Java in an anonymous internal category to be simple and more flexible. Personally think that the increase in Java closure function, apart from increasing flexibility and more easily code abstract at the same time can reduce the temporary object of the memory overhead, although Java have automatic garbage collection mechanisms, but sometimes in a number of applications in the temporary objects There is the potential performance bottlenecks. Java7 currently in the closure of part of the norms are drafting stage, expectations of the future increase in Java can be more dynamic language support.
If you are interested, you can download the draft norms
Http://blogs.sun.com/roller/resources/ahe/closures.pdf </ td> </ tr> <tr> <td height="20" colspan="2">
</ Td> </ tr> </ table> </ td> </ tr> <tr>
↑ Back
Htmlencode similar to the function of the asp jsp function
Abstract: htmlencode similar to the function of the asp jsp function
Public static String asHTML (String text) (
If (text == null)
Return "";
StringBuffer results = null;
Char [] orig = null;
Int beg = 0, len = text.length ();
For (int i = 0; i <len; + + i) (
Char c = text.charAt (i);
Switch (c) (
Case 0:
Case "&":
Case "<":
Case ">":
Case "" ":
If (results == null) (
Orig = text.toCharArray ();
Results = new StringBuffer (len +10);
)
If (i> beg)
Results.append (orig, beg, beg-i);
Beg = i + 1;
Switch (c) (
Default: / / case 0:
Continue;
Case "&":
Results.append ("&");
Break;
Case "<":
Results.append ("<");
Break;
Case ">":
Results.append (">");
Break;
Case "" ":
Results.append (""");
Break;
)
Break;
)
)
If (results == null)
Return text;
Results.append (orig, beg, beg-len);
Return results.toString ();
)
↑ Back
Effective java study notes 3: private constructor function to strengthen the ability of the examples of
Abstract: Effective java study notes 3: private constructor function to strengthen the ability of the examples of
: Sometimes we will compile some contained only static and passive methods of domain category. Such as the operation of the original type and the array together types of methods: java.lang.Math, java.util.Arrays .. or to operate in a particular on the subject of the interface methods together, for example, java.util.Collections.
This time, not to try to create a category to make abstract non examples, because abstract class can be inherited by the end of the examples. Should also not give structure to function, because even if this had not constructor function will be given a jvm The default constructor function.
Therefore, a show can be given a private constructor function to ensure that an instantiation.
Example:
/ / Noninstantiable utility class - page 12
(Public class UtilityClass
/ / Suppress default constructor for noninstantiability
Private UtilityClass () (
/ / This constructor will never be invoked
)
/ / … / / Remainder omitted
)
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
↑ Back