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卻沒有了這兩個文件,它將自已的安裝路徑和jdk的路徑都寫到注冊表中了.
這就需要在打包時動態(tài)的修改注冊表,否則將不能正常使用.
-- 安裝路徑:your_variable
--Dialog.Message(your_variable, your_variable, MB_OK, MB_ICONEXCLAMATION);
-- 創(chuàng)建一個全局變量,并賦予一個字符串
your_variable1 = "";
--Dialog.Message(your_variable, your_variable, MB_OK, MB_ICONEXCLAMATION);
-- 從注冊表中導(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);
-- 從注冊表中導(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 輸入一個文件夾
?? * zipFileName 輸出一個壓縮文件夾
?? */
? 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: 此類用于...
?*
?* @author??? wunaigang(2005-6-21)
?* @version?? 1.0.0
?*/
public class ZipManager {
?
??????? /**
???????? * zip壓縮功能測試. 將d:\\temp\\zipout目錄下的所有文件連同子目錄壓縮到d:\\temp\\out.zip.
???????? *
???????? * @param baseDir 所要壓縮的目錄名(包含絕對路徑)
???????? * @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)建一個ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????????????????????? ze = new ZipEntry(getAbsFileName(baseDir, f));
??????????????????????????????? ze.setSize(f.length());
??????????????????????????????? ze.setTime(f.lastModified());
?
??????????????????????????????? //將ZipEntry加到zos中,再寫入實際的文件內(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壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中
???????? *
???????? * @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)建一個ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????? ze = new ZipEntry(sourceFile.getName());
??????????????? ze.setSize(sourceFile.length());
??????????????? ze.setTime(sourceFile.lastModified());
?
??????????????? //將ZipEntry加到zos中,再寫入實際的文件內(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壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中
???????? *
???????? * @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)建一個ZipEntry,并設(shè)置Name和其它的一些屬性
??????????????? ze = new ZipEntry(sourceFile.getName());
??????????????? ze.setSize(sourceFile.length());
??????????????? ze.setTime(sourceFile.lastModified());
?
??????????????? //將ZipEntry加到zos中,再寫入實際的文件內(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();
??????? }
?
??????? /**
???????? * 測試解壓縮功能. 將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中得到一個ZipEntry
????????????????????? ze=(ZipEntry)zList.nextElement();
????????????????????? if(ze.isDirectory()){
????????????????????? continue;
????????????????????? }
????????????????????? //以ZipEntry為參數(shù)得到一個InputStream,并寫到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;
??????? }
?
??????? /**
???????? * 給定根目錄,返回一個相對路徑所對應(yīng)的實際文件名.
???????? *
???????? * @param baseDir
???????? *??????????? 指定根目錄
???????? * @param absFileName
???????? *??????????? 相對路徑名,來自于ZipEntry中的name
???????? * @return java.io.File 實際的文件
???????? */
??????? 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;
??????? }
?
??????? /**
???????? * 給定根目錄,返回另一個文件名的相對路徑,用于zip文件中的路徑.
???????? *
???????? * @param baseDir
???????? *??????????? java.lang.String 根目錄
???????? * @param realFileName
???????? *??????????? java.io.File 實際的文件名
???????? * @return 相對文件名
???????? */
??????? 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中得到一個ZipEntry
??????? ze=(ZipEntry)zList.nextElement();
??????? if(ze.isDirectory()){
??????? continue;
??????? }
??????? //以ZipEntry為參數(shù)得到一個InputStream,并寫到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是獨立的Java Object。
PO是由Hibernate納入其實體容器(Entity Map)的對象,它代表了與數(shù)據(jù)庫中某條記錄對應(yīng)的Hibernate實體,PO的變化在事務(wù)提交時將反應(yīng)到實際數(shù)據(jù)庫中。
實際上,這個VO被用作Data Transfer Object,即所謂的DTO。想必,VO就是Data Access Object ---DAO了啦。為什么要有這二者之分呢?如在傳統(tǒng)的MVC架構(gòu)中,位于Model層的PO,是否允許被傳遞到其他層面。由于PO的更新最終將被映射到實際數(shù)據(jù)庫中,如果PO在其他層面(如View層)發(fā)生了變動,那么可能會對Model層造成意想不到的破壞。
主要想說的還是如何進行二者之間的轉(zhuǎn)換:
屬性復(fù)制可以通過Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/)組件提供的屬性批量復(fù)制功能,避免繁復(fù)的get/set操作。down下來之后,里面的API DOC一應(yīng)俱全。
對于一些無需處理其它處理(如過濾)直接用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屬性的時候達到自己的要求,如:
范例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中通過方法copyProperties的時候,二者之間在的屬性名必須相同(Copy property values from the origin bean to the destination bean for all cases where the property names are the same)。而在范例2中通過
? Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
? PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
也是將源與目的之間copy相同的屬性名。而VO是在前臺顯示,所以難免會用到PO中所不存在的屬性值。比如PO中可能是一個對象,而VO中則可能是此對象的全部屬性。其中的一些轉(zhuǎn)換則需要依據(jù)前臺需要針對性地處理啦!
Reference:? Apache DOC and <>
板橋里人 http://www.jdon.com 2005/09/12
Struts/Tapestry/JSF是目前J2EE表現(xiàn)層新老組合的框架技術(shù)。從誕生時間上看,Struts應(yīng)該比較早,使用得非常廣泛,Tapestry 3.0逐漸引起廣泛的重視,正當Tapestry即將大顯身手時期,SUN推出JSF標準技術(shù),雖然JSF一開始推出尚不成熟,留出了一段空白期,但是隨著JSF1.1標準推出,JSF開始正面出擊,粉面隆重登場了。
其實,JSF和Tapestry也并不是那種頭碰頭的相同競爭性技術(shù),兩者還是各有側(cè)重點的,不過比較細微,但是這種細微點在實現(xiàn)一個大工程時可能帶來不同的感受和變化。
首先,我們從一個高度來抽象一下表現(xiàn)層框架應(yīng)有的技術(shù)架構(gòu),下圖可以說所有表現(xiàn)層框架技術(shù)都必須實現(xiàn)的功能架構(gòu)圖:
當然,我們不必廢話羅嗦MVC模式,MVC模式是基準模式,現(xiàn)在框架技術(shù)已經(jīng)不必再拼是否是MVC模式了。 在上圖MVC模式基礎(chǔ)上,一個表現(xiàn)層框架無外乎要實現(xiàn)圖中的三個功能:
1.在當前頁面能夠顯示一個組件對象的內(nèi)容;而不是象純JSP那樣,需要在Jsp頁面寫入“調(diào)用對象方法”的Java代碼。
2.當用戶按下頁面的提交按扭或鏈接后,事件發(fā)生,這時應(yīng)該觸發(fā)服務(wù)器端并將當前頁面的參數(shù)提交給服務(wù)器。這種機制表現(xiàn)在Form表單提交和有參數(shù)的鏈接<a href=""></a>
3.從一個頁面視圖直接跳轉(zhuǎn)到另外一個頁面視圖,單純的導(dǎo)航作用。
我們通過下表來比較這 三種框架在實現(xiàn)上圖各個功能時技術(shù)細節(jié),從而得出他們的異同點和偏重點。
|
Struts組件編程模型
Struts實現(xiàn)組件編程時有一些復(fù)雜:經(jīng)常為一個頁面中需要引入多個組件而頭疼,因為Struts中無法直接引入多個組件,必須繞一些圈子:
一般分兩種情況:如果同一個Action就可以對付這些組件,那么在這種情況下有兩個辦法:
1.將這多個組件裝入一個ActionForm中,如使用MapForm等機制;
2.手工將多個組件裝入request/session等scope中,然后根據(jù)其名稱在jsp中獲得。
這兩個方法都有缺點: 第一種辦法經(jīng)常一個ActionForm弄得面目全非,變成一個大雜燴,違反了OO分派封裝的原則;第2種辦法其實又回到j(luò)sp編程;
第二種情況,如果這些組件必須有預(yù)先由不同的Action來處理,每個組件必須經(jīng)過Action -->ActionForm流程,在這種情況下有兩種辦法:
1.使用Tiles, 不同流程輸出到同一個頁面的不同區(qū)域。是一種并行處理方式。
2. 對多個流程首尾相連,第一Action forward結(jié)果是第二個Action,最后輸出一個Jsp,在這個jsp中就可以使用前面多個流程的多個ActionForm了,這屬于串行方式。
Struts組件模型缺點
Struts組件編程必須限定在Action/ActionForm/JSP這三個框框中做文章,難度相對比較大,而Tapestry/JSF則沒有太多這些技術(shù)框框限制,兩者在組件編程方面更讓編程者自由一些,方便一些,這也是組件型框架的優(yōu)勢吧。
Struts標簽庫
在Struts中,經(jīng)常需要使用標簽庫來顯示組件ActionForm中內(nèi)容,這就涉及到一個結(jié)合的問題,標簽庫是別人寫的,參考Struts的標簽庫用法,而組件是自己的,難度和麻煩就體現(xiàn)在這個結(jié)合點上。
JSF基本思路和Struts差不多,只不過換了不同標簽庫,也需要標簽庫+組件的結(jié)合思考,不過因為組件這里是通用組件,沒有什么限制,所以這樣比Struts要輕松一些。
Tapestry使用了組件庫概念替代了標簽庫,沒有標簽庫概念,這樣就沒有標簽庫和自己的組件需要結(jié)合的問題,都是組件的使用,組件中分Tapestry標準組件和自己定義的組件,這也是接觸了Jsp體系的人學(xué)習(xí)Tapestry面臨的一個思路轉(zhuǎn)換。
具體以頁面跳轉(zhuǎn)為例子,頁面跳轉(zhuǎn)是靠鏈接<a href="目標"></a> 實現(xiàn),鏈接是頁面經(jīng)常使用的元素。
Struts提供的html:link在頻繁使用就特別不方便,尤其在傳遞多個參數(shù)時:其中html:link的page值,是跳轉(zhuǎn)對方頁面或Action的path,這個path一般需要到struts-config.xml查找Action的相應(yīng)path,一旦配置文件path值修改,涉及到這個所有相關(guān)頁面都要修改。
JSF將鏈接概念劃分兩個方面:導(dǎo)航性質(zhì)和事件激活,在導(dǎo)航方面還是需要到配置faces-config查詢Navigation的from-outcome的值。
由于Tapestry沒有標簽庫概念,只有組件或頁面兩個概念,因此,鏈接跳轉(zhuǎn)目標要么是組件,要么是頁面,簡潔簡單,它沒有多余的path概念,就是組件名,也就是對象名稱,組件名稱和path名稱合二為一。
總結(jié)
JSF在很大程度上類似Struts,而不是類似Tapestry,可以說是一種Struts 2.0,都是采取標簽庫+組件的形式,只是JSF的組件概念沒有象Struts那樣必須繼承ActionForm的限制;JSF在事件粒度上要細膩,不象Struts那樣,一個表單一個事件,JSF可以細化到表單中的每個字段上。
JSF只有在組件和事件機制這個概念上類似Tapestry,但是不似Tapestry那樣是一個完全組件的框架,所以,如果你做一個對頁面要求靈活度相當高的系統(tǒng),選用Tapestry是第一考慮。
Struts/JSF則適合在一般的數(shù)據(jù)頁面錄入的系統(tǒng)中,對于Struts和JSF的選用,我目前個人觀點是:如果你是一個新的系統(tǒng),可以直接從JSF開始;如果你已經(jīng)使用Struts,不必轉(zhuǎn)換,如果需要切換,可以將JSF和Tapestry一起考慮。
另外,JSF/Tapestry不只是支持Html,也支持多種客戶端語言如WML或XUI等。
這三者之間關(guān)系:如果說Struts是左派;那Tapestry則是右派;而JSF則是中間派,中庸主義是SUN聯(lián)盟的一貫策略。