According to abbreviationfinder, JSP stands for Java Server Pages. Java technology that allows you to generate dynamic content for the Web, in the form of HTML, XML or other documents.
This technology is a development of the Sun Microsystems company. The JSP 1.2 Specification was the first to be released and the JSP 2.1 Specification is now available.
The JSP’s allow the use of Java code through scripts. Also, it is possible to use some predefined JSP actions using tags. These tags can be enriched by using external Tag Libraries (TagLibs or Tag Libraries) and even customized.
Architecture
JSP can be considered as an alternative, and simplified, way of building servlets. This is why a JSP page can do everything a servlet can do, and vice versa. Each version of the JSP specification is strongly tied to a particular version of the servlet specification.
The general operation of JSP technology is that the Application Server interprets the code contained in the JSP page to build the Java code of the servlet to be generated. This servlet will be the one that generates the document (typically HTML) that will be presented on the user’s browser screen.
JSP -> Application Server (Servlets) -> Client (Browser)
It is possible to enrich the markup language used by JSP. For this we must extend the high-level JSP layer by implementing Tags Libraries. An example of these libraries are those provided by Sun under the name of JSTL or those distributed by Apache together with the Struts Framework.
TagLibs -> JSP -> Application Server (Servlets) -> Client (Browser)
The performance of a JSP page is the same as that of the equivalent server, since the code is compiled like any other Java class. In turn, the virtual machine will dynamically compile the parts of the application that require it to machine code. This makes JSP perform well and be more efficient than other web technologies that run code in a purely interpreted way.
Advantage
The main advantage of JSP over other languages is that the Java language is a general-purpose language that exceeds the web world and that is suitable for creating classes that handle business logic and data access in a neat way. This allows the web applications to be separated into levels, leaving the part in charge of generating the HTML document in the JSP file.
Another advantage is that JSP inherits the portability of Java, and it is possible to run the applications on multiple platforms without changes. It is even common for developers to work on one platform and for the application to end up running on another.
Servlets and Java Server Pages (JSPs) are two methods of creating dynamic web pages on the server using the Java language. In that sense they are similar to other methods or languages such as PHP, ASP or CGI, programs that generate web pages on the server. However, they differ from them in other ways.
JSPs and servlets are executed in a Java virtual machine, which allows that, in principle, they can be used on any type of computer, as long as there is a Java virtual machine for it.
Each servlet (or JSP, from now on we will use it interchangeably) runs on its own thread, that is, in its own context; but it does not start executing every time it receives a request, but persists from one request to the next, so that no time is wasted in invoking it (load program + interpreter). Its persistence also allows it to do a number of things more efficiently: connecting to databases and handling sessions, for example.
JSPs are actually servlets: a JSP is compiled into a Java program the first time it is invoked, and a class is created from the Java program that starts to run on the server as a servlet. The main difference between servlets and JSPs is the programming approach: a JSP is a web page with special tags and embedded Java code, while a servlet is a pure Java program that receives requests and generates a page from them. Web.
JSP Document Example
Code example of a JSP page:
<% @ page errorPage = “myerror.jsp” %> <% @ page import = “com.foo.bar” %> < html > < head > <%! int serverInstanceVariable = 1 ;%>… <% int localStackBasedVariable = 1; %> < table > < tr > < td > </ td > </ tr >…
Example of a JSP compilation or “Output”:
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 // 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 configuration Object page = this ; PageContext pageContext =…; // get the page context for this request javax. servlet. jsp. JspWriter out = pageContext. getOut (); HttpSession session = request. getSession (true );…
To run the JSP pages, you need a web server with a web container that meets the JSP and Servlet specifications. Tomcat 5 is a complete reference implementation for the Java Servlet 2.2 and JSP 1.1 specifications.
Syntax
Implicit variables
JSP pages include certain privileged variables without the need to declare or configure them:
Variable | Class |
pageContext | javax.servlet.jsp.PageContext |
request | javax.servlet.http.HttpServletRequest |
response | javax.servlet.http.HttpServletResponse |
session | javax.servlet.http.HttpSession |
config | javax.servlet.ServletConfig |
application | javax.servlet.ServletContext |
out | javax.servlet.jsp.JspWriter |
page | java.lang.Object |
exception | java.lang.Exception |
Directives
They are labels from which information is generated that can be used by the JSP engine. They do not produce user-visible output but instead configure how the JSP page will run.
Its syntax is:
<%@ directiva atributo=”valor” %>
The available directives are:
- include: Include the content of a file on the page through the file attribute.
<%@ include file=”cabecera.html” %>
- taglib: Import tag libraries (Tag Libraries)
<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
- page: Specify attributes related to the page to be processed. The attributes are:
Attribute | Syntax | Utilization |
import | <%@ page import=”class; class” %> | Import Java classes and packages to be used within the JSP file. |
session | <%@ page session=”false” %> | It specifies if it uses the data contained in the session; by default “true”. |
contentType | <%@ page contentType=”class; class” %> | Specifies the MIME type of the “response” object; by default “text / html; charset = ISO-8859-1”. |
buffer | <%@ page buffer=”12KB” %> | Buffer used by the writer object “out”; it can take the value of “none”; by default “8KB”. |
errorPage | <%@ page errorPage=”/path_to_error_page” %> | Specify the path of the error page that will be invoked in case of an exception during the execution of this JSP file. |
isErrorPage | <%@ page isErrorPage=”true” %> | Determine if this JSP file is an exception handling page. Only these types of pages can access the implicit variable “exception”, which contains the exception that caused the call to the error page. |
Scriptlets
It allows us to declare variables, functions and static data.
<%! int maxAlumnosClase = 30; %>
Scriptlets are pieces of Java code embedded between static elements on the page.
<%… código Java… %>
The expressions are evaluated within the servlet. They must not end in “;”.
<%= maxAlumnosClase + 1%>
The following example would put the “title” attribute contained in the request object as the page title:
<% String titulo = “”; if (request.getAttribute(“titulo”) != null) { titulo = (String) request.getAttribute (“titulo”); } %>… <title><%=titulo%></title>….