Java Programming History Part-11

Java programming topics
Post Reply
User avatar
shchamon
Sergeant
Sergeant
Posts: 20
Joined: Sat Apr 14, 2012 8:48 pm
Location: gobindogonj, bangladesh.

Java Programming History Part-11

Post by shchamon » Sat Apr 28, 2012 7:26 pm

Java Programming/JSP
............................................................................................................................
The Java Server Pages is a technology for merging HTML with Java to produce dynamic web content. To enable this, an HTML page is given the file extension .jsp and an XML markup page is given the file extension .jspx, so that the Java server (container) will recognize that the file requires JSP processing before sending it to the client. The Java is inserted either as embedded script lets or as standard/custom tags, thus producing an HTML page that can be displayed on a web browser. If used correctly, the source file can be read and interpreted by WYSIWYG editing programs or HTML programmers that have little to no experience with Java, while still giving the server side functionality of a servlet. In fact a JSP page becomes a standard Java servlet at compile time. JSP pages embed tags within a HTML or XML document that are evaluated by the compiler. This can be done through the use of scripting tags similar to those used in PHP or ASP.NET, or by importing a JSP Tag Library.

Example
............................................................................................................................
Regardless of whether the JSP compiler generates Java source code for a servlet or emits the byte code directly, it is helpful to understand how the JSP compiler transforms the page into a Java servlet. For example, consider the following input JSP and its resulting generated Java Servlet.
Input JSP

Code: Select all

<%@ page errorPage="myerror.jsp" %>
 <%@ page import="com.foo.bar" %>
 
 <html>
 <head>
 <%! int serverInstanceVariable = 1;%>
 
 <% int localStackBasedVariable = 1; %>
 <table>
 <tr><td><%= toStringOrBlank( "expanded inline data " + 1 ) %></td></tr>
Resulting servlet

Code: Select all

package jsp_servlet;
 import java.util.*;
 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
 
 import com.foo.bar; // Imported as a result of <%@ page import="com.foo.bar" %>
 import …
 
 class _myservlet implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage {
    // Inserted as a
    // result of <%! int serverInstanceVariable = 1;%>
    int serverInstanceVariable = 1;
    …
 
    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
        Object page = this;
        PageContext pageContext = …;            // Get the page context for this request
        javax.servlet.jsp.JspWriter out = pageContext.getOut();
        HttpSession session = request.getSession( true );
        try {
            out.print( "<html>\r\n" );
            out.print( "<head>\r\n" );
            …
            // From <% int localStackBasedVariable = 1; %>
            int localStackBasedVariable = 1;
            …
            out.print( "<table>\r\n" );
            out.print( " <tr><td>" );
            // From <%= toStringOrBlank( "expanded inline data " + 1 ) %>
            out.print( toStringOrBlank( "expanded inline data " + 1 ) );
            out.print( " </td></tr>\r\n" );
            …
        } catch ( Exception _exception ) {
            // Clean up and redirect to error page in <%@ page errorPage="myerror.jsp" %>
        }
    }
 } 
JSP Directives
............................................................................................................................
JSP directives are added at the top of a JSP page. These directives control how the JSP compiler generates the servlet. The following directives are available:

Include
The include directive informs the JSP compiler to include a complete file into the current file. It is as if the contents of the included file were pasted directly into the original file. This functionality is similar to the one provided by the C preprocessor. Included files generally have the extension "jspf" (for JSP Fragment):

Code: Select all

<%@ include file="somefile.jspf" %> 
page
The page directive has several attributes:

import
Results in a Java import statement being inserted into the resulting file.
contentType
Specifies the content that is generated. This should be used if HTML is not used or if the character set is not the default character set.
errorPage
Indicates the address of the page that should be shown if an exception occurs while processing the HTTP request.
isErrorPage
If set to true, it indicates that this is the error page. Default value is false.
isThreadSafe
A boolean indicating whether the resulting servlet is thread safe.
autoFlush
To autoflush the contents. A value of true, the default, indicates that the buffer should be flushed when it is full. A value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is illegal when also using buffer="none".
session
To maintain session. A value of true (the default) indicates that the predefined variable session (of type HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to it. A value of false indicates that no sessions will be used, and attempts to access the variable session will result in errors at the time the JSP page is translated into a servlet.
buffer
To set Buffer Size. The default is 8k and it is advisable that you increase it.
isELIgnored
Defines whether EL (Expression Language) expressions are ignored when the JSP is translated.
language
Defines the scripting language used in scriptlets, expressions and declarations. Right now, the only possible value is "java".
extends
Defines the superclass of the class this JSP will become. You won't use this unless you really know what you're doing - it overrides the class hierarchy provided by the Container.
info
Defines a String that gets put into the translated page, just so that you can get it using the generated servlet's inherited getServletInfo() method.
pageEncoding
Defines the character encoding for the JSP. The default is "ISO-8859-1"(unless the contentType attribute already defines a character encoding, or the page uses XML document syntax).

Code: Select all

<%@ page import="java.util.*" %> <%-- example import --%>
<%@ page contentType="text/html" %> <%-- example contentType --%>
<%@ page isErrorPage="false" %> <%-- example for non error page --%>
<%@ page isThreadSafe="true" %> <%-- example for a thread safe JSP --%>
<%@ page session="true" %> <%-- example for using session binding --%>
<%@ page autoFlush="true" %> <%-- example for setting autoFlush --%>
<%@ page buffer="20kb" %> <%-- example for setting Buffer Size --%> 
Note: Only the "import" page directive can be used multiple times in the same JSP.
Taglib
The taglib directive indicates that a JSP tag library is to be used. The directive requires that a prefix be specified (much like a namespace in C++) and the URI for the tag library description.

Code: Select all

<%@ taglib prefix="myprefix" uri="taglib/mytag.tld" %> 
N:B:-See more next time.
Post Reply

Return to “Java Programming”