Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

6. JavaServer Pages (JSPs)

6.1. Create Project

The following will demonstrate the creation and usage of a JaveServer Page . Create the Dynamic Web Project "de.vogella.wtp.jspsimple". and the package "de.vogella.wtp.jspsimple"

6.2. Create the JSP

Select the folder "WebContent", right-mouse click -> New -> JSP and create the JSP "FirstJSP". Select the "New JSP File (html)" template.

Create the following coding.

				
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP with the current date</title>
</head>
<body>
<%java.text.DateFormat df = new java.text.SimpleDateFormat("dd/MM/yyyy"); %>

<h1>Current Date: <%= df.format(new java.util.Date()) %> </h1>
</body>
</html>
			

6.3. Run it

Start your webapplication. You find your JSP under the URL "http://localhost:8080/de.vogella.wtp.jspsimple/FirstJSP.jsp".

6.4. Adjust web.xml

Set the JSP page as the welcome page for your application. This is optional but make it easier because the JSP is automatically opened if the application is started. Modify the file "WebContent/WEB-INF/web.xml" to the following.

				
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>de.vogella.wtp.jspsimple</display-name>
  <welcome-file-list>
    <welcome-file>FirstJSP.jsp</welcome-file>
  </welcome-file-list>
</web-app>
			

This allows to start the JSP via the path ""http://localhost:8080/de.vogella.wtp.jspsimple".