📄 helloejbservlet.java
字号:
/*
*
* Copyright 2002 Author,Zhang Hongzhan. All Rights Reserved.
*
* This software is the proprietary information of Author.
*
*/
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>");
//System.out.println("My EJB said:"+hello);
out.println("<h1>My EJB said:"+hello+"</h1>");
BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyHelloWorld.dollarToRMB(param);
//System.out.println("100 dollar = "+amount+" RMB");
out.println("<h1>100 dollar = "+amount+"RMB</h1>");
amount = currencyHelloWorld.poundToRMB(param);
//System.out.println("100 pound = "+amount+" RMB");
out.println("<h1>100 pound = "+amount+"RMB</h1>");
System.out.println("</body></html>");
//System.exit(0);
}catch(Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
/*private static void log(String s) {
System.out.println(s);
}
public static void main(String[] args) {
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();
System.out.println("My EJB said:"+hello);
BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyHelloWorld.dollarToRMB(param);
System.out.println("100 dollar = "+amount+" RMB");
amount = currencyHelloWorld.poundToRMB(param);
System.out.println("100 pound = "+amount+" RMB");
System.exit(0);
}catch(Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -