The first JSP

Java Server Page is a web page document that integrate Java codes to generate a dynamic content. In this tutorial we will create a simple JSP that show the current time.

  1. Open Eclipse, create a Dynamic Web Project, name it jsplabs and set Target runtime as Apache Tomcat v7.0.


  2. Right click on the WebContent folder, select New→JSP file.
  3. Name it first.jsp and click Finish


  4. Insert the following codes between the <body> tags:

    <h1>Fist JSP</h1>
    <ul>
    <li>Current time: <%= new java.util.Date() %>
    <li>Server: <%= application.getServerInfo() %>
    <li>Session ID: <%= session.getId() %>
    </ul>
  5. Save the file.
  6. Right click on the first.jsp file, select Run As→Run on Server.
  7. Select your Tomcat server and click on Finish.


  8. The file first.jsp is automatically opened on your browser at the URL: http://localhost:8080/jsplabs/first.jsp.


Exercises

  1. Play with some other functions, such as: application.getContextPath(); session.getCreationTime(); request.getRemoteHost(); request.getServerName(); request.getServerPort(); request.getContextPath(); request.getRemoteHost().
  2. View some system property by using function: System.getProperty(“PROPERTY_NAME”). Replace PROPERTY_NAME by os.name, user.name, user.home, user.dir, user.timezone, java.home, java.version.