The JSP - Java Server Pages
- JSP extension of the servlet technology which offers both scripting ( as in HTML) as well as programming (as in Servlets)
- When a JSP page is requested, the JSP container ( also called page compiler) compiles the jsp page into a servlet class.
- The JSP API that handles all the JSP technology has been bundled under the following two packages:
- javax.servlet.jsp
- javax.servlet.jsp.tagext
JSP Syntax
A JSP page is all about JSP tags and some template data ( such as HTML tags). That's pretty much of it. It always feels good learning about JSP as we get to see the very well-know HTML tags (I presumed that you already know HTML at this point).JSP consists of three types of elements.
- Directive elements
- Scripting elements
- Action elements
DIRECTIVES
<%@ directiveName (attributeName="atrributeValue")
%>
|
- Page directives
- Include directives
- Tag library directives
<%@ page (attributeName="atrributeValue") %>
<%@ page import="java.util.Date, java.util.Enumeration" language="java" %>
where,
page => represents directiveName
import/language => represents attributeName
and the values enclosed by quotes represents atrributeValue
The following is the list of available Page directive attributes:
Attribute
|
Value
Type
|
Default
Value
|
language
|
Scripting
language name
|
"java"
|
info
|
String
|
Depends on the
JSP container
|
contentType
|
MIME type,
character set
|
"text/html;charset=ISO-8859-1"
|
extends
|
Class name
|
None
|
import
|
Fully qualified
class name or package name
|
None
|
buffer
|
Buffer size or
false
|
8Kb
|
autoFlush
|
Boolean
|
"true"
|
session
|
Boolean
|
"true"
|
isThreadSafe
|
Boolean
|
"true"
|
errorPage
|
URL
|
None
|
isErrorPage
|
Boolean
|
"false"
|
NOTE:
|
|
- language
- <%@ page language="java" %>
- The only supported language by Tomcat JSP Container
- Other JSP containers might accept different languages
- info
- <%@ page info="any string here" %>
- Can be retrieved by calling the method getServletInfo() from anywhere withing the page
- contentType
- <%@ page contentType="application/json; charset=UTF-8" %>
- Defines MIME type of the HTTP response sent to the client
- extends
- <%@ page extends="yourClass" %>
- Defines the parent class that will be inherited by the generated servlet of the current page
- This attribute is less common
- import
- <%@ page import="java.io.*" %>
- One of the most common attributes used
- Same as we do in java classes/interfaces
- By default, tomcat imports the following packages:
- javax.servlet.*
- javax.servlet.http.*
- javax.servlet.jsp.*
- javax.servlet.jsp.tagext.*
- org.apache.jasper.runtime.*
- buffer
- <%@ page buffer="none" %>
- <%@ page buffer="20kb" %>
- <%@ page buffer="20" %>
- buffer="20" is the same as "20kb"
- But it's up to the JSP container to use its discretion to increase the buffer in order for the improved performance
- autoFlush
- <%@ page autoFlush="false" %>
- If true, the JSP container automatically flushes the buffer when it's full.
- If false, the buffer can be flushed manually by the following statement:
- out.flush()
- session
- <%@ page session="true" %>
- By default, the session is set "true", meaning the JSP page takes part in session management
- It is wise and strongly advised not to set it true unless you specifically require it because this consumes resources.
- isThreadSafe
- <%@ page isThreadSafe="true" %>
- True indicates that the simultaneous access to this page is safe
- False indicates that the simultaneous access to this page is not safe. In that case, the JSP container puts multiple requests in a queue and serves them only one at a time.
- errorPage
- <%@ page errorPage="MyErrorPage.jsp" %>
- Indicates the error page which is displayed if an uncaught exception occurs in the current page
- The error page must have the page attribute isErrorPage="true"
- isErrorPage
- <%@ page isErrorPage="true" %>
- If true, then only be set as an error page in other JSP pages
- If true, this page has an access to the exception implicit object
<%@ include file="includes/Footer.html" %>
- As the name suggest, Include Directive enables us to include the contents of other files in the current JSP page.
- The included page can be static HTML page as well as dynamic JSP page.
- Very commonly used to include Header page, Footer page or a page containing import statements and so on.
- The taglib, or tag library, directive can be used to extend JSP functionality. It's a broad topic so it's not covered here at this moment. It will be covered under it's own topic later soon.
SCRIPTING ELEMENTS
-
Scriptlets
-
Declarations
-
Expressions
- Syntax looks similar to Directives. But don't get confused, Directives starts with <%@ i.e. <%@ ... %>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("JDBC driver
loaded");
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
%>
...
...
<%
out.println("You can
do anything here");
String str = "some
string";
str = str + " string
concatenation";
out.println( str );
%>
|
2. Declarations
<%! ... %>
- Used to declare methods and variables that can be used from any point in the JSP page.
- Can appear anywhere throughout the page. For instance, a method declaration can appear above a page directive that imports a class, even though the class is used in the method.
<%!
String getSystemTime() {
return
Calendar.getInstance().getTime().toString();
}
%>
<%@ page
import="java.util.Calendar" %>
<%
out.println("Current Time: " +
getSystemTime());
%>
|
<%= ... %>
- Expressions get evaluated when the JSP page is requested and their results are converted into a String and fed to the print method of the out implicit object.
- If the result cannot be converted into a String, an error will be raised at translation time. If this is not detected at translation time, at request-processing time, a ClassCastException will be raised.
- Don't need to add a semicolon at the end of an expression
Below is the two equivalent statements:
<!-- Expression -->
<%= java.util.Calendar.getInstance().getTime()
%>
<!-- Equivalent
Scriptlet -->
<%
out.print(java.util.Calendar.getInstance().getTime()); %>
|