Friday, 7 September 2012

Example on MVC Model 2 : Search & Create Employees

In MVC model 1 example, all the business logic goes into JavaBeans. But still this approach is insufficient to separate different kinds of logic. Here JSPs handle the processing of incoming request as well as replying back to the client.

In MVC Model 2, controller was introduced. So here, we will create same application using model 2 architecture.

Program Flow
=================================
The following figure helps to understand the program flow of this example:




Here all the requests are submitted to controller which uses javaBeans and forwards the user to another view (JSP).
In case of any exception (JSP/controller), control will be transferred to emperror.jsp to display error message.


Code for this Application
=================================
JavaBeans (Model):
  EmployeeInfo.java
  EmployeeDB.java

Controller servlet (Controller):
  ControllerServlet.java

JSP Pages (View):
  addemployee.jsp
  searchemployee.jsp
  saveemployee.jsp
  showemployee.jsp
  emperror.jsp

Deployment Descriptor (web.xml)
=================================
Also, for accessing a servlet, we have to define URL pattern in web.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app>
   
   <servlet>
    <servlet-name> ControllerServlet </servlet-name>
    <servlet-class> controller.ControllerServlet </servlet-class>
   </servlet>

   <servlet-mapping>
    <servlet-name> ControllerServlet </servlet-name>
    <url-pattern> /controller </url-pattern>
   </servlet-mapping>
  </web-app>








No comments:

Post a Comment