Presentation is loading. Please wait.

Presentation is loading. Please wait.

Knowledge Byte In this section, you will learn about:

Similar presentations


Presentation on theme: "Knowledge Byte In this section, you will learn about:"— Presentation transcript:

1 Knowledge Byte In this section, you will learn about:
JSP Container Model Character encoding in JSP JSP and XML integration JavaServer Pages Standard Tag Library (JSTL) Collaborate

2 JSP Container Model JSP container is a Web container that stores and manages various JSP components. The javax.servlet.jsp package contains various classes and interfaces that define the rules and strategies for communication between the JSP container and a JSP page. Collaborate

3 JSP Container Model (Contd.)
The contracts between a JSP Container and a JSP page includes: JSP Page Implementation Class JSP Page Model Buffering Precompilation of a JSP Page Debugging Requirement of a JSP Page Collaborate

4 JSP Container Model (Contd.)
JSP Page Implementation Class: Is a servlet class, which is created by a JSP container by translating a JSP page. Is instantiated at the request time. Handles requests by generating a corresponding response for the request. Defines: API contracts Request and Response parameters Collaborate

5 JSP Container Model (Contd.)
JSP Page Model: Describes how to create a response object from a request object of a given protocol. Defines: Protocols to find JSP page instance Methods to define contract between JSP page author and JSP container The HttpJspPage Interface Collaborate

6 JSP Container Model (Contd.)
JSP Page Model (Contd.) The following figure shows the interaction between the JSP container and a JSP page: Collaborate

7 JSP Container Model (Contd.)
Buffering Is a technique used by the JSP container to buffer the response sent by the server. Works only if the JSP buffer attribute is set to true. Requires you to use the javax.servlet.jsp.JspWriter class in your JSP page. Collaborate

8 JSP Container Model (Contd.)
Precompilation of a JSP Page The JSP container supports simple precompilation protocol and some reserved parameter names to serve request from Web clients. All the request parameter names that start with the prefix, jsp are reserved by the JSP specification. A request made to the JSP page that has a request parameter with name jsp_precompile is a precompile request. The JSP container precompiles the JSP page into the JSP page implementation class when the container finds the jsp_precompile request parameter value set to true. Collaborate

9 JSP Container Model (Contd.)
Debugging Requirement of a JSP Page The JSP container generates source map debugging information for JSP pages and tag files. The JSP compiler generates .class files with a SourceDebugExtension attribute. The generated class maps each line of JSP code with the equivalent generated Java code. Collaborate

10 Character Encoding in JSP
Character encoding maps a character set to rules that defines how different characters needs to be represented. Java-platform supported character-encoding schemes are: US-ASCII ISO UTF-8 UTF-16 Various encoding types in JSP are: Page encoding Response encoding Request encoding Collaborate

11 Character Encoding in JSP (Contd.)
Page encoding: Encodes the JSP page or the tag file itself. Is determined from the following sources: JSP configuration element, page-encoding value whose URL pattern matches the page. The pageEncoding attribute of the JSP page directive. The charset value for the contentType attribute of the JSP page directive. Collaborate

12 Character Encoding in JSP (Contd.)
Response character encoding: Encodes the response generated in text format from a JSP page. Is determined from the following sources: The CHARSET value of the contentType attribute of the page directive. The encoding specified by the pageEncoding attribute. The pageEncoding value of a JSP property group. Collaborate

13 Character Encoding in JSP (Contd.)
Request character encoding (Contd.): Encodes the parameters of incoming requests. Uses the JavaServer Pages Standard Tag Library (JSTL) <fmt:requestEncoding> tag to control the encoding from the JSP page. Collaborate

14 JSP and XML Integration
JSP pages can be written using XML syntax. JSP pages with XML syntax are called JSP documents. JSP documents have .jspx extension. Advantages of using JSP XML syntax are: JSP code can be developed in an XML compatible syntax. XML content can be added into a JSP document using the new XML syntax. A container that follows the JSP 1.2 specification will automatically validate the XML syntax of the JSP document. Collaborate

15 JSP and XML Integration (Contd.)
The following table lists the differences between JSP tags and XML tags: Type JSP Tag JSP XML Tag page directive <% page ... %> <jsp:directive.page ... /> include directive include ... %> <jsp:directive.include ... /> taglib directive taglib ... %> <jsp:root xmlns:prefix="taglibURI"> ... </jsp:root> declaration <%! declaration %> <jsp:declaration> declaration </jsp:declaration expression <%= expression %>  <jsp:expression> expression </jsp:expression scriptlets <% scriptlet_code %> <jsp:scriptlet> scriptlet_code </jsp:scriptlet> Collaborate

16 JSP and XML Integration (Contd.)
JSP XML Root Element The <jsp:root> element specifies the root element of a JSP document. The attributes of <jsp:root> element can be used to specify the JSP specification version and to include a tag library in a JSP document. Following is the syntax of using the <jsp:root> element in a JSP document: <jsp:root xmlns:jsp=”path” xmlns:version=”JSP version”prefix1=” “ > </jsp:root> Collaborate

17 JavaServer Pages Standard Tag Library (JSTL)
Is a collection of methods and standard tags that you use in JSP pages to perform repetitive tasks, instead of using Java scriptlet code. The four groups in which the JSTL is divided are: Core tag library Formatting/Internationalization tag library Database tag library XML tag library Collaborate

