Java Server Pages - The scriptlets

December 2016



A JSP scriptlet is a block of Java code contained between the following tags:
<% /* scriptlet */ %>


The Java code (which must of course be valid) present between the <% and %> becomes the body of the _jspService() method when generating the servlet (at least if no directive is indicated).

The scriptlet implicitly has access to many objects. The most widely used objects in scriptlets are:
-The request object: to have the details of the HTTP request (e.g when using forms)
-The out object out to send data to the client browser. For example:

<% String[] phrases = {"John", "Peter", "Thomas", "What are you doing?"};
for (int i=0; i<phrases.length; i++) {
	out.println(phrases[i]);
}
%>

The main objects of JSP scriptlets


Here is the list of implicitly accessible objects from any scriptlet:

Here is the list of implicitly accessible objects from any scriptlet:


ObjetDérivé deDescription
requestjavax.servlet.ServletRequestObjet containing client request
responsejavax.servlet.ServletResponse Object containing the response from the JSP page. This object is used with servlets and is generally not used with JSP, since the HTML is created directly
pageContextjavax.servlet.jsp.PageContextObject that contains information about the server environment
sessionjavax.servlet.http.HttpSessionObject containing the current session
applicationjavax.servlet.ServletContextObject containing the servlet context
outjavax.servlet.jsp.JspWriterObject containing the output stream
configjavax.servlet.ServletConfigObject containing the configuration of the servlet
pagejava.lang.ObjectObject designating the page itself
exceptionjava.lang.ThrowableObject representing an uncaught exception





Original document published on CommentcaMarche.net

Related :

This document entitled « Java Server Pages - The scriptlets » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.