J2EE jsp_01


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

J2EE jsp_01

  1. 1. Java Server Pages Technology (Part-1) Biswabrata Banerjee
  2. 2. What is this JSP anyway !?  JavaServer Pages (JSP) is a java technology that provides a simplified, fast way to create dynamic web content. JSP technology enables rapid development of web-based applications that are server- and platform- independent. It helps software developers serve dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 as Sun's answer to ASP and PHP, JSP Specification was designed to address the perception that the Java programming environment didn't provide developers with enough support for the Web.  JSP , like other Java APIs, is a specification by Sun for vendors to implement. The JSP specification builds on the functionality provided by the servlet specification Biswabrata Banerjee
  3. 3. • A sample JSP page (code ref-1.1):- Today.jsp <%@ page import=“java.util.Date”%> <html> <body> The Current Date and Time is: <%=(new Date()).toString()) %> </body> </html> Biswabrata Banerjee
  4. 4. JSP can dynamically invoke other JSPs JSP <JSP tag> <JSP code> JSP <JSP tag> <JSP code> JSP Client Translated <JSP tag> <JSP code> servlet Biswabrata Banerjee
  5. 5. Biswabrata Banerjee
  6. 6. JSP architecture Biswabrata Banerjee
  7. 7. • No central component • Has to embed Big chunk of Java-code to implement business logic • Designers aren’t comfortable with server-side programmig. • Reusability of application components are is not promoted. Biswabrata Banerjee
  8. 8. JSP architecture 3 2/6 5 4 uses bean Biswabrata Banerjee
  9. 9. MVC Architecture Explained … In MVC model, the request is sent by the browser to the controller or the servlet. This request is instantiated by the servlet as a Java Bean. The main aspect is JSP are compiled into servlets at the back end and the front end tasks are not interrupted. The servlet engine takes up the responsibility of compiling JSP Servlet and producing the final JSP servlet class for usage. The front end presentation modules are handled by JSP for viewing and the manipulation of data is handled by Java Bean(model) and passed back to JSP when needed. The Presentation part of the MVC Model has no processing logic. It performs the task of extracting beans or objects that may have been initially created by the controller. It also extracts the dynamic content within for insertion within its static templates. The Application Manager in the MVC Model is the Controller that processes HTTP requests. They are not responsible for presentation tasks. That can be either servlets or JSP. They take the task of managing the application state, security, and presentation uniformity and thus, have a single point of entry.
  10. 10. JSP Life-cycle Phase Name Description Page Translation A Servlet (java) file is created Page compilation The java file compiled. Load class Compiled class loaded Create Instance An instance of Servlet is created Call jspInit() This method is called before other method to allow Syn : public void jspInit() { //.. } initialization. Call _jspService() This method is called for each request Syn : public void _jspService(HttpServletRequest req, HttpServletResponse res ) throws ServletException,IOException { //..} Call jspDestory() This method is called when Servlet decided to take the Syn : public void jspDestroy() { //.. } Servlet out of service
  11. 11. // not exact code ! Depends on vendor specification. import java.util.Date; public final class Todat_jsp implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage { public void jspInit(){ // initialization parameters } public void _jspService( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException , java.io.IOException { javax.servlet.ServletConfig config = …; // Get the servlet config out.wite( (new Date().toString() ) ; } public void jspDestroy(){ // … } }
  12. 12. JSP Nuts & Bolts page Directives include Comments Expression taglib Scriptlet Declaration Scripting Elements Actions getProperty include plugin useBean setProperty param forward
  13. 13. Attribute Description Default value language For defining scripting language java extends To indicate which clas the generated servlet will Omitted extend Import Reason is Same as import in a java class Omitted session specify whether a page participates in a session True buffer models Buffer for the output stream of the client Implementation dependent;at least 8kb autoFlush To empty the buffer if full True isThreadSafe Defines the level of thread safety true Info Defines an informative string [ servlet.getServletInfo() ] Omitted errorPage To invoke another JSP page on error Omitted isErrorPage If this is an error page of some other JSP page false contentType The character encoding & MIME (Multipurpose MIME type is text/html Internet Mail Extensions) type. CHARSET is ISO-8859-1
  14. 14. An example: <%@ page [ language="java" ] [ extends="package.class" ] [ import="{package.class | package.*}, ..." ] [ session="true | false" ] [ buffer="none | 8kb | sizekb" ] [ isThreadSafe="true | false" ] [ info="text" ] [ errorPage="relativeURL" ] [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] [ isErrorPage="true | false" ] %> <html> < !- - do anything here as you Like --> </html>
  15. 15. The include directive Include Directive Includes a file of text or code when the JSP page is translated i.e., at the time of compilation. JSP Syntax <%@ include file="relativeURL" %> Examples include.jsp: <html> <head><title>An Include Test</title></head> <body bgcolor="white"> <font color="blue"> The current date and time are <%@ include file="date.jsp" %> </font> </body> </html>
  16. 16. The include directive … Cont’d date.jsp: <%@ page import="java.util.*" %> <%= (new java.util.Date() ).toLocaleString() %> Displays in the page: The current date and time are Aug 30, 1999 2:38:40 Description The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. When you use the <%@ include %> directive, the include process is static. A static include means that the text of the included file is added to the JSP file. The included file can be a JSP file, HTML file, or text file. If the included file is a JSP file, its JSP elements are parsed and their results included (along with any other text) in the JSP file.
  17. 17. The taglib directive Taglib Directive defines a tag library and prefix for the custom tags used in the JSP page. JSP Syntax <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> Examples <%@ taglib uri="http://www.jspcentral.com/tags" prefix="public" %> <public:loop> </public:loop> Description The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix.