/*
* ConverterServlet.java
*
* Created on December 28, 2004, 12:18 PM
*/
package converter;
import java.io.*;
import java.math.BigDecimal;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Charlie Hunt
* @version 1.0
*/
public class CurrencyConverterServlet extends HttpServlet {
/** Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
*/
public String itsDescription = "A servlet to convert currency, i.e. US dollars to Yen, Yen to Euro.";
/**
*
* @param request
* @param response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
dispatchToWorker(request, response);
}
/**
*
* @param theRequest
* @param theResponse
* @throws java.io.IOException
*/
protected void dispatchToWorker(HttpServletRequest theRequest, HttpServletResponse theResponse)
throws IOException {
PrintWriter out = theResponse.getWriter();
String servletName = getServletName();
out.println("");
out.println("
Enter an amount to convert:
"); out.println(""); String amount = theRequest.getParameter("amount"); if ( amount != null && amount.length() > 0 ) { try { converter.ConverterRemote converter; converter = lookupConverter(); //converter = lookupConverter().create(); BigDecimal d = new BigDecimal(amount); out.println(""); out.println("
"); out.println(amount + " Dollars are " + converter.dollarToYen(d) + " Yen."); out.println("
");
out.println(amount + " Yen are " + converter.yenToEuro(d) + " Euro.");
converter.remove();
} catch (Exception e){
out.println("Cannot lookup or execute EJB!");
}
}
out.println("");
out.println("");
out.close();
}
// GET
method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
private converter.ConverterRemote lookupConverter() {
try {
javax.naming.Context c = new javax.naming.InitialContext();
Object remote = c.lookup("java:comp/env/ejb/Converter");
converter.ConverterRemoteHome rv = (converter.ConverterRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, converter.ConverterRemoteHome.class);
return rv.create();
} catch(javax.naming.NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
throw new RuntimeException(ne);
} catch(javax.ejb.CreateException ce) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
throw new RuntimeException(ce);
} catch(java.rmi.RemoteException re) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
throw new RuntimeException(re);
}
}
//