| package org.jboss.docs.interest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.docs.interest.Interest;
import org.jboss.docs.interest.InterestHome;
/**
* This Servlet provides a user interface to an Enterprise Java Bean.
* The example EJB described in the jBoss documentation at
* http://jboss.org/ calculates compound interest.
* This servlet will call the Interest EJB, passing it a few
* parameters. The EJB will return a result, which the servlet
* will display to the Web client.
*/
public class InterestServlet extends HttpServlet
{
private InterestHome interestHome = null;
/** Looks up the InterestHome interface and saves it for use in
doGet().
*/
public void init() throws ServletException
{
try
{
//下面开始调用EJB
// Get a naming context 首先定义一个naming context
InitialContext jndiContext = new InitialContext();
// Get a reference to the Interest Bean 通过JNDI寻找到EJB
//这里JNDI将 通过jboss.xml获得EJB
Object ref = jndiContext.lookup("java:comp/env/ejb/Interest");
// Get a reference from this to the Bean's Home interface
//获得一个远程的EJB EJB2.0里,这里可优化成调用本地EJB
interestHome = (InterestHome) PortableRemoteObject.narrow(ref, InterestHome.class);
}
catch(Exception e)
{
throw new ServletException("Failed to lookup
java:comp/env/ejb/Interest", e);
}
}
/**
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String title = "Servlet interface to EJB";
double principal = getValue("principal", 1000.0, request);
double rate = getValue("rate", 10.0, request);
double periods = getValue("periods", 2.0, request);
// set content type and other response header fields first
response.setContentType("text/html");
// then write the data of the response
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<H2>Calling EJB...</H2>");
//以上是定义html输出
try
{
// Create an Interest object from the Home interface
Interest bean = interestHome.create();
out.print("Interest on "+principal);
out.print(" units, at "+rate);
out.print("% per period, compounded over "+periods);
out.println(" periods is: ");
// call the calculateCompoundInterest() method to do the calculation
out.println(bean.calculateCompoundInterest(principal, rate/100, periods));
bean.remove();
}
catch(Exception e)
{
out.println(e.toString());
}
finally
{
out.println("</BODY></HTML>");
out.close();
}
}
private double getValue(String name, double defaultValue, HttpServletRequest request)
{
double value = defaultValue;
String pvalue = request.getParameter(name);
if( pvalue != null )
{
try
{
value = Double.valueOf(pvalue).doubleValue();
}
catch(NumberFormatException e)
{
}
}
return value;
}
}
|