D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\servelt\SiteWorxFormServlet.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package servelt;
  6 
  7 import java.io.IOException;
  8 import java.io.PrintWriter;
  9 import javax.servlet.ServletException;
 10 import javax.servlet.annotation.WebServlet;
 11 import javax.servlet.http.HttpServlet;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletResponse;
 14 
 15 /**
 16  *
 17  * @author sameldin
 18  */
 19 import utils.*;
 20 import constants.*;
 21 import dataobjects.*;
 22 import bo.*;
 23 
 24 
 25 @WebServlet(name = "SiteWorxFormServlet", urlPatterns = {"/SiteWorxFormServlet"})
 26 public class SiteWorxFormServlet extends HttpServlet 
 27 {
 28 
 29     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 30     /**
 31      * Handles the HTTP
 32      * <code>GET</code> method.
 33      *
 34      * @param request servlet request
 35      * @param response servlet response
 36      * @throws ServletException if a servlet-specific error occurs
 37      * @throws IOException if an I/O error occurs
 38      */
 39     @Override
 40     protected void doGet(HttpServletRequest request, HttpServletResponse response)
 41             throws ServletException, IOException 
 42     {
 43                 doPost(request, response);
 44     }
 45 
 46     /**
 47      * Handles the HTTP
 48      * <code>POST</code> method.
 49      *
 50      * @param request servlet request
 51      * @param response servlet response
 52      * @throws ServletException if a servlet-specific error occurs
 53      * @throws IOException if an I/O error occurs
 54      */
 55     @Override
 56     protected void doPost(HttpServletRequest request, HttpServletResponse response)
 57             throws ServletException, IOException 
 58     {
 59             response.setContentType("text/html");
 60             PrintWriter out = response.getWriter();
 61             out.println("<h2> SiteWorxFormServlet</h2>");
 62             out.println("<BR><BR>");
 63     //-----------------------------------------------------------------------        
 64             UserOutputDAO localUserOutputDAO = new UserOutputDAO();
 65             
 66             String firstName  = (String)request.getParameter(Constants.USER_FIRST_NAME_STRING);
 67             if(firstName != null)
 68             {
 69                 localUserOutputDAO.setUserFirstName(firstName);;
 70             }
 71             String lastName  = (String)request.getParameter(Constants.USER_LAST_NAME_STRING);
 72             if(lastName != null)
 73             {
 74                 localUserOutputDAO.setUserLastName(lastName);
 75             }
 76             String userID  = (String)request.getParameter(Constants.USER_ID_NUMBER_STRING);
 77             if(userID != null)
 78             {
 79                 try
 80                 {
 81                         long tempLong = Long.parseLong(userID);
 82                         localUserOutputDAO.setUserID(tempLong);
 83                 }
 84                 catch(NumberFormatException e)
 85                 {
 86                         localUserOutputDAO.setUserID(Constants.NO_VALUE);
 87                 }                
 88             }
 89             String userEmail  = (String)request.getParameter(Constants.USER_EMAIL_ADDRESS_STRING);
 90             if(userEmail != null)
 91             {
 92                 localUserOutputDAO.setUserEmailAddress(userEmail);
 93             }                    
 94             String  totalCount    = (String)request.getParameter(Constants.TOTAL_COUNT_STRING);
 95             if(totalCount != null)
 96             {
 97                 try
 98                 {
 99                     int tempCount = Integer.parseInt(totalCount);
100                     localUserOutputDAO.setTotalCount(tempCount);
101                 }
102                 catch(NumberFormatException e)
103                 {
104                     localUserOutputDAO.setTotalCount(Constants.NO_VALUE);
105                 }
106             }
107             String  sumValue    = (String)request.getParameter(Constants.SUM_VALUE_STRING);
108             if(sumValue != null)
109             {
110                 try
111                 {
112                     long tempSum = Long.parseLong(sumValue);
113                     localUserOutputDAO.setSumValue(tempSum);
114                 }
115                 catch(NumberFormatException e)
116                 {
117                     localUserOutputDAO.setSumValue(Constants.NO_VALUE);
118                 }                
119             }
120             String  averageValue    = (String)request.getParameter(Constants.AVERAGE_VALUE_STRING);
121             if(averageValue != null)
122             {
123                 try
124                 {
125                     double tempAverage = Double.parseDouble(averageValue);
126                     localUserOutputDAO.setAverageValue(tempAverage);
127                 }
128                 catch(NumberFormatException e)
129                 {
130                     localUserOutputDAO.setAverageValue(Constants.NO_VALUE);
131                 }                
132             }
133             String  medianValue    = (String)request.getParameter(Constants.MEDIAN_VALUE_STRING);
134             if(medianValue != null)
135             {
136                 try
137                 {
138                     double tempMedian = Double.parseDouble(medianValue);
139                     localUserOutputDAO.setMedianValue(tempMedian);
140                 }
141                 catch(NumberFormatException e)
142                 {
143                     localUserOutputDAO.setMedianValue(Constants.NO_VALUE);
144                 }                
145             }
146             String  highestValue    = (String)request.getParameter(Constants.HIGHTEST_VALUE_STRING);
147             if(highestValue != null)
148             {
149                 try
150                 {
151                     int tempHighest = Integer.parseInt(highestValue);
152                     localUserOutputDAO.setHighestValue(tempHighest);
153                 }
154                 catch(NumberFormatException e)
155                 {
156                     localUserOutputDAO.setHighestValue(Constants.NO_VALUE);
157                 }                
158             }
159             String  lowestValue    = (String)request.getParameter(Constants.LOWEST_VALUE_STRING);
160             if(lowestValue != null)
161             {
162                 try
163                 {
164                     int tempLowest = Integer.parseInt(lowestValue);
165                     localUserOutputDAO.setLowestValue(tempLowest);
166                 }
167                 catch(NumberFormatException e)
168                 {
169                     localUserOutputDAO.setLowestValue(Constants.NO_VALUE);
170                 }                                
171             }
172         StoreUserOutputDataObjectBO localStoreUserOutputDataObjectBO = new StoreUserOutputDataObjectBO(localUserOutputDAO);
173         out.println("<BR> ============================================");
174         out.println("<BR> User Data is stored in our Database");
175         out.println("<BR> Thanks for using our service<BR>");
176         out.println("<BR> ============================================");
177         out.flush();
178         out.close();
179        
180     }
181 
182     /**
183      * Returns a short description of the servlet.
184      *
185      * @return a String containing servlet description
186      */
187     @Override
188     public String getServletInfo() 
189     {
190         return "Short description: SiteWorxFormServlet";
191     }// </editor-fold>
192 }
193