📄 helloejbservlet.java
字号:
/*
*
* Copyright 2002 Author,Zhang Hongzhan. All Rights Reserved.
*
* This software is the proprietary information of Author.
*
*/
import helloejb.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class HelloEJBServlet extends HttpServlet{
private static final String JNDI_NAME = "HelloEJB";
private static final String url = "t3://localhost:7001";
public void init(ServletConfig config) throws ServletException{
super.init(config);
//set self environment variable
}
public void service(HttpServletRequest req,HttpServletResponse resp)
throws IOException{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
try{
// Get an InitialContext
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, url);
Context ctx = new InitialContext(env);
Object h=ctx.lookup(JNDI_NAME);
HelloEJBHome home=(HelloEJBHome)PortableRemoteObject.narrow(h,
HelloEJBHome.class);
HelloEJBRemote currencyHelloWorld =home.create();
String hello=currencyHelloWorld.HelloWorld();
//print out our Text
out.println("<html><head><title>Hello World!</title></head><body>");
out.println("<h4>My EJB said:"+hello+"</h4>");
BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyHelloWorld.dollarToRMB(param);
out.println("<h4>100 dollar = "+amount+"RMB</h4>");
amount = currencyHelloWorld.poundToRMB(param);
out.println("<h4>100 pound = "+amount+"RMB</h4>");
out.println("</body></html>");
}catch(Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -