import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ReadMac {
??? public ReadMac() {
??? }
??? public static String getMACAddress() {
??????? String address = "";
??????? String os = System.getProperty("os.name");
??????? if (os != null && os.startsWith("Windows")) {
??????????? try {
??????????????? String command = "cmd.exe /c ipconfig /all";
??????????????? Process p = Runtime.getRuntime().exec(command);
??????????????? BufferedReader br =
??????????????????????? new BufferedReader(
??????????????????????????????? new InputStreamReader(p.getInputStream()));
??????????????? String line;
??????????????? while ((line = br.readLine()) != null) {
??????????????????? if (line.indexOf("Physical Address") > 0) {
??????????????????????? int index = line.indexOf(":");
??????????????????????? index += 2;
??????????????????????? address = line.substring(index);
??????????????????????? break;
??????????????????? }
??????????????? }
??????????????? br.close();
??????????????? return address.trim();
??????????? } catch (IOException e) {}
??????? }
??????? return address;
??? }
public static void main(String[] args) {
??? System.out.println("Mac:"+ReadMac.getMACAddress());
}
}
set CATALINA_HOME=..//在這句下面加入
//設(shè)定jdk的路徑
set JAVA_HOME=..\..\%CATALINA_HOME%\jdk1.4
:gotHome? //=..//在這句上面加入
但是用了tomcat5.5卻沒(méi)有了這兩個(gè)文件,它將自已的安裝路徑和jdk的路徑都寫(xiě)到注冊(cè)表中了.
這就需要在打包時(shí)動(dòng)態(tài)的修改注冊(cè)表,否則將不能正常使用.
-- 安裝路徑:your_variable
--Dialog.Message(your_variable, your_variable, MB_OK, MB_ICONEXCLAMATION);
-- 創(chuàng)建一個(gè)全局變量,并賦予一個(gè)字符串
your_variable1 = "";
--Dialog.Message(your_variable, your_variable, MB_OK, MB_ICONEXCLAMATION);
-- 從注冊(cè)表中導(dǎo)入的值:
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Tomcat");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Tomcat\\5.5");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters");
your_variable1=String.Replace("D:\\Software\\Tomcat 5.5","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Tomcat\\5.5", "InstallPath", your_variable1, 7);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Tomcat\\5.5", "Version", "5.5.12", 7);
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Java");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Log");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Start");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Stop");
your_variable1=String.Replace("D:\\Software\\jdk1.5\\bin\\client\\jvm.dll","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Java", "Jvm",your_variable1, 1);
your_variable1=String.Replace("D:\\Software\\Tomcat 5.5\\bin\\bootstrap.jar","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Java", "Classpath",your_variable1, 1);
your_variable1=String.Replace("-Dcatalina.home=D:\\Software\\Tomcat 5.5|-Dcatalina.base=D:\\Software\\Tomcat 5.5|-Djava.endorsed.dirs=D:\\Software\\Tomcat 5.5\\common\\endorsed|-Djava.io.tmpdir=D:\\Software\\Tomcat 5.5\\temp|-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager|-Djava.util.logging.config.file=D:\\Software\\Tomcat 5.5\\conf\\logging.properties","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Java", "Options", your_variable1, 7);
your_variable1=String.Replace("D:\\Software\\Tomcat 5.5\\logs","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Log", "Path",your_variable1 , 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Log", "StdError", "auto", 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Log", "StdOutput", "auto", 1);
your_variable1=String.Replace("D:\\Software\\Tomcat 5.5","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Start", "WorkingPath", your_variable1, 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Start", "Class", "org.apache.catalina.startup.Bootstrap", 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Start", "Params", "start", 7);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Start", "Mode", "jvm", 1);
your_variable1=String.Replace("D:\\Software\\Tomcat 5.5","D:\\Software",your_variable,true);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Stop", "WorkingPath", your_variable1, 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Stop", "Class", "org.apache.catalina.startup.Bootstrap", 1);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Stop", "Params", "stop", 7);
Registry.SetValue(3, "Software\\Apache Software Foundation\\Procrun 2.0\\Tomcat5\\Parameters\\Stop", "Mode", "jvm", 1);
-- 從注冊(cè)表中導(dǎo)入的值:
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "Type", "16", 4);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "Start", "3", 4);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "ErrorControl", "1", 4);
your_variable1=String.Replace("\"D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\bin\\Tomcat5.exe\" //RS//Tomcat5","D:\\Program Files\\Apache Software Foundation",your_variable,true);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "ImagePath", your_variable1, 2);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "DisplayName", "Apache Tomcat", 7);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "ObjectName", "LocalSystem", 7);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5", "Description", "Apache Tomcat 5.5.12 Server - Registry.CreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Enum");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Parameters");
Registry.CreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Security");
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Enum", "0", "Root\\LEGACY_Tomcat5\\0000", 7);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Enum", "Count", "1", 4);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Enum", "NextInstance", "1", 4);
Registry.SetValue(3, "SYSTEM\\CurrentControlSet\\Services\\Tomcat5\\Security", "Security", "01 00 14 80 90 00 00 00 9C 00 00 00 14 00 00 00 30 00 00 00 02 00 1C 00 01 00 00 00 02 80 14 00 FF 01 0F 00 01 01 00 00 00 00 00 01 00 00 00 00 02 00 60 00 04 00 00 00 00 00 14 00 FD 01 02 00 01 01 00 00 00 00 00 05 12 00 00 00 00 00 18 00 FF 01 0F 00 01 02 00 00 00 00 00 05 20 00 00 00 20 02 00 00 00 00 14 00 8D 01 02 00 01 01 00 00 00 00 00 05 0B 00 00 00 00 00 18 00 FD 01 02 00 01 02 00 00 00 00 00 05 20 00 00 00 23 02 00 00 01 01 00 00 00 00 00 05 12 00 00 00 01 01 00 00 00 00 00 05 12 00 00 00", 3);
import java.io.File;
import org.apache.tools.zip.ZipOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
public class CompressBook {
? public CompressBook() {
? }
? /*
?? * inputFileName 輸入一個(gè)文件夾
?? * zipFileName 輸出一個(gè)壓縮文件夾
?? */
? public void zip(String inputFileName) throws Exception {
??? String zipFileName = "c:\\test.zip"; //打包后文件名字
??? System.out.println(zipFileName);
??? zip(zipFileName, new File(inputFileName));
? }
? private void zip(String zipFileName, File inputFile) throws Exception {
??? ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
??? zip(out, inputFile, "");
??? System.out.println("zip done");
??? out.close();
? }
? private void zip(ZipOutputStream out, File f, String base) throws Exception {
??? if (f.isDirectory()) {
????? File[] fl = f.listFiles();
????? out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
????? base = base.length() == 0 ? "" : base + "/";
????? for (int i = 0; i < fl.length; i++) {
??????? zip(out, fl[i], base + fl[i].getName());
????? }
??? }
??? else {
????? out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
????? FileInputStream in = new FileInputStream(f);
????? int b;
????? System.out.println(base);
????? while ( (b = in.read()) != -1) {
??????? out.write(b);
????? }
????? in.close();
??? }
? }
? public static void main(String [] temp){
??? CompressBook book = new CompressBook();
??? try {
????? book.zip("c:\\test",new File("c:\\test"));
??? }
??? catch (Exception ex) {
??? }
? }
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import java.util.zip.GZIPInputStream;
import java.io.DataInputStream;
?
/**
?* Description: 此類(lèi)用于...
?*
?* @author??? wunaigang(2005-6-21)
?* @version?? 1.0.0
?*/
public class ZipManager {
?
??????? /**
???????? * zip壓縮功能測(cè)試. 將d:\\temp\\zipout目錄下的所有文件連同子目錄壓縮到d:\\temp\\out.zip.
???????? *
???????? * @param baseDir 所要壓縮的目錄名(包含絕對(duì)路徑)
???????? * @param objFileName 壓縮后的文件名
???????? * @throws Exception
???????? */
??????? public void createZip(String baseDir, String objFileName) throws Exception {
??????????????? File folderObject = new File(baseDir);
?
??????????????? if (folderObject.exists()){
??????????????????????? List fileList = getSubFiles(new File(baseDir));
?
??????????????????????? //壓縮文件名
??????????????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName));
?
??????????????????????? ZipEntry ze = null;
??????????????????????? byte[] buf = new byte[1024];
??????????????????????? int readLen = 0;
??????????????????????? for (int i = 0; i < fileList.size(); i++) {
??????????????????????????????? File f = (File) fileList.get(i);
??????????????????????????????? System.out.println("Adding: " + f.getPath() + f.getName());
?
??????????????????????????????? //創(chuàng)建一個(gè)ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????????????????????? ze = new ZipEntry(getAbsFileName(baseDir, f));
??????????????????????????????? ze.setSize(f.length());
??????????????????????????????? ze.setTime(f.lastModified());
?
??????????????????????????????? //將ZipEntry加到zos中,再寫(xiě)入實(shí)際的文件內(nèi)容
??????????????????????????????? zos.putNextEntry(ze);
??????????????????????????????? InputStream is = new BufferedInputStream(new FileInputStream(f));
??????????????????????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
??????????????????????????????????????? zos.write(buf, 0, readLen);
??????????????????????????????? }
??????????????????????????????? is.close();
??????????????????????????????? System.out.println("done...");
??????????????????????? }
??????????????????????? zos.close();
??????????????? }else{
??????????????????????? throw new Exception("this folder isnot exist!");
??????????????? }
??????? }
??????? /**
???????? * zip壓縮功能測(cè)試. 將指定文件壓縮后存到一壓縮文件中
???????? *
???????? * @param baseDir 所要壓縮的文件名
???????? * @param objFileName 壓縮后的文件名
???????? * @return 壓縮后文件的大小
???????? * @throws Exception
???????? */
??????? public long createFileToZip(String zipFilename,String sourceFileName) throws Exception {
?
??????????????? File? sourceFile = new File(sourceFileName);
?
??????????????? byte[] buf = new byte[1024];
?
??????????????? //壓縮文件名
??????????????? File objFile = new File(zipFilename);
?
??????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));
?
??????????????? ZipEntry ze = null;
??????????????? //創(chuàng)建一個(gè)ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????? ze = new ZipEntry(sourceFile.getName());
??????????????? ze.setSize(sourceFile.length());
??????????????? ze.setTime(sourceFile.lastModified());
?
??????????????? //將ZipEntry加到zos中,再寫(xiě)入實(shí)際的文件內(nèi)容
??????????????? zos.putNextEntry(ze);
?
??????????????? InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
?
??????????????? int readLen = -1;
??????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
??????????????????????? zos.write(buf, 0, readLen);
??????????????? }
??????????????? is.close();
??????????????? zos.close();
?
??????????????? return objFile.length();
??????? }
??????? /**
???????? * zip壓縮功能測(cè)試. 將指定文件壓縮后存到一壓縮文件中
???????? *
???????? * @param baseDir 所要壓縮的文件名
???????? * @param objFileName 壓縮后的文件名
???????? * @return 壓縮后文件的大小
???????? * @throws Exception
???????? */
??????? public long createFileToZip(File sourceFile,File zipFile)throws IOException {
?
??????????????? byte[] buf = new byte[1024];
?
??????????????? //壓縮文件名
??????????????? File objFile = zipFile;
?
??????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));
?
??????????????? ZipEntry ze = null;
??????????????? //創(chuàng)建一個(gè)ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????? ze = new ZipEntry(sourceFile.getName());
??????????????? ze.setSize(sourceFile.length());
??????????????? ze.setTime(sourceFile.lastModified());
?
??????????????? //將ZipEntry加到zos中,再寫(xiě)入實(shí)際的文件內(nèi)容
??????????????? zos.putNextEntry(ze);
?
??????????????? InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
?
??????????????? int readLen = -1;
??????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
??????????????????????? zos.write(buf, 0, readLen);
??????????????? }
??????????????? is.close();
??????????????? zos.close();
?
??????????????? return objFile.length();
??????? }
?
??????? /**
???????? * 測(cè)試解壓縮功能. 將d:\\download\\test.zip連同子目錄解壓到d:\\temp\\zipout目錄下.
???????? *
???????? * @throws Exception
???????? */
??????? public void releaseZipToFile(String sourceZip, String outFileName)
??????????????????????? throws IOException{
????????????????????? ZipFile zfile=new ZipFile(sourceZip);
????????????????????? System.out.println(zfile.getName());
????????????????????? Enumeration zList=zfile.entries();
????????????????????? ZipEntry ze=null;
????????????????????? byte[] buf=new byte[1024];
????????????????????? while(zList.hasMoreElements()){
????????????????????? //從ZipFile中得到一個(gè)ZipEntry
????????????????????? ze=(ZipEntry)zList.nextElement();
????????????????????? if(ze.isDirectory()){
????????????????????? continue;
????????????????????? }
????????????????????? //以ZipEntry為參數(shù)得到一個(gè)InputStream,并寫(xiě)到OutputStream中
????????????????????? OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())));
????????????????????? InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
????????????????????? int readLen=0;
????????????????????? while ((readLen=is.read(buf, 0, 1024))!=-1) {
????????????????????? os.write(buf, 0, readLen);
????????????????????? }
????????????????????? is.close();
????????????????????? os.close();
????????????????????? System.out.println("Extracted: "+ze.getName());
????????????????????? }
????????????????????? zfile.close();
??????? }
?
??????? /**
???????? * 取得指定目錄下的所有文件列表,包括子目錄.
???????? *
???????? * @param baseDir
???????? *??????????? File 指定的目錄
???????? * @return 包含java.io.File的List
???????? */
??????? private List getSubFiles(File baseDir) {
??????????????? List ret = new ArrayList();
??????????????? //File base=new File(baseDir);
??????????????? File[] tmp = baseDir.listFiles();
??????????????? for (int i = 0; i < tmp.length; i++) {
??????????????????????? if (tmp[i].isFile()) {
??????????????????????????????? ret.add(tmp[i]);
??????????????????????? }
??????????????????????? if (tmp[i].isDirectory()) {
??????????????????????????????? ret.addAll(getSubFiles(tmp[i]));
??????????????????????? }
??????????????? }
??????????????? return ret;
??????? }
?
??????? /**
???????? * 給定根目錄,返回一個(gè)相對(duì)路徑所對(duì)應(yīng)的實(shí)際文件名.
???????? *
???????? * @param baseDir
???????? *??????????? 指定根目錄
???????? * @param absFileName
???????? *??????????? 相對(duì)路徑名,來(lái)自于ZipEntry中的name
???????? * @return java.io.File 實(shí)際的文件
???????? */
??????? private File getRealFileName(String baseDir, String absFileName) {
??????????????? String[] dirs = absFileName.split("/");
??????????????? //System.out.println(dirs.length);
??????????????? File ret = new File(baseDir);
??????????????? //System.out.println(ret);
??????????????? if (dirs.length > 1) {
??????????????????????? for (int i = 0; i < dirs.length - 1; i++) {
??????????????????????????????? ret = new File(ret, dirs[i]);
??????????????????????? }
??????????????? }
??????????????? if (!ret.exists()) {
??????????????????????? ret.mkdirs();
??????????????? }
??????????????? ret = new File(ret, dirs[dirs.length - 1]);
??????????????? return ret;
??????? }
?
??????? /**
???????? * 給定根目錄,返回另一個(gè)文件名的相對(duì)路徑,用于zip文件中的路徑.
???????? *
???????? * @param baseDir
???????? *??????????? java.lang.String 根目錄
???????? * @param realFileName
???????? *??????????? java.io.File 實(shí)際的文件名
???????? * @return 相對(duì)文件名
???????? */
??????? private String getAbsFileName(String baseDir, File realFileName) {
??????????????? File real = realFileName;
??????????????? File base = new File(baseDir);
??????????????? String ret = real.getName();
??????????????? while (true) {
??????????????????????? real = real.getParentFile();
??????????????????????? if (real == null)
??????????????????????????????? break;
??????????????????????? if (real.equals(base))
??????????????????????????????? break;
??????????????????????? else {
??????????????????????????????? ret = real.getName() + "/" + ret;
??????????????????????? }
??????????????? }
??????????????? System.out.println("TTTTT" + ret);
??????????????? return ret;
??????? }
??????? public void testReadZip() throws Exception{
??????? String baseDir="d:\\temp\\zipout";
??????? ZipFile zfile=new ZipFile("d:\\download\\src.zip");
??????? System.out.println(zfile.getName());
??????? Enumeration zList=zfile.entries();
??????? ZipEntry ze=null;
??????? byte[] buf=new byte[1024];
??????? while(zList.hasMoreElements()){
??????? //從ZipFile中得到一個(gè)ZipEntry
??????? ze=(ZipEntry)zList.nextElement();
??????? if(ze.isDirectory()){
??????? continue;
??????? }
??????? //以ZipEntry為參數(shù)得到一個(gè)InputStream,并寫(xiě)到OutputStream中
??????? OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(baseDir, ze.getName())));
??????? InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
??????? int readLen=0;
??????? while ((readLen=is.read(buf, 0, 1024))!=-1) {
??????? os.write(buf, 0, readLen);
??????? }
??????? is.close();
??????? os.close();
??????? System.out.println("Extracted: "+ze.getName());
??????? }
??????? zfile.close();
??????? }
?? public static void main(String args[]){
???? ZipManager manager = new ZipManager();
???? try {
?????? //manager.releaseZipToFile("c:\\test.zip","c:\\test");
?????? manager.testReadZip();
???? }
???? catch (Exception e) {}
???? System.out.println("over");
?? }
?
}
HaiHome.java: package ejbs; import java.io.Serializable; import java.rmi.*; import javax.ejb.*; public interface HaiHome extends EJBHome { HaiClient create() throws RemoteException, CreateException; } |
package ejbs; import javax.ejb.*; import java.rmi.RemoteException; public interface HaiClient extends EJBObject { public String sayHai() throws RemoteException; } haiBean.java: package ejbs; import javax.ejb.*; import javax.naming.*; public class HaiBean implements SessionBean { public String sayHai() { return "Hai, EJB technology!"; } public void ejbCreate() throws EJBException {} public void ejbRemove() throws EJBException {} public void ejbPassivate() {} public void ejbActivate() {} public void setSessionContext(SessionContext sc) {} } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC ’-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN’ ’http://java.sun.com/dtd/ejb-jar_2_0.dtd’> <ejb-jar> <description>Hai EJB instance.</description> <display-name>Hai EJB</display-name> <enterprise-beans> <session> <ejb-name>HaiEJB</ejb-name> <home>ejbs.HaiHome</home> <remote>ejbs.HaiClient</remote> <ejb-class>ejbs.HaiBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> |
<?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-beans> <session> <ejb-name>HaiEJB</ejb-name> <jndi-name>HaiEJB</jndi-name> </session> </enterprise-beans> </jboss> |
<%@ page contentType="text/html;charset=GBK" %> <%@ page import="ejbs.*,javax.ejb.*,javax.naming.*,javax.rmi.PortableRemoteObject,java.rmi.RemoteException" %> <html> <body> <% String message = "nothing!"; try { InitialContext ic = new InitialContext(); Object objRef = ic.lookup("HaiEJB"); HaiHome home = (HaiHome) PortableRemoteObject.narrow(objRef,ejbs.HaiHome.class); HaiClient haiRemote = home.create(); message = haiRemote.sayHai(); } catch (RemoteException re) { re.printStackTrace(); } catch (CreateException ce) { ce.printStackTrace(); } catch (NamingException ne) { ne.printStackTrace(); } %> <h1><%=message%></h1> </body> </html> |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC ’-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN’ ’http://java.sun.com/dtd/web-app_2_3.dtd’> <web-app> <ejb-ref> <ejb-ref-name>HaiEJB</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>ejbs.HaiHome</home> <remote>ejbs.HaiClient</remote> </ejb-ref> </web-app> |
<?xml version="1.0" encoding="UTF-8"?> <jboss-web> <ejb-ref> <ejb-ref-name>HaiEJB</ejb-ref-name> <jndi-name>HaiEJB</jndi-name> </ejb-ref> </jboss-web> |
<?xml version="1.0" encoding="UTF-8"?> <application> <display-name>HaiEJB J2EE Application</display-name> <module> <web> <web-uri>haiejb.war</web-uri> <context-root>/haiejb</context-root> </web> </module> <module> <ejb>haiejb.jar</ejb> </module> </application> |
JBOSS_HOME\server\default\log\server.log |
|
VO和PO的主要區(qū)別在于:
VO是獨(dú)立的Java Object。
PO是由Hibernate納入其實(shí)體容器(Entity Map)的對(duì)象,它代表了與數(shù)據(jù)庫(kù)中某條記錄對(duì)應(yīng)的Hibernate實(shí)體,PO的變化在事務(wù)提交時(shí)將反應(yīng)到實(shí)際數(shù)據(jù)庫(kù)中。
實(shí)際上,這個(gè)VO被用作Data Transfer Object,即所謂的DTO。想必,VO就是Data Access Object ---DAO了啦。為什么要有這二者之分呢?如在傳統(tǒng)的MVC架構(gòu)中,位于Model層的PO,是否允許被傳遞到其他層面。由于PO的更新最終將被映射到實(shí)際數(shù)據(jù)庫(kù)中,如果PO在其他層面(如View層)發(fā)生了變動(dòng),那么可能會(huì)對(duì)Model層造成意想不到的破壞。
主要想說(shuō)的還是如何進(jìn)行二者之間的轉(zhuǎn)換:
屬性復(fù)制可以通過(guò)Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/)組件提供的屬性批量復(fù)制功能,避免繁復(fù)的get/set操作。down下來(lái)之后,里面的API DOC一應(yīng)俱全。
對(duì)于一些無(wú)需處理其它處理(如過(guò)濾)直接用BeanUtilsBean.copyProperties方法,其參考如下:
public static void copyProperties(java.lang.Object dest,
????????????????????????????????? java.lang.Object orig)
?????????????????????????? throws java.lang.IllegalAccessException,
????????????????????????????????? java.lang.reflect.InvocationTargetExceptioCopy property values from the origin bean to the destination bean for all cases where the property names are the same.
范例1:
?TUser user? =?? new? TUser();
TUser anotherUser? =?? new? TUser();
user.setName( " Emma " );
user.setUserType( 1 );
?try??? {
BeanUtils.copyProperties(anotherUser,user);
System.out.println( " UserName =>? "
?+ anotherUser.getName()
);
System.out.println( " UserType =>? "
?+? anotherUser.getUserType()
);
}?? catch? (IllegalAccessException e)?? {
e.printStackTrace();
}?? catch? (InvocationTargetException e)?? {
e.printStackTrace();
}
?
也可以利用其中的一些方法在copy屬性的時(shí)候達(dá)到自己的要求,如:
范例2
? /** */ /** //*
?* Created on 2006-4-26
? */
?package? com.util;
?import? java.beans.PropertyDescriptor;
?import? java.util.Collection;
?import? org.apache.commons.beanutils.PropertyUtils;
?/** */ /**?? */ /** */ /**
?* CopyUtil
?*? @author? Jkallen
? */
? public?? class? CopyUtil?? {
???
???? /** */ /**?? */ /** */ /**
???? * Copy properties of orig to dest
???? * Exception the Entity and Collection Type
???? *? @param? dest
???? *? @param? orig
???? *? @return? the dest bean
????? */
????? public?? static? Object copyProperties(Object dest, Object orig)?? {
???????? if? (dest? ==?? null?? ||? orig? ==?? null )?? {
???????????? return? dest;
??????? }
???????
??????? PropertyDescriptor[] destDesc? =? PropertyUtils.getPropertyDescriptors(dest);
???????? try??? {
???????????? for? ( int? i? =?? 0 ; i? <? destDesc.length; i ++ )?? {
??????????????? Class destType? =? destDesc[i].getPropertyType();
??????????????? Class origType? =? PropertyUtils.getPropertyType(orig, destDesc[i].getName());
???????????????? if (destType? !=?? null?? &&? destType.equals(origType)
???????????????????????? &&?? ! destType.equals(Class. class ))?? {
???????????????????? if ( ! Collection. class .isAssignableFrom(origType))? {???????????????????
???????????????????????? try? {
??????????????????????????? Object value? =? PropertyUtils.getProperty(orig, destDesc[i].getName());
??????????????????????????? PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
??????????????????????? } catch (Exception ex)? {???????????????????????????
??????????????????????? }
??????????????????? }
??????????????? }
??????????? }
???????????
???????????? return? dest;
??????? } catch (Exception ex)?? {
???????????? throw?? new? CopyException(ex);
?//???????????? return dest;
???????? }
??? }????
???
???? /** */ /**?? */ /** */ /**
???? * Copy properties of orig to dest
???? * Exception the Entity and Collection Type
???? *? @param? dest
???? *? @param? orig
???? *? @param? ignores
???? *? @return? the dest bean
????? */
????? public?? static? Object copyProperties(Object dest, Object orig, String[] ignores)?? {
???????? if? (dest? ==?? null?? ||? orig? ==?? null )?? {
???????????? return? dest;
??????? }
???????
??????? PropertyDescriptor[] destDesc? =? PropertyUtils.getPropertyDescriptors(dest);
???????? try??? {
???????????? for? ( int? i? =?? 0 ; i? <? destDesc.length; i ++ )?? {
???????????????? if? (contains(ignores, destDesc[i].getName()))?? {
???????????????????? continue ;
??????????????? }
???????????????
??????????????? Class destType? =? destDesc[i].getPropertyType();
??????????????? Class origType? =? PropertyUtils.getPropertyType(orig, destDesc[i].getName());
???????????????? if (destType? !=?? null?? &&? destType.equals(origType)
???????????????????????? &&?? ! destType.equals(Class. class ))?? {
???????????????????? if ( ! Collection. class .isAssignableFrom(origType))? {
??????????????????????? Object value? =? PropertyUtils.getProperty(orig, destDesc[i].getName());
??????????????????????? PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
??????????????????? }
??????????????? }
??????????? }
???????????
???????????? return? dest;
??????? } catch (Exception ex)?? {
???????????? throw?? new? CopyException(ex);
??????? }
??? }
???
???? static?? boolean? contains(String[] ignores, String name)?? {
???????? boolean? ignored? =?? false ;
???????? for? ( int? j? =?? 0 ; ignores? !=?? null?? &&? j? <? ignores.length; j ++ )?? {
???????????? if? (ignores[j].equals(name))?? {
??????????????? ignored? =?? true ;
???????????????? break ;
??????????? }
??????? }
???????
???????? return? ignored;
??? }
}
?
?
可以看到,在范例1中通過(guò)方法copyProperties的時(shí)候,二者之間在的屬性名必須相同(Copy property values from the origin bean to the destination bean for all cases where the property names are the same)。而在范例2中通過(guò)
? Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
? PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
也是將源與目的之間copy相同的屬性名。而VO是在前臺(tái)顯示,所以難免會(huì)用到PO中所不存在的屬性值。比如PO中可能是一個(gè)對(duì)象,而VO中則可能是此對(duì)象的全部屬性。其中的一些轉(zhuǎn)換則需要依據(jù)前臺(tái)需要針對(duì)性地處理啦!
Reference:? Apache DOC and <>
板橋里人 http://www.jdon.com 2005/09/12
Struts/Tapestry/JSF是目前J2EE表現(xiàn)層新老組合的框架技術(shù)。從誕生時(shí)間上看,Struts應(yīng)該比較早,使用得非常廣泛,Tapestry 3.0逐漸引起廣泛的重視,正當(dāng)Tapestry即將大顯身手時(shí)期,SUN推出JSF標(biāo)準(zhǔn)技術(shù),雖然JSF一開(kāi)始推出尚不成熟,留出了一段空白期,但是隨著JSF1.1標(biāo)準(zhǔn)推出,JSF開(kāi)始正面出擊,粉面隆重登場(chǎng)了。
其實(shí),JSF和Tapestry也并不是那種頭碰頭的相同競(jìng)爭(zhēng)性技術(shù),兩者還是各有側(cè)重點(diǎn)的,不過(guò)比較細(xì)微,但是這種細(xì)微點(diǎn)在實(shí)現(xiàn)一個(gè)大工程時(shí)可能帶來(lái)不同的感受和變化。
首先,我們從一個(gè)高度來(lái)抽象一下表現(xiàn)層框架應(yīng)有的技術(shù)架構(gòu),下圖可以說(shuō)所有表現(xiàn)層框架技術(shù)都必須實(shí)現(xiàn)的功能架構(gòu)圖:
當(dāng)然,我們不必廢話(huà)羅嗦MVC模式,MVC模式是基準(zhǔn)模式,現(xiàn)在框架技術(shù)已經(jīng)不必再拼是否是MVC模式了。 在上圖MVC模式基礎(chǔ)上,一個(gè)表現(xiàn)層框架無(wú)外乎要實(shí)現(xiàn)圖中的三個(gè)功能:
1.在當(dāng)前頁(yè)面能夠顯示一個(gè)組件對(duì)象的內(nèi)容;而不是象純JSP那樣,需要在Jsp頁(yè)面寫(xiě)入“調(diào)用對(duì)象方法”的Java代碼。
2.當(dāng)用戶(hù)按下頁(yè)面的提交按扭或鏈接后,事件發(fā)生,這時(shí)應(yīng)該觸發(fā)服務(wù)器端并將當(dāng)前頁(yè)面的參數(shù)提交給服務(wù)器。這種機(jī)制表現(xiàn)在Form表單提交和有參數(shù)的鏈接<a href=""></a>
3.從一個(gè)頁(yè)面視圖直接跳轉(zhuǎn)到另外一個(gè)頁(yè)面視圖,單純的導(dǎo)航作用。
我們通過(guò)下表來(lái)比較這 三種框架在實(shí)現(xiàn)上圖各個(gè)功能時(shí)技術(shù)細(xì)節(jié),從而得出他們的異同點(diǎn)和偏重點(diǎn)。
|
Struts組件編程模型
Struts實(shí)現(xiàn)組件編程時(shí)有一些復(fù)雜:經(jīng)常為一個(gè)頁(yè)面中需要引入多個(gè)組件而頭疼,因?yàn)镾truts中無(wú)法直接引入多個(gè)組件,必須繞一些圈子:
一般分兩種情況:如果同一個(gè)Action就可以對(duì)付這些組件,那么在這種情況下有兩個(gè)辦法:
1.將這多個(gè)組件裝入一個(gè)ActionForm中,如使用MapForm等機(jī)制;
2.手工將多個(gè)組件裝入request/session等scope中,然后根據(jù)其名稱(chēng)在jsp中獲得。
這兩個(gè)方法都有缺點(diǎn): 第一種辦法經(jīng)常一個(gè)ActionForm弄得面目全非,變成一個(gè)大雜燴,違反了OO分派封裝的原則;第2種辦法其實(shí)又回到j(luò)sp編程;
第二種情況,如果這些組件必須有預(yù)先由不同的Action來(lái)處理,每個(gè)組件必須經(jīng)過(guò)Action -->ActionForm流程,在這種情況下有兩種辦法:
1.使用Tiles, 不同流程輸出到同一個(gè)頁(yè)面的不同區(qū)域。是一種并行處理方式。
2. 對(duì)多個(gè)流程首尾相連,第一Action forward結(jié)果是第二個(gè)Action,最后輸出一個(gè)Jsp,在這個(gè)jsp中就可以使用前面多個(gè)流程的多個(gè)ActionForm了,這屬于串行方式。
Struts組件模型缺點(diǎn)
Struts組件編程必須限定在Action/ActionForm/JSP這三個(gè)框框中做文章,難度相對(duì)比較大,而Tapestry/JSF則沒(méi)有太多這些技術(shù)框框限制,兩者在組件編程方面更讓編程者自由一些,方便一些,這也是組件型框架的優(yōu)勢(shì)吧。
Struts標(biāo)簽庫(kù)
在Struts中,經(jīng)常需要使用標(biāo)簽庫(kù)來(lái)顯示組件ActionForm中內(nèi)容,這就涉及到一個(gè)結(jié)合的問(wèn)題,標(biāo)簽庫(kù)是別人寫(xiě)的,參考Struts的標(biāo)簽庫(kù)用法,而組件是自己的,難度和麻煩就體現(xiàn)在這個(gè)結(jié)合點(diǎn)上。
JSF基本思路和Struts差不多,只不過(guò)換了不同標(biāo)簽庫(kù),也需要標(biāo)簽庫(kù)+組件的結(jié)合思考,不過(guò)因?yàn)榻M件這里是通用組件,沒(méi)有什么限制,所以這樣比Struts要輕松一些。
Tapestry使用了組件庫(kù)概念替代了標(biāo)簽庫(kù),沒(méi)有標(biāo)簽庫(kù)概念,這樣就沒(méi)有標(biāo)簽庫(kù)和自己的組件需要結(jié)合的問(wèn)題,都是組件的使用,組件中分Tapestry標(biāo)準(zhǔn)組件和自己定義的組件,這也是接觸了Jsp體系的人學(xué)習(xí)Tapestry面臨的一個(gè)思路轉(zhuǎn)換。
具體以頁(yè)面跳轉(zhuǎn)為例子,頁(yè)面跳轉(zhuǎn)是靠鏈接<a href="目標(biāo)"></a> 實(shí)現(xiàn),鏈接是頁(yè)面經(jīng)常使用的元素。
Struts提供的html:link在頻繁使用就特別不方便,尤其在傳遞多個(gè)參數(shù)時(shí):其中html:link的page值,是跳轉(zhuǎn)對(duì)方頁(yè)面或Action的path,這個(gè)path一般需要到struts-config.xml查找Action的相應(yīng)path,一旦配置文件path值修改,涉及到這個(gè)所有相關(guān)頁(yè)面都要修改。
JSF將鏈接概念劃分兩個(gè)方面:導(dǎo)航性質(zhì)和事件激活,在導(dǎo)航方面還是需要到配置faces-config查詢(xún)Navigation的from-outcome的值。
由于Tapestry沒(méi)有標(biāo)簽庫(kù)概念,只有組件或頁(yè)面兩個(gè)概念,因此,鏈接跳轉(zhuǎn)目標(biāo)要么是組件,要么是頁(yè)面,簡(jiǎn)潔簡(jiǎn)單,它沒(méi)有多余的path概念,就是組件名,也就是對(duì)象名稱(chēng),組件名稱(chēng)和path名稱(chēng)合二為一。
總結(jié)
JSF在很大程度上類(lèi)似Struts,而不是類(lèi)似Tapestry,可以說(shuō)是一種Struts 2.0,都是采取標(biāo)簽庫(kù)+組件的形式,只是JSF的組件概念沒(méi)有象Struts那樣必須繼承ActionForm的限制;JSF在事件粒度上要細(xì)膩,不象Struts那樣,一個(gè)表單一個(gè)事件,JSF可以細(xì)化到表單中的每個(gè)字段上。
JSF只有在組件和事件機(jī)制這個(gè)概念上類(lèi)似Tapestry,但是不似Tapestry那樣是一個(gè)完全組件的框架,所以,如果你做一個(gè)對(duì)頁(yè)面要求靈活度相當(dāng)高的系統(tǒng),選用Tapestry是第一考慮。
Struts/JSF則適合在一般的數(shù)據(jù)頁(yè)面錄入的系統(tǒng)中,對(duì)于Struts和JSF的選用,我目前個(gè)人觀點(diǎn)是:如果你是一個(gè)新的系統(tǒng),可以直接從JSF開(kāi)始;如果你已經(jīng)使用Struts,不必轉(zhuǎn)換,如果需要切換,可以將JSF和Tapestry一起考慮。
另外,JSF/Tapestry不只是支持Html,也支持多種客戶(hù)端語(yǔ)言如WML或XUI等。
這三者之間關(guān)系:如果說(shuō)Struts是左派;那Tapestry則是右派;而JSF則是中間派,中庸主義是SUN聯(lián)盟的一貫策略。