What are the JSP comments in Java


The JavaServer Pages technology (JSP for short) enables us to do this easy generation of a dynamic HTML page (or XML page). Here is HTML and Java code mixed directly and is therefore comparable to PHP.

JavaServer Pages usually have the file extension and contain normal HTML code and some Blocks of codewhich contain Java code. Through the code blocks you can dynamic expenses, Queries and much more can be performed. The resulting concept enables the mixing of HTML and Java code. This is not always desirable due to the MVC concept, but it is an easy way to create a page.

In principle, all classes of the Java packages can be used within a JavaServer Page. In addition, objects from various servlet classes and interfaces can be called up using so-called standard variables.

The JavaServer Pages technology was already in use at the end of the 1990s and is therefore one of the older technologies for server-side programming. Because of the ever-increasing Replacement by other technologies and the use of frameworks (such as JavaServer Faces), JSP is used less and less and has been considered obsolete since Java EE version 6. deprecated). Therefore, it is not recommended to use JSP for new projects. However, we also offer tutorials on many other server-side technologies on this website.

First example

To get a better idea of ​​JSP, let's first create a simple JSP example. As in the servlet tutorial, we have the NetBeans development environment as well as the Apache Tomcat web server, which is part of the XAMPP software package, was used to create the examples in this tutorial. If you have not yet installed an IDE or web server, we would recommend that you use NetBeans as this will allow you to follow the steps below.

After starting the NetBeans development environment, we have to create a new project. In the project creation dialog, we select the “Java Web” category and the “Web Application” project type. Next we have to select the project name and storage location and then the server (in this case the Apache Tomcat) and the version (usually Java EE 6). We don't have to choose anything when integrating frameworks.

The project that has now been created already contains a JSP file:. This file was generated automatically and is located in the root directory of the web project. The generated JavaServer Page already contains a basic HTML structure and a directive (more on this in the Directives section), but no code block yet. Now you can customize the page and insert the following Java code:. Now when you run the project you will notice that the page shows the current date and time. If you reload the page in the browser, the date and time will also be updated. You now have yours first dynamic website created using JSP.

<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Erste Seite - JavaServer Pages Code-Beispiel</title> <meta charset="utf-8" /> <meta name="robots" content="noindex,nofollow" /> <meta name="publisher" content="Homepage-Webhilfe" /> </head> <body> Serverzeit: <%= (new java.util.Date()).toString() %> </body> </html>


In principle, JavaServer contain pages normal HTML or XML code. To get there now dynamic content as well as placing Java code, special tags or XML elements are used. A rough division can be made between script elements (often also referred to as script tags or code blocks), directives and actions. We will go into more detail on the directives and actions in the next few topics.

There are three different ones in Java Script elementswhich differ in the opening day. The closing day of the elements is always. The simplest script element begins with the script tag. Java code can be specified between and. This code will run where you put it. This syntax is often called Scriptlet designated. Here is a short example:

<% for (int i = 0; i < 10; i++) out.println(i + "<br />"); %>

If you Define global variables or methods you have to replace the start day with. It could look like this:

<%! private double berechneKreisUmfang(double dRadius) { return 2 * Math.PI * dRadius; } %>

In order to output a value (constant, variable or return value of a function), we could call the function and this the value to be output to hand over. However, the script tag is preferable. Ultimately, the function is also called here, but this makes our code shorter and easier to read.

<%= (new java.util.Date()).toString() %>

If you have a comment place, you can use the tags and. The difference between the JSP comment and a simple HTML code is that the JSP comment only exists on the server side and is therefore not sent to the browser. Depending on the intended use, either the JSP or the HTML comment is to be preferred.

<%-- Dies ist serverseitiger Kommentar! --%>

Standard variables

As already mentioned above, we can access standard variables in the form of objects within a JSP, which enable us to query information or to adapt the "response". Some of these objects may sound familiar to you from the servlet tutorial:

variableClass / interfacedescription
requestHttpServletRequestObject for retrieving request information
responseHttpServletResponseObject to customize the answer
outJspWriterObject for the output stream
sessionHttpSessionObject of the HTTP session
pageObjectObject of the page itself
pageContextPageContextObject for extended access to the page or its data
exceptionThrowableObject for exception error information (for error pages)
applicationServletContextObject of the web server application
configServletConfigConfiguration object


The best way to explain how JavaServer Pages works is with a step-by-step sequence:

  1. First, the visitor's web browser sends a request to the web server.
  2. The web server receives the request. request) and saves the request information.
  3. The web server now determines that the requested resource is a JavaServer Page (this is determined by means of the file extension).
  4. Since it is a JavaServer Page, the page must be accompanied by a JSP compiler to be translated. The JSP is simply converted into a completely normal HTTP servlet.
  5. In the next step, the HTTP servlet is supported by the Java compiler translated and thus converted into a file. This file is saved for possible renewed use (more on this in a moment).
  6. The generated Java bytecode, which represents the servlet, is now executed by the servlet container.
  7. The servlet, which was executed by the servlet container, next sends an HTTP response. response) generated. This contains i. d. Usually an HTTP header and an HTML page that appears statically for the browser.
  8. The generated answer is sent back to the browser.

The Java bytecode of the servlet generated by the JSP compiler is saved. This means that it has to be used for a new request cannot be recompiledbut can be executed directly. To see whether the JSP file has changed in the meantime, the Checked the modification date of the file. If a change was recognized, a compilation is carried out again.