The AtmServlet Class


The AtmServlet Class

We will declare a class named AtmServlet that will serve as the servlet for our web-based atmosphere modeling application. The class will extend the HttpServlet class. Since the USatm76 object only requires two input parameters, we will use an HTTP GET command for the client request. In order to respond to GET commands, the AtmServlet class will override the doGet() method.

The first thing the doGet() method does is to extract the input parameters from the query string. If this is the first time the servlet is accessed, there won't be a query string. In this case, the inputs are given default values. If there is a query string, the values are extracted from it using the HttpServletRequest object and the getParameter() method.

Next the doGet() method writes the GUI front end for the atmosphere modeling application back to the client machine. The HttpServletResponse object acquires a PrintStream that is connected to the client machine. The PrintStream uses the println() method to send an HTML page with GUI components back to the client machine. In this case, the GUI components consist of a list component that define the system of units to be used, a text field into which the desired altitude is typed, and a button that when pressed sends the HTTP request to the server. If you try this example, you will have to change www.JackZack.com to whatever the name of your server is.

If this is not the first time the servlet is run, the doGet() method then creates a USatm76 object. The atmospheric conditions represented by the USatm76 object are written back to the client machine. One interesting thing to note about the AtmServlet.java code is that when the Compute button is pressed, the servlet creates a USatm76 object and then calls itself to update the display on the client machine. When the servlet is initially invoked, the HTTP GET command won't include a query string. In this case, the application is not run and the servlet simply displays the GUI front-end with default input values.

The AtmServlet class source code is shown here.

 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class AtmServlet extends HttpServlet {   //  The doGet() method is called when the servlet   //  is invoked   public void doGet(HttpServletRequest request,                     HttpServletResponse response)                   throws ServletException, IOException {     String line, units, altitude;     boolean firstTime = false;     USatm76 atm;     //  The first time this servlet is accessed, the     //  query string might be null. If this is the case,     //  provide defaults for the inputs. Otherwise,     //  extract the inputs from the query string using     //  the HttpServletRequest object.     if ( request.getQueryString() == null ) {       units = "SI";       altitude = "20000.0";       firstTime = true;     } else {       units = request.getParameter("units");       altitude = request.getParameter("altitude");     }     //  Use the HttpServletResponse object to specify     //  the response type and to open an output stream.     response.setContentType("text/html");     PrintWriter pw = response.getWriter();     //  Start creating the output HTML file that will     //  be displayed by the browser.     char dq = '\"';     pw.println("<HTML><HEAD><TITLE>"+               "Atmospheric Model Application</TITLE>");     pw.println("</HEAD><BODY>");     //  Create the Client GUI.  This contains the input     //  parameter components and the "run" button.  When     //  the "Compute" button is selected, the HTTP     //  request is sent to the server.     pw.println("<FORM  METHOD=GET ACTION=" + dq +       http://www.JackZack.com:8080/servlets/AtmServlet +                  dq+">");     //  Create the units list component.  The if     //  statement insures that whatever species is     //  selected will still be selected when the screen     //  is refreshed.     pw.println("Units");     pw.println("<SELECT NAME=units SIZE=1 >");     if ( units.equals("English") ) {       pw.println("<OPTION VALUE=SI>SI</OPTION>");       pw.println("<OPTION SELECTED                    VALUE=English>English"+                  "</OPTION SELECTED>");     } else {       pw.println("<OPTION VALUE=SI>SI</OPTION>");       pw.println("<OPTION                    VALUE=English>English</OPTION>");     }     pw.println("</SELECT><BR><BR><BR>");     //  Create the altitude text field component     pw.println("altitude (ft or m) ");     pw.println("<INPUT TYPE=TEXT NAME=altitude                  VALUE="+altitude+"><BR><BR>");     //  Create the run button component     pw.println("Compute atmospheric conditions");     pw.println("<INPUT TYPE=submit NAME=compute                   VALUE="+dq+"Compute"+dq+">");     pw.println("</FORM>");     //  If this is not the first time the servlet is     //  accessed (i.e., if nondefault input parameters     //  exist), create a USatm76 object     if ( !firstTime )     {       atm = new USatm76(units,                         Double.parseDouble(altitude));     //  Display the results in a nice, column-aligned     //  table       pw.println("<HR WIDTH=100% ALIGN=LEFT>");       pw.println("<BR>");       pw.println("Results");       pw.println("<BR><BR>");       String label[] = atm.getLabels();       pw.println("<TABLE BORDER=0 CELLSPACING=0                     CELLPADDING=0>");       pw.println("<TR>");         pw.println("<TD ><TT> geometric altitude                         </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+                        atm.getAltitude()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[0]+"</TT></TD>");       pw.println("</TR>");       pw.println("<TR>");         pw.println("<TD ><TT> geopotential altitude                         </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+             atm.getGeoPotentialAltitude()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[1]+"</TT></TD>");       pw.println("</TR>");       pw.println("<TR>");         pw.println("<TD ><TT> temperature                         </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+                    atm.getTemperature()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[2]+"</TT></TD>");       pw.println("</TR>");       pw.println("<TR>");         pw.println("<TD ><TT> pressure </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+                        atm.getPressure()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[3]+"</TT></TD>");       pw.println("</TR>");       pw.println("<TR>");         pw.println("<TD ><TT> molar mass                         </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+                        atm.getMolarMass()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[4]+"</TT></TD>");       pw.println("</TR>");       pw.println("<TR>");         pw.println("<TD ><TT> density </TT></TD>");         pw.println("<TD WIDTH=20> = </TD>");         pw.println("<TD ALIGN=RIGHT><TT>"+                        atm.getDensity()+"</TT></TD>");         pw.println("<TD WIDTH=10> </TD>");         pw.println("<TD><TT>"+label[5]+"</TT></TD>");       pw.println("</TR>");       pw.println("</TABLE>");     }  /*  End of "if ( !firstTime )" block  */     //  The HTML form is complete     pw.println("</BODY></HTML>");     pw.close();   } } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net