⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch14.txt

📁 精通Java网络编程代码全部
💻 TXT
字号:
/* 代码14-1
 * Created on 2005-5-22
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.*;
import javax.naming.InitialContext;
public class HelloServlet extends HttpServlet{
	public void service(HttpServletRequest req,HttpServletResponse res) throws
		IOException {
		res.setContentType("text/html");
		PrintWriter out =res.getWriter();//设置输出流
		out.println("<html><head><title>the first EJB</title></head>");
		try{
			InitialContext ctx=new InitialContext();
			Object objRef = ctx.lookup("java:comp/env/ejb/Hello");
			//主接口
			HelloHome
				home=(HelloHome)javax.rmi.PortableRemoteObject.narrow(
				objRef,HelloHome.class);
			//组件接口
			Hello bean =home.create();
			out.println(bean.getHello());
		}catch(javax.naming.NamingException ne){
			out.println("Naming Exception caught:"+ne);
			ne.printStackTrace(out);
		}catch(javax.ejb.CreateException ce){
			out.println("Create Exception caught:"+ce);
			ce.printStackTrace(out);
		}catch(java.rmi.RemoteException re){
			out.println("Remote Exception caught:"+re);
			re.printStackTrace(out);
		}
		out.println("</body></html>");
	}
}

/* 代码14-2
 * Created on 2005-5-22
 */

StatelessDate.java 
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.util.Date;
public interface StatelessDate extends EJBObject{
	public int getDayInRange(Date lowerLimitDate,Date upperLimitDate)
		throws RemoteException,InsufficientDateException;//获取日期信息
	public int getDayForOlympic()
		throws RemoteException,InsufficientDateException;
}

/* 代码14-3
 * Created on 2005-5-22
 */

