Advanced Java Programming - Unit -Vi Iii – Ii It

Advanced Java Programming - Unit -Vi Iii – Ii It

ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT Java Server Pages (JSP) Java Server Pages (JSP) is a Java API in J2EE which allows dynamic creation of web pages using Java. It has the same purpose as other existing technologies like CGI or PHP. A JSP page is a text-based document that contains two types of text: Static template data: which can be expressed in any text-based format, such as HTML,SVG, WML, and XML and will be used for presentation. JSP elements: which construct dynamic content based on the logic. JSP provides clear separation between logic and presentation. JSP simply puts Java code inside HTML pages and saving the file with .jsp extension. Disadvantages with Servlets: When we are developing servlets, we need to have many things in to consideration like overriding service methods and entries in web.xml. From the developer’s perspective, Servlets are pure java programs with class and method definitions whereas a JSP page is much like a text document or web page. With servlets, developer must write java code to output the entire markup, on the other hand a JSP page can be designed like a static web page. Servlets are well suited for handling client request and executing application logic whereas JSP pages are well suited as views and application logics also. It's hard to take advantage of web-page development tools when designing the application interface. If such tools are used to develop the web page layout, the generated HTML must embedded into the servlet code manually, which is time consuming, error prone, and extremely boring. Benefits of JSP: JSP allows the Separation of request processing, business logic, and presentation. Auto transition and compilation. Means we need not recompile every time when we do changes in JSP. There is no deployment descriptor for JSP. Multiple deployment is avoided. Means on the fly we can do changes to JSP Pages. JSP are translated and compiled into JAVA servlets but are easier to develop than JAVA servlets. JSP uses simplified scripting language based syntax for embedding HTML into JSP. JSP containers provide easy way for accessing standard objects and actions. JSP use HTTP as default request /response communication paradigm and thus make JSP ideal as Web Enabling Technology. 1 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT JSP Application Processing A JSP page cannot be sent as-is to the browser; all JSP elements must first be processed by the server. This is done by turning the JSP page into a servlet, and then executing the servlet. A JSP container is responsible for converting the JSP page into a servlet. The lifecycle of a JSP is as follows 2 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT Life cycle of a JSP page consists of three phases. Those are Translation phase Compilation phase Execution phase Translation Phase Here the web container will translates the .jsp page into a servlet compatible java file. Here the entire static markup in the JSP page is converted into the code that writes the data to response stream. During the translation phase JSP elements are treated as follows: JSP directives control the behavior of the resultant servlet. Scripting elements results into the equivalent Java code. Custom tags are converted into the code that calls method on tag handlers Compilation Phase Here servlet compatible java file will be compiled into a class file. Execution Phase Executes the servlet (response is sent to the client). JSP life cycle's execution phase is almost similar to that of the Servlet life cycle, because ultimately it's a servlet which is being executed. The Servlet class generated at the end of the translation phase represents the contract between container and the JSP page. JSP life cycle includes three methods jspInit(), _jspService() and jspDestroy() jspInit(): This method is invoked when the JSP page is initialized. This method is similar to init() method in servlet. If we want to provide initialization for a JSP page, we define this method in declaration part of the JSP page. _jspService( ): This method represents the body of the JSP page and invoked at each client request. This method is similar to service() method in servlet. jspDestroy(): This method is called only once in JSP's servlet life time, when the JSP Engine removes the JSP's servlet from memory. It is always better to remove instance variable resources such as JDBC connections, sockets. 3 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT Anatomy/Structure of a JSP Page A JSP page is a mixture of standard HTML tags, web page content, and some dynamic content that is specified using JSP Elements and implicit objects. Everything except the JSP constructs is called Template Text which can be text or HTML. Template text can be any text: HTML, WML, XML, or even plain text. JSP Elements (Tags) There are six types of JSP elements that we can use and they are: 1. Declaration Element 2. Scriptlets 3. Expression Element 4. Comments 5. Directive Elements 6. Action Elements Scriptlet: A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Following is the syntax of Scriptlet: <% code fragment %> Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP: 4 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT <html> <head><title>Hello World</title></head> <body> Hello World!<br/> <% out.println("Welcome to JSP”); %> </body> </html> JSP Declarations: A declaration declares one or more variables or methods that we can use in Java code later in the JSP file. We must declare the variable or method before we use it in the JSP file. Following is the syntax of JSP Declarations: <%! declaration; [ declaration; ]+ ... %> Following is the simple example for JSP Declarations: <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> <%! public void jspInit() { }% JSP Expression: A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, we can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but we cannot use a semicolon to end an expression. Following is the 5 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT syntax of JSP Expression: <%= expression %> Example: <body> Todays Date: <% = new java.util.Date() %> </body> Which would generate the following output Todays Date: 07-July-2011 14:45:55 JSP Directives: A JSP directive affects the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %> There are three types of directive tag: Directive Description Defines page-dependent attributes, such as scripting language, error <%@ page ... %> page, and buffering requirements <%@ include ... %> Includes a file during the translation phase. <%@ taglib ... %> Declares a tag library, containing custom actions, used in the page Page Directive: The page directive is used to provide instructions to the container that pertain to the current JSP page. We may code page directives anywhere in our JSP page. By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive: <%@ page attribute="value" Include Directive: The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page. The general usage form of this directive is as follows: 6 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT <%@ include file="relative url" > Example: A good example of include directive is including a common header and footer with multiple pages of content. Let us define following three files (a) header.jps (b)footer.jsp and (c)main.jsp as follows: Following is the content of header.jsp: <%! int pageCount = ; void addCount() { pageCount++; } %> <% addCount(); %> <html> <head> <title>The include Directive Example</title> </head> <body> <center> <h2>The include Directive Example</h2> <p>This site has been visited <%= pageCount %> times.</p> </center> <br/><br/> Following is the content of footer.jsp: <br/><br/> <center> <p>Copyright © 2010</p> </center> </body> </html> Finally here is the content of main.jsp: 7 DEPARTMENT OF INFORMATION TECHNOLOGY ADVANCED JAVA PROGRAMMING - UNIT -VI III – II IT <%@ include file="header.jsp" %> <center> <p>Thanks for visiting my page.</p> </center> <%@ include file="footer.jsp" %> Now let us keep all these files in root directory and try to access main.jsp. This would display follow result: This site has been visited 1 times. Thanks for visiting my page. Copyright © 2010 Taglib Directive: The JavaServer Pages API allows us to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior. The taglib directive declares that our JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in our JSP page. The taglib directive follows the following syntax: <%@ taglib uri="uri" prefix="prefixOfTag" > Where the uri attribute value resolves to a location the container understands and the prefix attribute informs a container what bits of markup are custom actions.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    27 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us