What is java server page?
JavaServer Pages JSP is a server side Java technology that allows software developers to create dynamically generated web pages, with HTML, XML, or other document types, in response to a Web client request to a Java Web Application container (server). To allow this an HTML page is given the file extension jsp and an XML markup page is given the file extension jspx for the Java server (container) to recognise the file requires JSP processing before sending it to the client. JSP pages are loaded in the server and operated from a structured special installed Java server packet called a J2EE Web Application often packaged as a .war or .ear file archive.
The technology allows Java code and certain pre-defined actions to be embedded into static page content and compiled on the server at runtime of each page request. Both the Java Server (J2EE specification) and the page scripts and/or extended customised programming added operate by(in the runtime context of being loaded programs used) a special pre-installed base program called a Virtual Machine that integrates with the host Operating System, this type being the Java Virtual Machine(JVM).
Because either, both a Compiler-JVM set(called an SDK or JDK) or the lone JVM(called a (JRE) Java Runtime Environment) is made for most computer platform OSs and the compiled programs for the JVM are compiled into special Java Byte code files for the JVM the Byte-code files(compiled Java program .class files) can be effectively transferred between platforms with no requirement to be recompiled excepting versioning compatibility, or special circumstance. The source code for these J2EE servlet or J2EE JSP programs is almost always supplied with J2EE JSP material and J2EE Web Applications because the server must call the compiler when loading them . These small extension programs (custom tags,servlets,beans,page scripting) are variable and likely to be updated or changed either shortly before runtime or intermittently but particularly when sending JSP page requests themselves, it requires the JSP server to have access to a Java compiler(SDK or JDK) and the required source code (not simply the JVM JRE and byte code class files) to successfully exploit the method of serving.
JSP syntax has two basic forms, scriptlet and markup though fundamentally the page is either HTML or XML markup. Scriptlet tagging(called Scriptlet Elements) (delimited) blocks of code with the markup are not effectively markup and allows any java server relevant API(e.g. the servers running binaries themselves or datatbase connections API or java mail API) or more specialist JSP API language code to be embedded in an HTML or XML page provided the correct declarations in the JSP file and file extension of the page are used. Scriptlet blocks do not require to be completed in the block itself only the last line of the block itself being completed syntactically correctly as a statement is required, it can be completed in a later block . This system of split inline coding sections is called step over scripting because it can wrap around the static markup by stepping over it . At runtime(during a client request) the code is compiled and evaluated, but compilation of the code generally only occurs when a change to the code of the file occurs. The JSP syntax adds additional XML-like tags, called JSP actions, to be used to invoke built-in functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as extensions to the standard HTML or XML tags.
JSPs are compiled into servlets by a JSP compiler. The compiler either generates a servlet in Java code that is then compiled by the Java compiler, or it may compile the servlet to byte code which is directly executable. JSPs can also be interpreted on-the-fly, reducing the time taken to reload changes.
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
<%@ page errorPage="myerror.jsp" %>
<%@ page import="com.foo.bar" %>
<%! int serverInstanceVariable = 1;%>
<% int localStackBasedVariable = 1; %>
<%= toStringOrBlank( "expanded inline data " + 1 ) %>//it will be in html body tag |
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 ):
<%@ include file="somefile.jspf" %>
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).
<%@ page import="java.util.*" %>
<%@ page contentType="text/html" %>
<%@ page isErrorPage="false" %>
<%@ page isThreadSafe="true" %>
<%@ page session="true" %>
<%@ page autoFlush="true" %>
<%@ page buffer="20kb" %>
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.
<%@ taglib prefix="myprefix" uri="taglib/mytag.tld" %>
Scripting Elements
There are three basic kinds of scripting elements that allow java code to be inserted directly into the servlet.
•A declaration tag places a variable definition inside the body of the java servlet class. Static data members may be defined as well. Also inner classes should be defined here.
<%! int serverInstanceVariable = 1; %>
Declaration tags also allow methods to be defined.
<%! /** * Converts the Object into a string or if * the Object is null, it returns the empty string. */ public String toStringOrBlank( Object obj ){ if(obj != null){ return obj.toString(); } return ""; } %>
•A scriptlet tag places all of the statements contained within it, inside the _jspService() method of the java servlet class.
<% int localStackBasedVariable = 1; out.println(localStackBasedVariable); %>
•An expression tag places an expression to be evaluated inside the java servlet class. Expressions should not be terminated with a semi-colon .
<%= "expanded inline data " + 1 %>
Examples of tags jsp:include
jsp:forward
In this forwarding example, the request is forwarded to subpage.jsp The request handling does not return to this page.
jsp:plugin
JSP Standard Tag Library (JSTL)
The JavaServer Pages Standard Tag Library (JSTL) is a component of the Java EE Web application development platform. It extends the JSP specification by adding a tag library of JSP tags for common tasks, such as XML data processing, conditional execution, loops and internationalization.
JSP Tags
Answer: In addition to the pre-defined JSP actions, developers may add their own custom actions using the JSP Tag Extension API. Developers write a Java class that implements one of the Tag interfaces and provide a tag library XML description file that specifies the tags and the java classes that implement the tags.
Consider the following JSP.
<%@ taglib uri="mytaglib.tld" prefix="myprefix" %> …
The JSP compiler will load the mytaglib.tld XML file and see that the tag 'myaction' is implemented by the java class 'MyActionTag'. The first time the tag is used in the file, it will create an instance of 'MyActionTag'. Then (and each additional time that the tag is used), it will invoke the method doStartTag() when it encounters the starting tag. It looks at the result of the start tag, and determines how to process the body of the tag. The body is the text between the start tag and the end tag. The doStartTag() method may return one of the following:
SKIP_BODY
The body between the tag is not processed.
EVAL_BODY_INCLUDE
Evaluate the body of the tag.
EVAL_BODY_TAG
Evaluate the body of the tag and push the result onto stream (stored in the body content property of the tag).
Note: If tag extends the BodyTagSupport class, the method doAfterBody() will be called when the body has been processed just prior to calling the doEndTag(). This method is used to implement looping constructs.
When it encounters the end tag, it invokes the doEndTag() method. The method may return one of two values:
EVAL_PAGE
This indicates that the rest of the JSP file should be processed.
SKIP_PAGE
This indicates that no further processing should be done. Control leaves the JSP page. This is what is used for the forwarding action.
The myaction tag above would have an implementation class that looked like something below:
public class MyActionTag extends TagSupport { // Releases all instance variables. public void release () { … } public MyActionTag () { … } // Called for the start tag public int doStartTag () { … } // Called at the end tag public int doEndTag (){ … } }
Add Body Tag description.
If you want to iterate the body a few times, then the java class (tag handler) implements IterationTag interface. It returns EVAL_BODY_AGAIN - which means to invoke the body again.
Implicit Objects
Implicit Objects The JSP container exposes a number of implicit objects that can be used by the programmer:
out
The JspWriter used to write the data to the response stream.
page
The servlet itself.
pageContext
A PageContext instance that contains data associated with the whole page. A given HTML page may be passed among multiple JSPs.
request
The HttpServletRequest object that provides HTTP request information.
response
The HttpServletResponse object that can be used to send data back to the client.
config : Provides servlet configuration data.
application : Data shared by all JSPs and servlets in the application.
exception
Exceptions not caught by application code.
Scripting Elements
There are three basic kinds of scripting elements that allow java code to be inserted directly into the servlet.
•A declaration tag places a variable definition inside the body of the java servlet class. Static data members may be defined as well. Also inner classes should be defined here.
<%! int serverInstanceVariable = 1; %>
Declaration tags also allow methods to be defined.
<%! /** * Converts the Object into a string or if * the Object is null, it returns the empty string. */ public String toStringOrBlank( Object obj ){ if(obj != null){ return obj.toString(); } return ""; } %>
•A scriptlet tag places all of the statements contained within it, inside the _jspService() method of the java servlet class.
<% int localStackBasedVariable = 1; out.println(localStackBasedVariable); %>
•An expression tag places an expression to be evaluated inside the java servlet class. Expressions should not be terminated with a semi-colon .
<%= "expanded inline data " + 1 %>
The Request Object
Methods of request Object:
There are numerous methods available for request Object. Some of them are:
• getCookies()
• getHeader(String name)
• getHeaderNames()
• getAttribute(String name)
• getAttributeNames()
• getMethod()
• getParameter(String name)
• getParameterNames()
• getParameterValues(String name)
• getQueryString()
• getRequestURI()
• getServletPath()
• setAttribute(String,Object)
• removeAttribute(String)
The Response Object
The response object denotes the HTTP Response data. The result or the information of a request is denoted with this object. The response object handles the output of the client. This contrasts with the request object . The class or the interface name of the response object is http.HttpServletResponse .
The response object is written: Javax.servlet.http.httpservletresponse .
The response object is generally used by cookies.
The response object is also used with HTTP Headers.
Methods of response Object:
There are numerous methods available for response object. Some of them are:
• setContentType()
• addCookie(Cookie cookie)
• addHeader(String name, String value)
• containsHeader(String name)
• setHeader(String name, String value)
• sendRedirect(String)
• sendError(int status_code)
No comments:
Post a Comment