login

Writing the first JSP page

A JavaServer Page, technically speaking, is a web page which is embedded Java code. Java code is executed in the server side and merge with the static elements of the web page such as HTML tags... then returns the result which is plain old HTML code, JavaScript and CSS to the web browser.

This is the source code of your first JSP page which prints the simple famous greeting in programming world "Hello World" on the web browser.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <h1>
            <%
                out.println("Hello World");
            %>
        </h1>
    </body>
</html>

Here is the result when you run the JSP page in the web browser. It prints the "Hello World" as a standard heading of the HTML page.

 When we view the source code of the JSP page we can see the code as below. It is pure HTML.

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <h1>
            Hello World
 
        </h1>
    </body>
</html>

JSP Page is composed of HTML and Java code. The Java code is embedded between the notations <% and %> and  it is called Scriptlet.  Inside the scriptlet block, we call the method println of the out object to print the text "Hello World".