001: /*
002: * ConverterServlet.java
003: *
004: * Created on 04 May 2005, 18:38
005: */
006:
007: package converter;
008:
009: import java.io.*;
010: import java.net.*;
011:
012: import javax.servlet.*;
013: import javax.servlet.http.*;
014:
015: /**
016: *
017: * @author Administrator
018: * @version
019: */
020: public class ConverterServlet extends HttpServlet {
021:
022: /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
023: * @param request servlet request
024: * @param response servlet response
025: */
026: protected void processRequest(HttpServletRequest request,
027: HttpServletResponse response) throws ServletException,
028: IOException {
029: response.setContentType("text/html;charset=UTF-8");
030: PrintWriter out = response.getWriter();
031: out.println("<html>");
032: out.println("<head>");
033: out.println("<title>Servlet ConverterServlet</title>");
034: out.println("</head>");
035: out.println("<body>");
036: out.println("<h1><b><center>Converter</center></b></h1>");
037: out.println("<hr>");
038: out.println("<p>Enter an amount to convert:</p>");
039: out.println("<form method=\"get\">");
040: out
041: .println("<input type=\"text\" name=\"amount\" size=\"25\">");
042: out.println("<br>");
043: out.println("<p>");
044: out.println("<input type=\"submit\" value=\"Submit\">");
045: out.println("<input type=\"reset\" value=\"Reset\">");
046: out.println("</form>");
047: String amount = request.getParameter("amount");
048: if (amount != null && amount.length() > 0) {
049: try {
050: converter.ConverterRemote converter;
051: converter = lookupConverterBean();
052:
053: java.math.BigDecimal d = new java.math.BigDecimal(
054: amount);
055: out.println("<p>");
056: out.println("<p>");
057: out.println(amount + " Dollars are "
058: + converter.dollarToYen(d) + " Yen.");
059: out.println("<p>");
060: out.println(amount + " Yen are "
061: + converter.yenToEuro(d) + " Euro.");
062:
063: converter.remove();
064: } catch (Exception e) {
065: out.println("Cannot lookup or execute EJB!");
066: }
067:
068: }
069: out.println("</body>");
070: out.println("</html>");
071: out.close();
072: }
073:
074: // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
075: /** Handles the HTTP <code>GET</code> method.
076: * @param request servlet request
077: * @param response servlet response
078: */
079: protected void doGet(HttpServletRequest request,
080: HttpServletResponse response) throws ServletException,
081: IOException {
082: processRequest(request, response);
083: }
084:
085: /** Handles the HTTP <code>POST</code> method.
086: * @param request servlet request
087: * @param response servlet response
088: */
089: protected void doPost(HttpServletRequest request,
090: HttpServletResponse response) throws ServletException,
091: IOException {
092: processRequest(request, response);
093: }
094:
095: /** Returns a short description of the servlet.
096: */
097: public String getServletInfo() {
098: return "Short description";
099: }
100:
101: // </editor-fold>
102:
103: private converter.ConverterRemote lookupConverterBean() {
104: try {
105: javax.naming.Context c = new javax.naming.InitialContext();
106: Object remote = c.lookup("java:comp/env/ejb/ConverterBean");
107: converter.ConverterRemoteHome rv = (converter.ConverterRemoteHome) javax.rmi.PortableRemoteObject
108: .narrow(remote, converter.ConverterRemoteHome.class);
109: return rv.create();
110: } catch (javax.naming.NamingException ne) {
111: java.util.logging.Logger.getLogger(getClass().getName())
112: .log(java.util.logging.Level.SEVERE,
113: "exception caught", ne);
114: throw new RuntimeException(ne);
115: } catch (javax.ejb.CreateException ce) {
116: java.util.logging.Logger.getLogger(getClass().getName())
117: .log(java.util.logging.Level.SEVERE,
118: "exception caught", ce);
119: throw new RuntimeException(ce);
120: } catch (java.rmi.RemoteException re) {
121: java.util.logging.Logger.getLogger(getClass().getName())
122: .log(java.util.logging.Level.SEVERE,
123: "exception caught", re);
124: throw new RuntimeException(re);
125: }
126: }
127: }
|