How to Run a JSP Program in Apache Tomcat (Linux)

A Java Server Page, or JSP, program is a crucial part of a Java web application because the JSP will send a response back to the server in the form of a web page. For example, a JSP might display the line items of an order to the browser user. In this topic, you will create a very simple JSP and learn how to run the program at the Tomcat server.

To learn how to run a JSP in Apache Tomcat in a Linux environment, follow these 7 steps:

  1. In your text editor, you will develop a simple JSP that creates a web page to display the current date. Type in the following statements in a new file:
    <%@ page language="java" contentType="text/html"%>
    <%@ page import="java.text.*,java.util.*" %>
    <title>Date JSP</title>
    <% SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy"); %>
    <h1>Welcome to Tomcat! Today is <%= sdf.format(new Date()) %></h1>
    The program contains <%@ tags that provide metadata about the JSP and import directives. The JSP also contains HTML tags that will be rendered by the browser in the usual way. Note the <% tags that encapsulate Java code. The snippets of Java code are referred to as "scriplets." When the JSP is requested by the browser, the program will be converted into a servlet by a program in the Tomcat container (Jasper) and the HTML output will be sent to the browser.
  2. Save your file as DateJSP.jsp.
  3. Copy your file to CATALINA_BASE/webapps/ROOT, e.g., /var/lib/tomcat7/webapps/ROOT.
  4. Start the Tomcat server.
  5. Start your browser if it is not already running.
  6. In the address area of the browser, type http://localhost:8080/DateJSP.jsp and submit that address to the browser.
  7. The output of your JSP page will be displayed:
    Date JSP
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.