Running Web Applications in Eclipse

From EQUIS Lab Wiki

Jump to: navigation, search

In CISC 322, we will use the Eclipse programming environment to create Java and JSP programs, and the Winstone servlet container to run them. This page provides instructions for how to set up your first JSP project with Eclipse and Winstone.

Contents

Installation

Eclipse and Winstone are installed for your use in the CASLab. If you wish to install Eclipse and Winstone at home, follow these instructions.

Locating Your Files

Assuming you are developing an application called Example and that your deployment directory is called webapps, you should structure your directory like this:

  • workspace
    • Example
      • src
        • application's *.java files
      • ROOT
        • application's *.jsp and *.html files
        • WEB-INF
          • classes
            • application's *.class files
          • lib
            • applications *.jar files

You will use Eclipse to develop your application's class files, and Winstone as your servlet container to allow execution of your application from a web browser.

Running Eclipse

In the CASLab, run Eclipse via Start|Programs|Program Development|Eclipse|Eclipse 3.3.0. Make sure to use this version of Eclipse. Eclipse will prompt you where to save your workspace. The default is on your Z: drive. This is a good choice, as it will allow you to use Eclipse from any CASLab computer.

At home, run Eclipse via the shortcut you created when installing. Place your repository whereever you wish.

If this is your first time using Eclipse, I recommend that you follow the Tutorials link on the Eclipse Welcome screen (shown as a chalkboard icon), and work through the Build a simple Java application tutorial.

Creating a JSP Project Under Eclipse

You will use Eclipse to create the class files used by your web application. You can also use Eclipse to edit your JSP files.

Creating a JSP Project

  • In Eclipse, click the File menu and select New|Java Project.
  • A dialogue will appear with the header Create a Java Project.
    • In the Project Name field, enter this project's name. We will assume that the project is called Example.
    • Under Project Layout, ensure that Create separate source and output folders is selected.
    • Click Next.
  • The dialogue header will change to Java Settings.
    • Under Default Output Folder, fill in Example/ROOT/WEB-INF/classes. Note that the spelling is very important here.
    • Click Finish to create the new project.

Testing the JSP Project

You can test that the JSP project was correctly created even before you start writing any code. When executed, an empty project (like the one you just created) will allow you to view the contents of the project's ROOT directory.

  • Open a command prompt
  • Change directory to your project's ROOT directory within your Eclipse workspace
    • In the CASLAB, this will typically be the commands:
      • Z:
      • cd Workspace\Example\ROOT
  • Execute the command winstone
    • You should see the message HTTP Listener started: port-8080

Testing your Winstone Server

To test that your Winstone server is running, open a web browser, and point it to http://127.0.0.1:8080. You should see a directory listing showing no files, with a message similar to Directory list generated by Winstone Servlet Engine v0.9.9 at Sat Sep 08 15:10:22 EDT 2007.

Creating a JSP Program

The combination of Eclipse and Winstone makes creating JSP pages very easy. The short version is that you need to create a file with a .jsp extension in the ROOT directory of your project.

These instructions assume you are working in a project created following the instructions given above.

Configuring Eclipse to Edit JSPs

Eclipse does not contain a JSP editor. We will simply use Eclipse's text editor for JSPs. To do this:

  • Open Window|Preferences...
  • Open General|Editors|File Associations
  • Click Add... under File types
    • In the dialogue, enter *.jsp
  • Click Add... under Associated editors
    • Select Text editor in the dialogue

You only need to perform this step once.

Editing a JSP

Right-click on the ROOT folder in the Package Explorer pane. Select New and File. A dialog with the header Create a file resource will appear.

Ensure that the ROOT folder is selected in the dialogue. Select it if it is not.

Enter index.jsp in the File name field and click Finish. The dialogue will disappear and an index.jsp file will appear in the ROOT folder.

If you need a quick example of the features available in JSP, paste the following into the editor:

<%@ page import="java.io.*"
         import="java.util.*" %>

<jsp:useBean id="map" scope="page" class="java.util.HashMap"/>

<html>
<head></head>
<body>

<% /* This starts a block of Java code */

    map.put("key", "value");
    map.put("foo", "bar");
    map.put("baz", "woo");
    
%> 

The value of 'key' is: <%= map.get("key") %>

<table>
<%
    /* loops and conditionals can include HTML content */
    for (Iterator it = map.entrySet().iterator(); it.hasNext(); )
    {
        Map.Entry entry = (Map.Entry) it.next();
%>
<tr><td><%= entry.getKey() %></td><td><%= entry.getValue() %></td></tr>
<%  } %>
</table>

<%
    /* to access parameters supplied by the browser, use
       the 'request' object */

    String username = request.getParameter("username");
    if (username != null)
    {
	/* the response.sendError method can be used to
          deliver HTTP errors. */
 	response.sendError(HttpServletResponse.SC_FORBIDDEN,
		"You tried to specify a username parameter");
    }

%>

</body>
</html>

Running a JSP

To view your JSP, simply browse to http://127.0.0.1:8080/index.jsp.

Java Classes and JSPs

When creating Java classes to be used from the JSPs, you must place them in a non-default package (i.e. they must have a package declaration). JSP files are compiled to Java classes in a package chosen by the container, so can not easily access classes from the default package.