StatelessDateEJB.java 
import javax.ejb.*;
import java.util.Date;
public class StatelessDateEJB implements SessionBean{
	public void ejbCreate(){}
	public void ejbRemove(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void setSessionContext(SessionContext ctx){}
	//计算两个日期之间相隔的天数
	public int getDayInRange(Date lowerLimitDate,Date upperLimitDate)
		throws InsufficientDateException{
		long upperTime,lowerTime;
		upperTime=upperLimitDate.getTime();
		lowerTime=lowerLimitDate.getTime();
		if(upperTime<lowerTime)
			throw new InsufficientDateException();
		Long result=new Long((upperTime-lowerTime)/(1000*60*60*24));
		return result.intValue();
	}
	//得到距离2008 年奥运会天数
	public int getDayForOlympic()
		throws InsufficientDateException {
		Date olympic=new Date("2008/01/01");
		Date today=new Date(System.currentTimeMillis());
		return getDayInRange(today,olympic);
	}
}

/* 代码14-4
 * Created on 2005-5-22
 */

//StatelessDateServlet .java 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.*;
import javax.naming.InitialContext;
public class StatelessDateServlet extends HttpServlet{
	public void service(HttpServletRequest req,HttpServletResponse res) throws IOException {
		res.setContentType("text/html");//设置标题
		PrintWriter out =res.getWriter();//设置输出流
		out.println("<html><head><title>StatelessDateServlet</title></head>");
		out.println("<body><h2>Test Result:<hr>");
		try{
			InitialContext ctx =new InitialContext();
			Object objRef =ctx.lookup("java:comp/env/ejb/StatelessDate");
			StatelessDateHome home=(StatelessDateHome)
				javax.rmi.PortableRemoteObject.narrow(
				objRef,StatelessDateHome.class);//采用无状态连接方式
			StatelessDate bean=home.create();//无状态控件
			java.util.Date lowerLimitDate= new java.util.Date(new
				String(req.getParameter("lowerLimitDate")));
			java.util.Date upperLimitDate= new java.util.Date(new
				String(req.getParameter("upperLimitDate")));
			//以下为打印各种信息
			out.println("the lowerLimitDate:"+lowerLimitDate+"<br>");
			out.println("the upperLimitDate:"+upperLimitDate+"<br>");
			out.println("the method getDayInRange()
Result:"+bean.getDayInRange(lowerLimitDate,upperLimitDate)+" days");
	   out.println("<hr>");
			out.println("the method getDayForOlympic() Result:"+bean.getDayForOlympic()+"
				days");
				bean=null;
		}catch(javax.naming.NamingException ne){
			out.println("Naming Exception caught:"+ne);
			ne.printStackTrace(out);//打印异常信息
		}catch(javax.ejb.CreateException ce){
			out.println("Create Exception caught:"+ce);
			ce.printStackTrace(out);
		}catch(java.rmi.RemoteException re){
			out.println("Remote Exception caught:"+re);
			re.printStackTrace(out);
		}catch(InsufficientDateException ie){
			out.println("InsufficientDate Exception caught:"+ie);
			ie.printStackTrace(out);
		}
		out.println("</body></html>");
	}
}

/* 代码14-5
 * Created on 2005-5-22
 */

//StatefulAccountEJB.java
import javax.ejb.*;
public class StatefulAccountEJB implements SessionBean{
	public void ejbCreate(double fund)throws CreateException{
		if (fund<0)
			throw new CreateException("Invalid fund");
		this.fundBalance=fund;
	}
	public void ejbRemove(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void setSessionContext(SessionContext ctx){}
	//实例变量,有状态的会话bean 将在组件池中维护这个实例的值
	private double fundBalance;
	//向基金帐户增加基金
	public void addFunds(double fund)throws InsufficientFundException{
		if (fund<0)
			throw new InsufficientFundException("Invalid fund");
		this.fundBalance+=fund;
	}
	//从基金帐户撤除部分基金
	public void removeFunds(double fund)throws InsufficientFundException{
		if(fund<0)
			throw new InsufficientFundException("Invalid fund");
		if(this.fundBalance<fund)
			throw new InsufficientFundException("the balance less than fund");
		this.fundBalance-=fund;
	}
	//得到基金帐户的余额
	public double getBalance(){
		return this.fundBalance;
	}
}

/* 代码14-6
 * Created on 2005-5-22
 */

//StatefulAccountServlet .java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.*;
import javax.naming.InitialContext;
public class StatefulAccountServlet extends HttpServlet{
	public void service(HttpServletRequest req,HttpServletResponse res) throws IOException {
		res.setContentType("text/html");//设置标题信息
		PrintWriter out =res.getWriter();
		out.println("<html><head><title>StatefulAccountServlet</title></head>");
		out.println("<body><h2>Test Result:<hr>");
		try{
			InitialContext ctx =new InitialContext();
			Object objRef =ctx.lookup("java:comp/env/ejb/StatefulAccount");
			StatefulAccountHome home=(StatefulAccountHome)
				javax.rmi.PortableRemoteObject.narrow(
				objRef,StatefulAccountHome.class);
			//得到一个基金帐户对象的引用,并初始化帐户金额为100000
			StatefulAccount bean=home.create(100000);
			out.println("the account balance:"+bean.getBalance()+"<br>");
			//向基金帐户增加5000.25
			bean.addFunds(5000.25);
			out.println("method of addFunds(5000.25) Result:"+bean.getBalance()+"<br>");
			//从基金帐户调出1000.02
			bean.removeFunds(1000.02);
			out.println("method of removeFunds(1000.02) Result:"+bean.getBalance());
			out.println("<hr>");
			out.println("current account balance:"+bean.getBalance());
			bean=null;
		}catch(javax.naming.NamingException ne){
			out.println("Naming Exception caught:"+ne);
			ne.printStackTrace(out);
		}catch(javax.ejb.CreateException ce){
			out.println("Create Exception caught:"+ce);
			ce.printStackTrace(out);
		}catch(java.rmi.RemoteException re){
			out.println("Remote Exception caught:"+re);
			re.printStackTrace(out);
		}catch(InsufficientFundException ie){
			out.println("InsufficientFund Exception caught:"+ie);
			ie.printStackTrace(out);
		}
		out.println("</body></html>");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -