JSP_Day_1

JSP_Day_1 - JSP – Dynamic JSP – Dynamic Content...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: JSP – Dynamic JSP – Dynamic Content Generation Agenda Agenda Introduction JSP – What and Why? My first JSP JSP Fundamentals JSP Directives and APIs JSP and Tags MVC (Model 2) Application Introduction Introduction Architecture Architectures Architectures Developing web pages is easy Designing flexible and extensible web applications that combine HTML, JSP, JavaBeans, databases, etc., is not We discuss two common approaches for building such structures Both use the Model­View­Controller design pattern Model 1 involves JSPs and JavaBeans Model 2 adds Servlets to act as a controller Model­View­Controller Model­View­Controller A Design Pattern Controller ­­ receives user interface input, updates data model Model ­­ represents state of the world (e.g. shopping cart) View ­­ looks at model and generates an appropriate user interface to present the data and allow for further input Model­View­Controller Model­View­Controller JSP View Bean Model Controller Servlet A powerful advantage of JSP is the ability to separate an application's business logic from its presentation. Using Smalltalk object­oriented terminology, JSP encourages MVC (model­view­controller) web applications. JSP classes or beans are the model, JSP is the view, and a servlet is the controller. Model 1 Architecture Model 1 Architecture Model-1 is very simple to understand and itMakes generation of simple pages simple. B But it also messy when a complex page is needed and tends to require lots of Java code in the JSP file (a real no no from a design point of view). Model 1 Architecture Model 1 Architecture Browser submits requests to JSP pages JSPs access business objects indirectly, through JavaBeans Insulates the JSPs from changes in the business objects As long as bean interfaces remain constant, the JSPs are independent of the business object implementations Model 1 Architecture Model 1 Architecture Software developers implement the business objects and beans Web content designers implement the JSP pages Oftentimes, however, the JSP pages decisions to be made regarding destinations Winds up incorporating Java into the page Model 2 Architecture Model 2 Architecture The Model 2 architecture also separates content generation from content presentation Browser requests are submitted to a servlet Servlet accesses business objects to create content Most Java code goes here Content is stored in a JavaBean JSP pages access the beans for presentation Model 2 Architecture Model 2 Architecture Model­2 is very clean, Makes generation of complex pages simpler as it removes most of the Java code from the JSP file and places most of the logic work in Java (where it belongs). But its not as simple as Model­1 and is makes creation of simple JSP files a bit complex. Model 2 Architecture Model 2 Architecture Model 2 is a modified Model­View­ Controller architecture The business objects and beans are the model The JSPs are the view The servlets are the controllers Servlets Servlets Servlets Servlets Request for Servlet 1 Request for Servlet 2 Web Server Servlet 1 Servlet Container Request for Servlet 1 Requests handled by multiple threads inside the servlet container Servlet 2 JSP JSP What is it? Why do we need it? helps effectively separate presentation from content separation of presentation from content by using the JSP Model 2 architecture. This model can also be seen as a server­side implementation of the popular Model­View­ Controller (MVC) design pattern What is JSP? What is JSP? JSP == Java Server Pages A simplified way to generate dynamic web content Simpler then Servlets (Java is not mandatory) Editing tool friendly Can be used to separate the coding of presentation and business logic JSP is based on Java and Servlet technologies An HTML page with imbedded tags and Java Code. At runtime the JSP page is translated into a Java Servlet The runtime is usually encapsulated in a special JSP­ Runtime Servlet. Difference … Difference … JSP is Java code embedded in HTML; the Java code is compiled (if necessary) and run by the container on the server and the client only sees the results of that code's execution mixed in appropriately with the html. Servlets are compiled pure Java class files (not mixed with HTML) that get posts from the client and send html to in return. Both require a container on the server, such as Tomcat, which provides the environment and VM for the Java program. JSP – A standard JSP – A standard JSP is a Java Standard: Defined by a group of companies led by JavaSoft. Current version is 1.2, but previous versions are also in use (mainly 1.1). Ongoing effort to improve JSP. Why JSP? Why JSP? Servlets that generate HTML output hide the HTML inside Java. Makes it hard for an HTML expert (!= Java expert) to fix stuff. Lock the content in Java. Makes look and feel changes to the site problematic. The ability to script with Java and JavaBeans makes generating dynamic content simpler. How it works? How it works? Unlike javaScript which is interpreted in the web browser, JSP pages are translated into java servlets and execute in the servlet engine.. All the work is done on the server and all the browser see is ordinary HTML. Steps.. Steps.. A JSP page is written and stored in the document tree of a Web server, usually as a file with name ending in .jsp The web server receives a request for the document. Since the name ends in .jsp, it forwards the request to the servlet engine The servlet engine passes the request on to the JSP engine, which is typically a servlet itself. The last step… The last step… The JSP engine determines whether the .jsp file is newer than the servlet class that implements if. If this is the case, it parses the .jsp file and creates java source code for the equivalent servlet. It then compiles the servlet and causes the servlet engine to load and execute it. Remember… Remember… When a .jsp file is first requested from a web browser, the JSP engine compares its timestamp with the timestamp on the generated servlet class. If the class is older(or non existent), the JSP engine creates the java source code for the servlet and compiles it. The generated class is then loaded as any other servlet would be. The JSP Syntax The JSP Syntax Inline Java code delimited by <% and %>. Also printing of expressions as text by using <%= %>. Special tags to declare class wide variables and methods. Special tags to use with JavaBeans. Special tags to expose JSP services. JSP directives to specify. Interfaces implemented by the Servlet, classes it extends, packages to import etc. JSP Directives and Scripting JSP Directives and Scripting Elements Directives Declarations Expressions Code Fragment/Scriptlet Comments <%@ directive %> <%! declaration %> <%= expression %> <% code fragment %> <%­­ comment ­­%> My first JSP My first JSP Hello World Helloworld.jsp – The code Helloworld.jsp – The code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 <!DOCTYPE Transitional//EN"> Transitional//EN"> <HTML> <HEAD> <%@ page language="java" <%@ contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII" %> pageEncoding="US-ASCII" <META name="GENERATOR" content="IBM WebSphere Studio"> <TITLE>helloworld.jsp</TITLE> </HEAD> <BODY> <P> <% out.print("This is a sample JSP file"); %> </P> <P> </P> </BODY> </HTML> Helloworld.jsp – Output Helloworld.jsp – Output Helloworld.jsp – Observations Helloworld.jsp – Observations At first, the JSP file looks like a regular HTML page There is a new <%@ page … %> “Tag” that has nothing to do with HTML HTML tags, doctype, etc. This is a JSP directive We have some Java code enclosed between <% and %> This is a JSP scriptlet Examples Examples JSPscriptlet.jsp http://localhost:8080/examples/jsp/JSPscriptlet.jsp? JSPExpressions.jsp JSP Fundamentals JSP Fundamentals JSP Fundementals JSP Fundementals Adding Java to a plain HTML page. Scriptlets Use JSP implicit variables. To obtain information. To write data back to the user. Defining class wide variables and methods. For initialization. Commenting your scripts. JSP Directives and APIs JSP Directives and APIs JSP 1.0 provides directives and APIs to allow enhanced control over the page. Directives provides: Declarative page control (caching for example). Defining Java related page attributes. The APIs provides: Programmatic page control. Access to information that was supplied by using directives Servlet Instantiation Servlet Instantiation The container instantiates a new Servlet in two occasions: A user request service from a Servlet that was not instantiated yet. The Servlet is in the list of startup Servlets and the container is starting. A Servlet is instantiated in the following manner: The container loads the Servlet’s class. Using Class.newInstance() the container instantiates a new instance of the Servlet. MVC (Model 2) MVC (Model 2) Application General Web Application Design General Web Application Design Rules Separate presentation and business logic. JSP is only about presenting data. The JSP file does not need to know where the data came from. Separate the work done by the HTML and Java coders to eliminate contention. These are two different developers. The tools used by the HTML/Java coders are not that good when it comes to handle Java/HTML code. Reminders… Reminders… Data access and business logic is done in Java. JSP get to see the results of this business logic via JavaBeans/special tags. Servlets and JSP can interact in two access models known as Model­1 and Model­2 ...
View Full Document

Ask a homework question - tutors are online