Anda di halaman 1dari 3

package bonus.controllers; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.

HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import bonus.calculation.*; /** * This servlet accepts user data and * returns the calcuated bonus. */ public class BonusCalculationServlet extends HttpServlet { /** * Home to create BonusCalculator session beans */ protected BonusCalculatorHome bonusCalculatorHome; /** * This method will be called by the Servlet container when * this servlet is being placed into service.<br> * This method does a JNDI lookup, obtains the * session bean's home interface and stores it in the * instance attribute for future use. * @param config - the <code>ServletConfig</code> object that * contains configutation information for this servlet * @exception ServletException if an exception occurs that * interrupts the servlet's operation */ public void init(ServletConfig config) throws ServletException { System.out.println("BonusCalculationServlet: init()"); try { Context context = new InitialContext(); Object homeObject = context.lookup("BonusCalculatorHome"); bonusCalculatorHome = (BonusCalculatorHome)PortableRemoteObject.narrow( homeObject, BonusCalculatorHome.class); } catch (Exception exception) {

exception.printStackTrace(); throw new ServletException( "BonusCalculatorHome could not be created due to " + exception. getMessage()); } } /** * This method handles the HTTP GET requests for this servlet. * It calculates the bonus using {@link BonusCalculator session * bean} on the data passed in the request * and sends the bonus as a html page response. * @param request - object that contains the request the * client has made of the servlet * @param response - object that contains the response the servlet * sends to the client * @exception java.io.IOException - if an input or output error is * detected when the servlet handles the GET request * @exception ServletException - if the GET request could not be * */ public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //Get the multiplier and the social Security Information String inputMultiplier = request.getParameter("multiplier"); Integer multiplierAsInteger = new Integer(inputMultiplier); int multiplier = multiplierAsInteger.intValue(); String ssn = request.getParameter("ssn"); // calculate the bonus using the BonusCalculator double inputBonus = 100.00; // create an instance of the session bean BonusCalculator bonusCalculator = bonusCalculatorHome.create(); double bonus = bonusCalculator.calculateBonus(multiplier, inputBonus); // write the response to be displayed response.setContentType("text/html"); PrintWriter out = response.getWriter();

handled

out.println("<HTML><HEAD><TITLE>"); String title = "Servlet Example - Bonus Calculator"; out.println(title); out.println("</TITLE></HEAD><BODY>"); out.println("<H1>Bonus Calculation:</H1>"); out.println("<P>Social Security Number: " + ssn + "<P>"); out.println("<P>Multiplier: " + multiplier + "<P>"); out.println("<P>Bonus Amount: " + bonus + "<P>"); out.println("</BODY></HTML>"); out.close(); } catch(Exception exception) { exception.printStackTrace(); } } /** * This method will be called by the Servlet Container when this * servlet is being taken out of service */ public void destroy() { System.out.println("BonusCalculationServlet: destroy()"); }