Created
November 20, 2012 23:41
-
-
Save jobscry/4122045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package web; | |
//Business logic component imports | |
import domain.Course; | |
import domain.Student; | |
import domain.RegisterService; | |
//Servlet imports | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.ServletContext; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
//Support classes | |
import util.Status; | |
import java.io.IOException; | |
import java.util.Properties; | |
public class RegistrationServlet extends HttpServlet { | |
public void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws IOException, ServletException { | |
//Dispatch the request | |
processRequest(request, response); | |
} | |
//The method that performs the Control aspects of the Web application | |
public void processRequest(HttpServletRequest request, | |
HttpServletResponse response) throws IOException, ServletException { | |
//Declare the dispatcher for the View | |
RequestDispatcher view=null; | |
//Create the business logic object | |
RegisterService regService=null; | |
//Create the Status object and store it in the request | |
//for use by the 'Registration Form' View (if necessary) | |
Status status=new Status(); | |
request.setAttribute("status", status); | |
//Retrieve the HTML form parameters | |
String semester=request.getParameter("semester"); | |
String number=request.getParameter("number"); | |
String name=request.getParameter("name"); | |
String address=request.getParameter("address"); | |
String city=request.getParameter("city"); | |
String state=request.getParameter("state"); | |
String zipCode=request.getParameter("zipCode"); | |
//Verify form fields data; create Exception objects if data are missing | |
//These "error messages" are presented to the user | |
//in a red bulleted list | |
if (semester.equals("unknown")) | |
status.addException(new Exception( | |
"Please select a course semester. ")); | |
if (number.equals("unknown")) | |
status.addException(new Exception( | |
"Please select a course number. ")); | |
if ((name==null) || (name.length()==0)) | |
status.addException(new Exception("Please enter your name. ")); | |
if ( (address==null) || (address.length()==0) | |
|| (city==null) || (city.length()==0) | |
|| (state==null) || (state.length()==0) | |
|| (zipCode==null) || (zipCode.length()==0) ) | |
status.addException(new Exception( | |
"Please enter your full address. ")); | |
//In case of errors, forward the request back to | |
//'Registration Form' View | |
//and return without proceeding with the rest of | |
//the business logic. | |
if (!status.isSuccessful()) { | |
view=request.getRequestDispatcher("form.jsp"); | |
view.forward(request, response); | |
return; | |
} | |
//If no verification errors are found, the Controller | |
//uses the business services to perform the registration. | |
try { | |
Properties sysprops=System.getProperties(); | |
String fs=sysprops.getProperty("file.separator"); | |
String path=getServletContext().getRealPath("/")+ | |
"WEB-INF/storage"+fs; | |
regService=new RegisterService(); | |
//Retrieve the Course object and verify that it exists | |
Course course=regService.getCourse(semester, number, path); | |
if (course==null) throw new Exception("The course you have "+ | |
" selected does not yet exist; please select another one."); | |
//Create and populate the student object | |
Student student=regService.createStudent( | |
name,address,city,state,zipCode); | |
//Delegate registration to the RegisterService object | |
regService.register(course, student, path); | |
request.setAttribute("course", course); | |
request.setAttribute("student", student); | |
//Select the next View for the user in case | |
//registration is successful | |
//Forward to the thankYou.jsp View | |
view=request.getRequestDispatcher("thankYou.jsp"); | |
view.forward (request, response); | |
} | |
//Select the next View for the user in case of business logic errors. | |
//Forward back to the 'Registration Form' View to report | |
//what errors occurred. | |
catch (Exception e) { | |
status.addException(e); | |
//Select next View. | |
//Exceptions are caught, forward back to the form.jsp View. | |
view=request.getRequestDispatcher("form.jsp"); | |
view.forward(request, response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment