3 jsp design&architecture

Upload: suresh1130

Post on 31-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 3 JSP Design&Architecture

    1/22

    JSP Design/Architecture

  • 8/14/2019 3 JSP Design&Architecture

    2/22

    Advantage of Front Controller Pattern7Identifying the Operation to Perform6

    How to make all the request go to one servlet ?5

    JSP Model 2- Front Controller Pattern4

    Page-View with bean3

    Page-View2

    JSP Model 11

    contents

  • 8/14/2019 3 JSP Design&Architecture

    3/22

    Know

    The JSP Model 1 & Model 2 architectures

  • 8/14/2019 3 JSP Design&Architecture

    4/22

    Be able to

    Implement JSPs using the Model 1 and Model 2approaches

  • 8/14/2019 3 JSP Design&Architecture

    5/22

    JSP Model 1 Requests are made directly to the JSP

    page or servlets that produce the

    response.

    JSP or servlet access the enterprise

    resources through a java bean andgenerate response themselves.

    Two variants:

    Page-View

    Page-View with bean

  • 8/14/2019 3 JSP Design&Architecture

    6/22

    JSP Model 1 Advantage:

    simple to program and allows page author to

    generate content easily, based upon the

    request and the state of the resources.

    Disadvantage: the page complexity increases with the

    increase of application complexity excessive

    java code embedded within JSP page.

  • 8/14/2019 3 JSP Design&Architecture

    7/22

    Page-View

    JSP Business Processing

    All the business processing is done in the jsp

  • 8/14/2019 3 JSP Design&Architecture

    8/22

    Page-View with bean

    Bean functionality can be modified without

    affecting jsp code. Some JSP code will sometimes have tomanipulate sessions ,application-wide resourceor state before response can be generated. This

    will lead to more of java code embedded in theJSP code.

    Also the flow of the application logic isembedded in the JSP.

    JSP Business ProcessingWorker Bean

  • 8/14/2019 3 JSP Design&Architecture

    9/22

    JSP Model 2- Front Controller Pattern

    Underlying idea of MVC architecture

  • 8/14/2019 3 JSP Design&Architecture

    10/22

    Controller

    View

    View

    request

    request

    response

    response

    Model

    Model

    Model 2 Architecture/ Front Controller Pattern

    Single control point for presentation tier

  • 8/14/2019 3 JSP Design&Architecture

    11/22

    Having one servlet as a single point of entry intowhole application

    This entry point produces no output by itself but processes the request

    optionally manipulates session andapplication state

    redirects requests to appropriate JSP view ora sub-controller that knows how to handle asubset of requests to the application.

    Models are provided by java beans created ormanaged by the controller and made availableto JSP views

    Preferred approach.

  • 8/14/2019 3 JSP Design&Architecture

    12/22

    How to make all the requests go to one

    servlet ?

    Make url pattern for the servlet as:/*

    Problem with this is that when the servlet tries toforward to another url then the request again goes

    back to the same servlet Make url pattern for the servlet as:

    *.xxx

    provided we dont have any such pages in ourapplication with that extension.

    For instance we could have pattern as*.html

  • 8/14/2019 3 JSP Design&Architecture

    13/22

    In that case, we make all the view pages

    as jsp pages so that servlet can forward tojsp pages.

    Implies that all the url links that we

    make in our HTML pages will link toanother html page even though wedont have even a single html page inour application.

  • 8/14/2019 3 JSP Design&Architecture

    14/22

    Identifying the Operation to Perform

    Indicate the operation in a hidden formfield, which a POST operation delivers to

    the controller; for example:

    Indicate the operation in a HTTP GET

    query string parameter; for example:

    http://myHost/myApp/servlets/myServlet?op=

    createUser

  • 8/14/2019 3 JSP Design&Architecture

    15/22

    Through requested url paths : Servlet canextract the requested operation's name from

    the request URL. Servlet myServlet canextract the requested operation's name fromthe request URL.

    String path=request.getServletPath();The BluePrints recommendation is to use

    servlet mappings when they are available.Servlet mappings provide the most flexible

    way to control where to route URLs based onpatterns in the URL

  • 8/14/2019 3 JSP Design&Architecture

    16/22

    Servletrequests

    Sub-Controller Class 1 Sub-Controller Class 2

    RequestHandlerString handleRequest(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

    returnsURL

    Model Class 1 Model Class 2JSP Page 1JSP Page 2

    handleRequest()

    url

    data

  • 8/14/2019 3 JSP Design&Architecture

    17/22

    Servlet_C

    equestEmp_C Dept_C

    RequestHandlerString handleRequest(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

    returnsURL

    Employee DepartmentEmpInsertDeptInsert

    handleRequest()

    url

    data

    Example

  • 8/14/2019 3 JSP Design&Architecture

    18/22

    public class ControllerServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,

    HttpServletResponse response)throws IOException,ServletException {

    String path=request.getServletPath();

    RequestHandler rh;

    if(path.equals(\empIns.html)){

    rh=(RequestHandler) new Emp_C;

    else

    if(path.equals(\deptIns.html)){

    rh=(RequestHandler) new Dept_C;

    String view=rh.handleRequest(request,response);

    request.getRequestDispatcher(view).forward(request,respons

    e);}

  • 8/14/2019 3 JSP Design&Architecture

    19/22

    Employee

    -empno-empName-dept

    boolean insert()static EmployeefindByName(String name)static EmployeefindById(String id)static Vector findAll()

    Model classes

    Index

    view classes

    InsertEmployee

    FindEmployeeFindDepartme

    nt

    InsertDepartme

    nt

    Department

    -deptName-managerid

    boolean insert()static Vector findAll()static DepartmentfindDept(String dept)

  • 8/14/2019 3 JSP Design&Architecture

    20/22

    Controller classes

    EmpHandler

    Vector findAllDepartments()EmployeefindEmployees(Stringname,String empno)booleaninsertEmployee(String

    name,String empno, Stringdept)boolean validateEmployee()

    ControllerServlet

    doGet()

    DeptHandler

    Department findDept(Stringname )boolean insertDept(Stringdeptname,String managerid)boolean validateDept()Vector findAllEmployees()

    P tt

  • 8/14/2019 3 JSP Design&Architecture

    21/22

    A Model 1 architecture is best

    when the page navigation is simple and fixed, andwhen a simple directory structure can representthe structure of the pages in the application.

    Over time, as the application grows and changes,page flow logic accumulates. T

    The application becomes difficult to maintainbecause the page flow logic is distributed acrossmultiple pages. This causes maintenanceproblem. All the flow is handled by a single servlet.

    Pattern

    P tt

  • 8/14/2019 3 JSP Design&Architecture

    22/22

    Servlet code and the controller code is decoupledfrom sub-controller so any change in one does noteffect another.

    The same set of classes (sub-controller and entityclasses) and can be used for a different types ofclient.

    Pattern