📄 cmdrecordpurchases2.java
字号:
package com.xyz.command;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xyz.ejb.SalesFacade;
import com.xyz.ejb.SalesFacadeHome;
/**
* @author Mauget
*/
public class CmdRecordPurchases2 implements ICommand {
/**
* @see com.xyz.command.ICommand#execute(HttpServletRequest, HttpServletResponse)
*/
public String execute(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String targetPage = "ErrorPage.jsp";
Long[] itemIds = getItemIds(request);
try {
Context jndiContext = new InitialContext();
Object ref = jndiContext.lookup(facRef);
SalesFacadeHome home =
(SalesFacadeHome) PortableRemoteObject.narrow(
ref,
SalesFacadeHome.class);
SalesFacade facade = home.create();
String zipcode = request.getParameter("zipcode");
if (zipcode != null && facade.makePurchase(zipcode, itemIds))
targetPage = "Purchases2.jsp";
} catch (javax.naming.NamingException ex) {
throw new EJBException();
} catch (java.rmi.RemoteException ex) {
throw new EJBException();
} catch (javax.ejb.CreateException ex) {
throw new EJBException();
}
return targetPage;
}
/**
* Helper method returns an array of Long Item IDs
* @param request java.servlet.HttpServletRequest
* @return Long[]
*/
private Long[] getItemIds(HttpServletRequest request) {
ArrayList list = new ArrayList(20);
Enumeration enum = request.getParameterNames();
while (enum.hasMoreElements()) {
String idStr = (String) enum.nextElement();
if (idStr.startsWith("buy")) {
String key = request.getParameter(idStr);
Long id = new Long(Long.parseLong(key));
list.add(id);
}
}
return (Long[]) list.toArray(new Long[list.size()]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -