Está en la página 1de 22

JSP Design/Architecture

contents

1 JSP Model 1
2 Page-View
3 Page-View with bean
4 JSP Model 2- Front Controller Pattern
5 How to make all the request go to one servlet ?
6 Identifying the Operation to Perform
7 Advantage of Front Controller Pattern
Know

• The JSP Model 1 & Model 2 architectures


Be able to

• Implement JSPs using the Model 1 and Model 2


approaches
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 and
generate response themselves.
• Two variants:
• Page-View
• Page-View with bean
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.
Page-View

JSP Business Processing

All the business processing is done in the jsp


Page-View with bean
JSP Worker Bean Business Processing

• Bean functionality can be modified without


affecting jsp code.
• Some JSP code will sometimes have to
manipulate sessions ,application-wide resource
or state before response can be generated. This
will lead to more of java code embedded in the
JSP code.
• Also the flow of the application logic is
embedded in the JSP.
JSP Model 2- Front Controller Pattern

Underlying idea of MVC architecture


Single control point for presentation tier

response View
Model
request

Controller
request
Model

response View

Model 2 Architecture/ Front Controller Pattern


• Having one servlet as a single point of entry into
whole application
• This entry point produces no output by itself but
• processes the request
• optionally manipulates session and
application state
• redirects requests to appropriate JSP view or
a sub-controller that knows how to handle a
subset of requests to the application.
• Models are provided by java beans created or
managed by the controller and made available
to JSP views
• Preferred approach.
How to make all the requests go to one
servlet ?
• Make url pattern for the servlet as:
• <url-pattern>/*</url-pattern>
• Problem with this is that when the servlet tries to
forward to another url then the request again goes
back to the same servlet
• Make url pattern for the servlet as:
• <url-pattern>*.xxx</url-pattern>
provided we don’t have any such pages in our
application with that extension.
• For instance we could have pattern as
• <url-pattern>*.html</url-pattern>
• In that case, we make all the view pages
as jsp pages so that servlet can forward to
jsp pages.
• Implies that all the url links that we
make in our HTML pages will link to
another html page even though we
don’t have even a single html page in
our application.
Identifying the Operation to Perform

• Indicate the operation in a hidden form


field, which a POST operation delivers to
the controller; for example:
<FORM METHOD="POST" ACTION="myServlet">
<INPUT TYPE="HIDDEN" NAME="OP"
VALUE="createUser"/> … </FORM>
• Indicate the operation in a HTTP GET
query string parameter; for example:

http://myHost/myApp/servlets/myServlet?op=
createUser
• Through requested url paths : Servlet can
extract the requested operation's name from
the request URL. Servlet myServlet can
extract the requested operation's name from
the 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 on
patterns in the URL
RequestHandler
returns String handleRequest(HttpServletRequest req,
URL HttpServletResponse res) throws ServletException,
IOException
requests handleRequest()
Servlet
url Sub-Controller Class 1 Sub-Controller Class 2

JSP Page 1 data


Model Class 1 Model Class 2
JSP Page 2
Example

RequestHandler
returns String handleRequest(HttpServletRequest req,
URL HttpServletResponse res) throws ServletException,
IOException

request handleRequest()
Servlet_
C url Emp_C Dept_C

EmpInsert data
Employee Department
DeptInsert
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);}
Model classes
Employee Department
-empno -deptName
-empName -managerid
-dept <Setters>
<Setters> <Getters>
<Getters> boolean insert()
boolean insert() static Vector findAll()
static Employee static Department
findByName(String name) findDept(String dept)
static Employee
findById(String id)
static Vector findAll()
view classes
Index InsertEmploye FindEmploy
e ee
InsertDepartme FindDepartme
nt nt
Controller classes
ControllerServlet

doGet()

EmpHandler DeptHandler

Vector findAllDepartments() Department findDept(String


Employee name )
findEmployees(String boolean insertDept(String
name,String empno) deptname,String managerid)
boolean boolean validateDept()
insertEmployee(String Vector findAllEmployees()
name,String empno, String
dept)
boolean validateEmployee()
Pattern
• A Model 1 architecture is best
• when the page navigation is simple and fixed, and
when a simple directory structure can represent
the 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 maintain
because the page flow logic is distributed across
multiple pages. This causes maintenance
problem. All the flow is handled by a single servlet.
Pattern

• Servlet code and the controller code is decoupled


from sub-controller so any change in one does not
effect another.
• The same set of classes (sub-controller and entity
classes) and can be used for a different types of
client.

También podría gustarte