18 JavaServer Pages Standard Tag Library (JSTL) (Contd.)
Core Tag Library It is a collection of tags that are used to perform common repetitive tasks in JSP pages. It also consists of tags related to JSP expressions, flow control, looping, input, and output. In addition, it provides tags for URL-based resources whose content can be included or processed within a JSP page. You need to prefix the alphabet, c, when you use core tag library. Collaborate

19 JavaServer Pages Standard Tag Library (JSTL) (Contd.)
Core Tag Library (Contd.) It is divided into four groups depending on the function that the tag performs: Variable support tags Flow control tags URL management tags Miscellaneous tags Collaborate

20 JavaServer Pages Standard Tag Library (JSTL) (Contd.)
Formatting or Internationalization Tag Library It consists of tags that support internationalization of JSP pages. You need to prefix, fmt while using internationalization tag. It is divided into three groups depending on the function that the tag performs: Setting locale tags Messaging tags Number and date formatting tags Collaborate

21 JavaServer Pages Standard Tag Library (JSTL) (Contd.)
SQL Tag Library It consists of tags that provides a mechanism to interact with databases. You prefix, sql before an SQL tag. Various tags within the SQL tag library are: setDataSource tag query tag update tag transaction tag Collaborate

22 JavaServer Pages Standard Tag Library (JSTL) (Contd.)
XML Tag Library It consists of tags that can access XML elements to process or parse an XML document. You prefix the symbol, x with the XML tags. It is divided into three groups depending on the function, the tag performs: Core tags Flow Control tags Transformation tags Collaborate

23 From the Expert’s Desk This section, will introduce the following:
Best Practices on Developing JSP Application FAQs on JSP Collaborate

24 Best Practices Developing JSP Applications
Various issues, such as application performance, scalability, security, and speed, needs to be considered while developing JSP applications. Some best practices that you can implement to enhance JSP applications are: Proper usage of JSP objects scope Minimize Java code in JSP pages Proper use of include mechanism Use standard libraries for custom tags Collaborate

25 Best Practices (Contd.)
Developing JSP Applications (Contd.) Proper usage of JSP objects scope Use page scope for objects, which need to be available to only a specific page. You do not need to explicitly remove the objects declared with page scope when it is not required. Use request scope for objects, which need to be shared between JSP pages while processing a request. Object with request scope are automatically discarded once the processing of the request ends. Use session scope for objects, which need to be shared between the JSP pages involved in maintaining client session. You need to explicitly remove the object with session scope when it is not required. Collaborate

26 Best Practices (Contd.)
Developing JSP Applications (Contd.) Minimize Java code in JSP pages JSP page that embeds a considerable amount of Java code is difficult to design and manage. Any compilation error present in the Java code of the JSP page cannot be determined before it is deployed and executed. You can use custom tag and JavaBean to minimize the Java code in the JSP page. Collaborate

27 Best Practices (Contd.)
Developing JSP Applications (Contd.) Proper use of include mechanism Use include directive to include the content of a static resource during translation phase. Use include action dynamically to include the response of a resource during the execution phase. Use include directive to include static contents, such as copyright information and contact addresses. Use include action to include dynamic contents that might change during runtime or if the calling page dynamically decides what content to include. Collaborate

28 Best Practices (Contd.)
Developing JSP Applications (Contd.) Use standard libraries for custom tags Use the standard tag libraries to perform repetitive task in a JSP page. JSTL provides a set of standard tags to perform common tasks, such as database access and formatting an XML document Collaborate

29 FAQs How can you edit the web.xml deployment descriptor file of a JSP application to specify a new custom tag mapping for your JSP application? When you deploy a JSP application in the J2EE application server a directory with the name of your application is created. This directory contains a WAR directory that contains your application file in the J2EE Web application directory structure. The web.xml deployment descriptor file resides in the WEB-INF directory. If your application name is MyApp and your J2EE application server is located in the C drive, you can locate the web.xml file in: C:\Sun\AppServer\domains\domain1\applications\j2ee-apps\MyApp\war-ic_war\WEB-INF Open the web.xml file in a text editor and modify the taglib element to map to the TLD file of the new tag library. Collaborate

30 FAQs (Contd.) Can you use Filters in JSP application?
Yes, you can use filters to intercept request and responses transmitting between the client and your JSP application. You can use filters, to perform tasks, such as providing information of the client to the JSP application and compressing the responses sent by your JSP application. Collaborate

31 Challenge How can you specify to the container that it needs to queue multiple requests to the JSP page and to send only one request at a time? How can you specify the buffer size of the out object? The exception JSP implicit object is an instance of the ___________class. Specify the implicit object and the corresponding method used to log error messages in a JSP page. The _______________ constant can be used in a tag handler to specify that the container needs to skip evaluating the remaining content of the JSP page. Collaborate

32 Solution 1. Set the value of isThreadSafe attribute to false.
2. Set the value of the page directive attribute, buffer, to a specified buffer size. 3. java.lang.Throwable. 4. application implicit object (ServletContext) and method is log(java.lang.String) and log(java.lang.Throwable, java.lang.String). 5. SKIP_PAGE. Collaborate


Download ppt "Knowledge Byte In this section, you will learn about:"

Similar presentations


Ads by Google