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):

Controller servlet (Controller):

JSP Pages (View):

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"?>
    <servlet-name> ControllerServlet </servlet-name>
    <servlet-class> controller.ControllerServlet </servlet-class>

    <servlet-name> ControllerServlet </servlet-name>
    <url-pattern> /controller </url-pattern>

No comments:

Post a Comment