⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tradercontroller.java

📁 java ejb----html jsp
💻 JAVA
字号:
package com.genuitec.traderx.web;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.genuitec.traderx.ejb.TradeResult;
import com.genuitec.traderx.interfaces.Trader;
import com.genuitec.traderx.interfaces.TraderHome;

/**
 * @version 	1.0
 * @author
 */
public class TraderController {

    public static final String RESULT_URL = "/jsp/result.jsp";

    private HttpServlet servlet;
    private TraderHome traderHome;

    /**
     * Constructor for TraderController.
     */
    public TraderController(
        HttpServlet theServlet,
        String theJNDIName)
        throws NamingException {

        servlet = theServlet;
        traderHome = lookupHome(theJNDIName);
    }

    public void doTrade(
        HttpServletRequest theRequest,
        HttpServletResponse theResponse) {

        TradeResult result = null;
        //no big deal
        System.out.println("#doTrade");
        OrderForm orderForm = new OrderForm();

        orderForm.populate(theRequest);

        //terminate operation if errors
        if (orderForm.hasErrors()) {
            ViewDispatcher.displayErrors(theRequest, theResponse, orderForm.getErrors());
        }

        //perform transaction using Trader EJB
        try {
            if (orderForm.isSell()) {
                result = getTrader().sell(orderForm.getSymbol(), orderForm.getQuantity());
            } else {
                result = getTrader().buy(orderForm.getSymbol(), orderForm.getQuantity());
            }

            theRequest.setAttribute("TradeResult", result);
            System.out.println(
                "result: " + result.getStockSymbol() + "  " + result.getNumberTraded());

            ViewDispatcher.includeRequest(theRequest, theResponse, RESULT_URL);

        } catch (RemoteException ex) {
            ViewDispatcher.displayError(
                theRequest,
                theResponse,
                "Remote exception while performing trade.");
        } catch (Exception ex) {
            ViewDispatcher.displayError(
                theRequest,
                theResponse,
                "Exception creating Trader");
        }
    }

    public Trader getTrader() throws RemoteException, NamingException, CreateException {
        return traderHome.create();
    }

    public TradeResult doSell(String theSymbol, int theShareCount)
        throws RemoteException, NamingException, CreateException {

        return getTrader().sell(theSymbol, theShareCount);
    }

    public TradeResult doBuy(String theSymbol, int theShareCount)
        throws RemoteException, NamingException, CreateException {

        return getTrader().buy(theSymbol, theShareCount);
    }

    /**
     * RMI/IIOP clients should use this narrow function
     */
    private Object narrow(Object ref, Class c) {
        return PortableRemoteObject.narrow(ref, c);
    }

    /**
     * Lookup the EJBs home in the JNDI tree
     */
    private TraderHome lookupHome(String theJNDIName)
        throws NamingException {

        // Lookup the beans home using JNDI
        Context ctx = getInitialContext();

        try {
            Object home = ctx.lookup(theJNDIName);
            log("Looking up name " + theJNDIName);
            return (TraderHome)home; // breaks on wls 6. (TraderHome) narrow(home, TraderHome.class);
        } catch (NamingException ne) {
            log("Unable to lookup the TraderBeanHome.  Please make sure ");
            log(
                "that you have deployed the ejb with the JNDI name "
                    + theJNDIName
                    + " on the server");
            throw ne;
        }
    }

    /**
     * Using a Properties object will work on JDK 1.1.x and Java2
     * clients
     */
    private Context getInitialContext() throws NamingException {

        try {
            // Get an InitialContext
            return new InitialContext();

        } catch (NamingException ne) {
            log("We were unable to get a connection to the server with new InitialContext().  ");
            log("Please make sure that the server is running.");
            throw ne;
        }
    }

    private void log(Object theMsg) {
        servlet.log(theMsg.toString());
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -