Jump to content

Jakarta EE Programming/Jakarta Server Pages

25% developed
From Wikibooks, open books for an open world
(Redirected from JSP)

Here is a minimalist tutorial to create a JSP using Eclipse.

  1. If you have not already installed Eclipse, read this page.
  2. If you have not already installed an application server, read this page.
  3. Launch Eclipse
  4. On the Project Explorer view, right-click and select New -> Other...
  5. Select Web -> Dynamic Web project .
  6. Type helloworld for the Project name.
  7. On Target runtime, make sure that you have selected your application server instance.
  8. Click on Finish .
  9. Double-click on your new project to open it.
  10. Right-click on the folder WebContent .
  11. Select New -> JSP File .
  12. On File name, type FirstPage.jsp. It will be the name of your JSP.
  13. Click on Finish . The new FirstPage.jsp file should appear in the folder WebContent .
  14. Locate the text <body> in the new JSP file.
  15. After this text, write Hello World!.
  16. Right-click on the folder WebContent/WEB-INF .
  17. Select New -> File .
  18. On File name, type web.xml. This file is used to link our JSP with a URL in order to access to it. It can map many other things.
  19. Click on Finish .
  20. Double-click on the new file to open it.
  21. In the file, write the following content:
<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <servlet>
        <servlet-name>firstpage</servlet-name>
        <jsp-file>/FirstPage.jsp</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>firstpage</servlet-name>
        <url-pattern>/firstpage</url-pattern>
    </servlet-mapping>

</web-app>
  1. Right-click on the project.
  2. Select Export -> WAR file . If you don't find the option WAR file, click on Export... instead, select Web -> WAR file and click on Next >. The web project should be named helloworld .
  3. Choose a location for the destination. It's the folder where your application containing your JSP will be created. Remember this location.
  4. Click on Finish .
  5. Go on the folder where you have created your application. You should see a file named helloworld.war .
  6. Copy/paste your WAR file in the deployment folder of your application server.
  7. Start your application server.
  8. On a browser, go on http://localhost:8080/helloworld/firstpage. You should see "Hello World!".

On the URL, helloworld comes from the name of the WAR file we have created and firstpage comes from the markup <url-pattern> in the web.xml file.


Clipboard

To do:
Explain how to build a WAR without an IDE.