📄 servlet.java
字号:
//定义本接口在包examples.ejb.basic.beanManaged
package examples.ejb.basic.beanManaged;
//本类用到的其他类。javax.ejb.*是开发EJB应用需要的类库。javax.naming.*是实现JNDI服务需要的类库
import java.io.IOException;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weblogic.html.BreakElement;
import weblogic.html.HeadingElement;
import weblogic.html.ServletPage;
import weblogic.html.StringElement;
/**
* 这个servlet和Client类类似
* 这个servlet必须在web.xml文件中注册
* 使用url http://localhost:7001/examplesWebApp/beanManaged调用这个servlet
*/
public class Servlet extends HttpServlet {
static String accountId = "10020";
/**
* 查找beanManaged主接口
* 创建beans并调用远程接口中定义的方法
*
* @参数 req HttpServletRequest
* @参数 res HttpServletResponse
* @异常 java.io.IOException
* 如果IO错误
*/
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
//设置文档类型
res.setContentType("text/html");
OutputStream out = res.getOutputStream();
String title = "EJBean Bean-managed Persistence Servlet";
//ServletPage实例
ServletPage sp = new ServletPage(title);
//编辑html元素
sp.getBody()
.addElement(new HeadingElement(title, 1));
printElement("", sp);
// 获取参数
// "http://localhost:7001/beanManaged?user=foobar&password=FooBarNone"
//用户名
String user = req.getParameter("user");
//密码
String password = req.getParameter("password");
if(user!=null && password!=null){
printElement("Using user <b>" + user
+ "</b> and password <b> " + password + "</b>", sp);
printElement("", sp);
} else {
printElement("No user and password credentials", sp);
printElement("", sp);
}
double amount = 100;
double balance = 3000;
Vector v = new Vector();
try {
// 获取上下文
Context ctx = getInitialContext();
//查找主接口
AccountHome home = (AccountHome) ctx.lookup("beanManaged.AccountHome");
// 查找Account或创建它
Account ac = null;
try {
printElement("Looking up account " + accountId + "...", sp);
ac = (Account) home.findByPrimaryKey(accountId);
}
catch (Exception ee) {
//异常处理
printElement("Did not find " + accountId, sp);
}
if (ac == null) {
printElement("Account " + accountId +
" being created; opening balance is $" + balance, sp);
//创建远程对象
ac = home.create(accountId, balance);
}
else {
printElement("Account " + accountId + " found; balance is $" + ac.balance(), sp);
}
printElement("", sp);
// A部分: 存入和提取大于当前结算的金额,会抛出用户定义的异常
printElement("Part A: Depositing $" + amount, sp);
//存入
balance = ac.deposit(amount);
printElement("Current balance is $" + balance, sp);
printElement("", sp);
amount = balance + 10;
try {
printElement("Withdrawing amount greater than current balance. Expecting an exception...", sp);
//提取
balance = ac.withdraw(amount);
printElement("Error: expected an exception.", sp);
}
catch (ProcessingErrorException pe) {
//用户定义的异常
printElement("Received expected Processing Error:<br>" + pe, sp);
}
printElement("", sp);
// B部分: 创建一些新的用户,并给定不同的初始结算
// 查找结算大于给定值的所有账号
// 最后,删除所有新的账号
int numAccounts = 5;
printElement("Part B: Creating " + numAccounts + " new accounts...", sp);
long now = System.currentTimeMillis();
for (int i = 0; i < numAccounts; i++) {
String id = "" + now + i; // 唯一的账号id
balance = i*100; // 初始结算
v.addElement(home.create(id, balance));
printElement("Created account: " + id +
"; balance is $" + balance, sp);
}
printElement("", sp);
if (v.size() == numAccounts) {
printElement("" + numAccounts + " accounts successfully created", sp);
}
else {
printElement("Error: Only " + v.size() +
" accounts were created successfully", sp);
}
printElement("", sp);
double balanceGreaterThan = 200;
printElement("Querying for accounts with a balance greater than " +
balanceGreaterThan + "...", sp);
//调用主接口的方法
Enumeration e = home.findBigAccounts(balanceGreaterThan);
if (e != null) {
while (e.hasMoreElements()) {
//每个账号
Account bigAccount= (Account) e.nextElement();
//打印每个账号的主键
printElement("Account " + bigAccount.getPrimaryKey() +
"; balance is $" + bigAccount.balance(), sp);
Thread.sleep(1000);
}
}
printElement("", sp);
printElement("Removing accounts just created...", sp);
//清除所有账号
for (int i = 0; i < numAccounts; i++) {
String id = "" + now + i;
((Account)(v.elementAt(i))).remove();
printElement("Removed account: " +id, sp);
}
printElement("", sp);
// 捕获异常
}
catch (ProcessingErrorException pe) {
printElement("Unexpected Processing Error: " + pe, sp);
}
catch (Exception e) {
//其它异常
printElement(":::::::::::::: Unexpected Error :::::::::::::::::", sp);
e.printStackTrace();
}
finally {
printElement("End beanManaged.Servlet...", sp);
}
sp.output(out);
out.flush();
}
/**
* 基本的servlet信息
*/
public String getServletInfo() {
return "EJBean Servlet";
}
/**
* 打印一个bean的HTML信息
*/
static private void printAccount(Account account, ServletPage sp)
throws RemoteException, IOException {
printElement("Account " + account.getPrimaryKey() +
"; balance is $" + account.balance(), sp);
}
/**
* 打印消息给浏览器
*/
static private void printElement(String message, ServletPage sp)
throws IOException {
sp.getBody()
.addElement(new BreakElement())
.addElement(new StringElement(message));
}
/**
* 获取当前用户、密码和url的初始化上下文。
*
* @return 上下文
* @异常 java.lang.Exception if there is
* an error in getting the Context
*/
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
return new InitialContext(p);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -