download eclipse Java EE
Ensure that you also have a Java Runtime Enivronment installed for Eclipse to use...
Window -> Preferences -> Server -> Runtime Environments
Add -> Apache Tomcat 6.0 (Download and Install to a directory) -> FINISH
(Note that you can specify a different JRE than the "default workbench")
OK -> Window -> Show View -> Other -> Server -> Servers OK
Right click in the new tab/space (middle center) and choose New -> Server
Automatically it should have selected your previous Tomcat 6.0 and you can click NEXT
Right are apps "published" (viewable through your browser), Left are "available", click FINISH
When you click on the Tomcat v6.0 Server at localhost [Stopped] you can select
(from a menu below the tab but above the "browser area")
Bug = debug, Play = start server, Stop ... etc.
You may see a lot of messages (errors even?) and something about CATALINA but it should finish:
[Started, Synchronized]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
File -> New -> Dynamic Web Project ... project name: helloworld
NEXT -> NEXT ... context root: helloworld Content Directory: WebContent
check Generated webxml deployment descriptor - FINISH
ECLIPSE GENERATES ALL REQUIRED FOLDER STRUCTURES FOR YOUR SERVLET PROJECT
Right click on helloworld -> New -> JSP File
Enter or select the parent folder: helloworld/WebContent
File name: index.jsp
FINISH (afterwards you'll see +helloworld -> +WebContent -> +WEB-INF ... index.jsp )
Modify the default template code so it looks like:
<%@ 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>Insert title here</title>
</head>
<body>
<%= // Comment: nothing appears in Eclipse, but IE8 is fine
new java.util.Date()
%>
</body>
</html>
Press Play - republish and you'll see nothing...
your index.jsp is being turned into a Java file, compiled and loaded...
(JSPs are compiled into servlets by a JSP compiler, servlets are Java classes
that can generate dynamic HTML content using print statements.)
Try System.getProperty for java.version, java.home, os.name, user.name, user.home, user.dir etc.
<% out.println("hello"); %>
Difference between %= , % and %@?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TROUBLESHOOTING
Remember two minor things:
1. java classname (not java classname.java e.g. filename.java)
2. http://localhost:8080/projectname/index.jsp
VMware Server 2.0 uses a "light apache tomcat" for web administration.
Running your Eclipse application/apache/tomcat may give the following error:
Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at
localhost are already in use. The server may already be running in
another process, or a system process may be using the port. To start
this server you will need to stop the other process or change the port
number(s).
From cmd.exe you should try "netstat -an" ... this will show you if the above ports are in use.
ALSO, check your firewall to ensure it is not blocking Tomcat (e.g. port 8009)
Strangely I had to modify the Tomcat properties in 2 places:
1. In Eclipse double click on the Server in the Server Perspective
(usually a tab in the center below)
Now you can modify Tomcat v6.0 Server at Localhost ... Ports
2. Modifying port 8005 up to 8006 and 8009 to 8010
/apache-tomcat-install-ROOT/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
3. Alternatively, if you're not using both at the same time, Start -> run -> Services.msc
Right click and stop all vmware services
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modular: Java computation in separate classes/files from .jsp presentation
Include directive: <%@ include file="filename" %>
Include action: <jsp:include page="page.jsp" flush="true" />
right click on Project helloworld and create a new Servlet
http://www.besthowtodo.com/blog/2010/05/how-to-create-dynamic-web-project-in-eclipse.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Right click on helloworld in Project Explorer to create a New class
Name: helloworld (yes we want a public static void main(String[] args)
NOTE: on the left the Java Resources: src ... expands to show (default package) with ...
BEST PRACTICE: create your own package, don't use the default package
You may want to add the line to the default code...
System.out.println("helloworld");
Click on the "Run" (play button at the top) for Eclipse to Build and run the application.
(Choose between Java Application or Run on Server -> NEXT -> Choose an existing Server
NEXT -> ... Configured: helloworld -> FINISH
type Status report
message /helloworld/WEB-INF/classes/helloworld.java
description The requested resource (/helloworld/WEB-INF/classes/helloworld.java) is not available.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://tomcat.apache.org
http://www.eclipse.org/downloads/
http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html
http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html