Starting and stopping Tomcat from within the Eclipse IDE, once set up, can speed up your Java Web Application development time because you won't have the extra step of opening the terminal to start and stop Tomcat. The following instructions assume you have Eclipse installed and Tomcat installed.
Step 1: Open up the External Tools Configurations in Eclipse. In Eclipse's toolbar click on the little icon with a run symbol and the toolbox, then External Tools Configurations... .
Step 2: In the dialog box that pops up, right-click on Program and select "New".
Step 3: Give the new run configuration a name such as "Stop Tomcat", and in the "Location" text field enter "/Library/Tomcat/bin/shutdown.sh". You need to adjust the path of the shutdown.sh to match the file's location on your computer. Also, if you're using a Windows machine you need to call shutdown.bin instead. Click "Run".
Step 4: Create another configuration for starting Tomcat be repeating steps 1 through 3.
From now on, starting and stopping Tomcat as well as building your projects is accessible with a click of the mouse. Just click the little down-arrow next to that icon with a run symbol and the toolbox, and you'll see a list of the run configurations that you have set up.
Piece of Cake!!
Sunday, November 15, 2009
Set Tomcat Memory Heap Size
Want to integrate charts into your webapp? Check out XChart.
If your Java Web Application running on Tomcat ever crashes and throws the exception:
SEVERE: Servlet.service() for servlet WebAppServlet threw exception
java.lang.OutOfMemoryError: Java heap space
, you need to increase the allocated memory that Tomcat has for running applications. This is accomplished by adding "-Xms256m -Xmx512m" to the catalina.sh file found in the "bin" directory of the Tomcat installation, where 256 and 512 should be replaced with appropriate values for your application. Xms mean starting heap size and Xmx means maximum heap size. I was able to increase the memory allocated to Tomcat on a Mac running Mac OS X Leopard with Tomcat 6.0.20 installed. These instructions should work for any *nix machine, but I can't guarantee it.
Step 1: Open up the catalina.sh file with any text editor.
Step 2: Search for "JAVA_OPTS=" in the file and in particular locate the following lines of code:
Step 3: At the end of the JAVA_OPTS definitions add "-Xms256m -Xmx512m".
Step 4: Save the file and restart Tomcat.
Piece of Cake!!
If your Java Web Application running on Tomcat ever crashes and throws the exception:
SEVERE: Servlet.service() for servlet WebAppServlet threw exception
java.lang.OutOfMemoryError: Java heap space
, you need to increase the allocated memory that Tomcat has for running applications. This is accomplished by adding "-Xms256m -Xmx512m" to the catalina.sh file found in the "bin" directory of the Tomcat installation, where 256 and 512 should be replaced with appropriate values for your application. Xms mean starting heap size and Xmx means maximum heap size. I was able to increase the memory allocated to Tomcat on a Mac running Mac OS X Leopard with Tomcat 6.0.20 installed. These instructions should work for any *nix machine, but I can't guarantee it.
Step 1: Open up the catalina.sh file with any text editor.
Step 2: Search for "JAVA_OPTS=" in the file and in particular locate the following lines of code:
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi
Step 3: At the end of the JAVA_OPTS definitions add "-Xms256m -Xmx512m".
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms256m -Xmx512m"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Xms256m -Xmx512m"
fi
Step 4: Save the file and restart Tomcat.
Piece of Cake!!
Setup Tomcat and Connector/J MySQL Connector
Using MySQL and Java? Check out an easier way: Yank
If you plan to create a Java Web Application which runs in Tomcat and accesses a MySQL database you'll need to add a Connector/J MySQL Connector Jar to the Tomcat installation. That's all you should really need for setting up Tomcat and Connector/J. Here's how to do that in detail assuming you already have Tomcat installed.
Step 1: Download the most current release of the Connector/J MySQL Connector from mysql.com and unpack the *.tar.gz file.
Step 2: Inside the unpacked folder you'll find a Jar called "mysql-connector-java-5.1.10-bin.jar" or something similar. Copy that file into Tomcat's "lib" directory.
Step 3: Restart Tomcat.
Piece of Cake!!
Friday, November 13, 2009
Setup Eclipse and CVS on Mac OS X
Integrating CVS with your Eclipse IDE seems like a daunting feat, but in fact it's not too bad. Let's get right to it...
Step 1: Install Xcode, the Mac Application development suite, which can be downloaded for free at developer.apple.com after you sign up for a ADC membership. One teeny-tiny part of the install puts the CVS code on your computer where you have acess to it's function via the terminal. I believe, but am not sure, that you can install Xcode from the Mac OS X installation CD. If there is a way to just get the CVS part of xCode and install it separately, please tell me how. :)
Step 2: Create a folder for your CVS repository somewhere. I created a folder called "csv_repository" on the root of the hard drive, i.e. /csv_repository.
Step 3: Fire up the Terminal and enter:
Your CVS repository is now set up!
Step 4: Start Eclipse and set up the workspace so that the CVS Repositories and the Synchronize views are shown. I like to dock them underneath the Package Explorer view.
Step 5: Right-click somewhere in the CVS Repositories View and select New --> Repository Location. A Wizard opens up that you use to create a new Repository.
Step 6: Fill out the info as follows using the computer's admin username and password. If you installed the repository under a specific user account, use that accounts's name and password.
Step 7: Add your project to the CVS repository. In this tutorial, I'm using a Hello World Java Web Application for the CVS demonstration. In the Package Explorer view, right-click on the project you'd like to add. Select Team --> Share Project... .
Step 8: Go through the wizard, on the first page, selecting the new repository location you added in steps 5 and 6. On the "Share Project Resources" page of the wizard add the "build" and "dist" folders to .cvsignore by right-clicking on it and selecting "Add to .cvsignore...". These two folders are dynamically created for deploying the web app during the ANT build process and are not needed in the repository. Also add .cvsignore (yup, you should add the .cvsignore to itself). Click Finish.
Step 9: Set up the synchronizer. In the Synchronize View, there's a little icon in the view's tool bar that when you hover over it it says "synchronize...". Click that button. Another wizard pops up. Just click through it to the end not changing anything.
Step 10. Commit your code to the repository. Right-click the resources you want to commit and choose Commit... . In the window that pops up enter a comment such as "initial commit" and click "Finish".
That's it, your project is committed! Now onto some optional testing steps...
Step 11: Make a change in your source code and commit it. In my Hello World Java Web Application, I changed some code in the Servlet class. To commit the change, right-click on the file and select Team --> Commit. Writing a comment during the commit is a good habit to get into because it will really help one day when your looking back through the history of your file for a certain change you made. Also, you can always go to the Synchronize view to commit your code when you have extensive changes in your project.
Step 12: View the history of the Servlet. In the Package Explorer view, right-click on the file and select Team --> show history. You should see a history of the file which you can use to compare different versions of the file.
Piece of Cake!!
Step 1: Install Xcode, the Mac Application development suite, which can be downloaded for free at developer.apple.com after you sign up for a ADC membership. One teeny-tiny part of the install puts the CVS code on your computer where you have acess to it's function via the terminal. I believe, but am not sure, that you can install Xcode from the Mac OS X installation CD. If there is a way to just get the CVS part of xCode and install it separately, please tell me how. :)
Step 2: Create a folder for your CVS repository somewhere. I created a folder called "csv_repository" on the root of the hard drive, i.e. /csv_repository.
Step 3: Fire up the Terminal and enter:
cvs -d /cvs_repository init
Your CVS repository is now set up!
Step 4: Start Eclipse and set up the workspace so that the CVS Repositories and the Synchronize views are shown. I like to dock them underneath the Package Explorer view.
Step 5: Right-click somewhere in the CVS Repositories View and select New --> Repository Location. A Wizard opens up that you use to create a new Repository.
Step 6: Fill out the info as follows using the computer's admin username and password. If you installed the repository under a specific user account, use that accounts's name and password.
Step 7: Add your project to the CVS repository. In this tutorial, I'm using a Hello World Java Web Application for the CVS demonstration. In the Package Explorer view, right-click on the project you'd like to add. Select Team --> Share Project... .
Step 8: Go through the wizard, on the first page, selecting the new repository location you added in steps 5 and 6. On the "Share Project Resources" page of the wizard add the "build" and "dist" folders to .cvsignore by right-clicking on it and selecting "Add to .cvsignore...". These two folders are dynamically created for deploying the web app during the ANT build process and are not needed in the repository. Also add .cvsignore (yup, you should add the .cvsignore to itself). Click Finish.
Step 9: Set up the synchronizer. In the Synchronize View, there's a little icon in the view's tool bar that when you hover over it it says "synchronize...". Click that button. Another wizard pops up. Just click through it to the end not changing anything.
Step 10. Commit your code to the repository. Right-click the resources you want to commit and choose Commit... . In the window that pops up enter a comment such as "initial commit" and click "Finish".
That's it, your project is committed! Now onto some optional testing steps...
Step 11: Make a change in your source code and commit it. In my Hello World Java Web Application, I changed some code in the Servlet class. To commit the change, right-click on the file and select Team --> Commit. Writing a comment during the commit is a good habit to get into because it will really help one day when your looking back through the history of your file for a certain change you made. Also, you can always go to the Synchronize view to commit your code when you have extensive changes in your project.
Step 12: View the history of the Servlet. In the Package Explorer view, right-click on the file and select Team --> show history. You should see a history of the file which you can use to compare different versions of the file.
Piece of Cake!!
Thursday, November 12, 2009
Hello World Java Web Application in Eclipse (Part 3)
Want to integrate charts into your webapp? Check out XChart.
Download .war and full source code of this and other sample Java Web Applications here.
This is the third of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In Part 1, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In Part 2, I take you through getting the Java jar needed to code Java servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In this part, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
Step 1: Understand the build.xml file. After Part 2, we have a Hello World Java Web Application project in Eclipse named HelloWebApp with the build.xml file shown below. The build file is used by ANT to take all the source code and configuration files in the Web App project, compile them, and package them in a .war file, which Tomcat needs to run your application.
build.xml
build.properties
The build file consists of several targets and a properties file and classpath definition. The ANT builder, which comes bundled with Eclipse parses this file and carries out the instructions defined within it. Here, I'll go through the file step-by-step and explain what the ANT builder does.
Line 2: The "deploy_local" target is the default target and will be implemented first. The "basedir", where all the paths will be relative to is defined as "." meaning the directory where the build.xml file is found.
Line 4: The properties file "build.properties" is imported by ANT and stores certain key value pairs used for the build. Mostly directory names and paths are defined in the properties file.
Line 6: The jars in the classpath are used by ANT when compiling the Java source code.
Line 51: Let's skip down to this line because ANT starts with this target because it is defined as the default target. This target depends on the "war" target, which depends on the "copy" target, which depends on the "compile" target, which depends on the "init" target, which depends on the "clean" target. So actually, this means the "clean" target will be implemented first, then the "init" and so on...
Line 10: The "echo" element just prints out to the console what you tell it to. In this case, "Cleaning the ${build.dir}", where "${build.dir}" is replaced with the value "build" from the build.properties file, and "Cleaning the build" is printed out. The "delete" elements delete the "build" and "dist" folders in the project if they exist.
Line 16: The "init" target creates 3 folders in the project: "build/WEB-INF/classes", "build/WEB-INF/lib", and "dist".
Line 23: The "compile" target compiles all the java files found in the src folder and puts them in the "build/WEB-INF/classes" folder. It uses the jars defined on the classpath on line 6.
Line 30: The "copy" target moves files from the "web" and "lib" folders to the "build" folder.
Line 39: We don't copy over the servlet-api.jar because the Tomcat server already contains this jar. After all, that we're the jar came from in the first place.
Line 44: The "war" target takes everything in the "build folder", makes a WAR file and puts it in the "dist" folder.
Line 53: Inside the "deploy_local" target, the war is copied from the "dist" folder to the "/Library/Tomcat/webapps" folder, which is where the Tomcat installation resides.
Step 2: Run ANT to build the project. Right-click on build.xml and choose Run As --> Ant Build.
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View after the ANT build:
Step 3: Startup Tomcat. Now that the Tomcat installation has the WAR file in it's webapps folder, you need to start Tomcat up so it runs the web app. To do this, fire up the terminal and type: "sh /Library/Tomcat/bin/startup.sh". If you're using a non-UNIX-based computer (Windows), you have to start the startup.bat file instead, remove the sh at the front of the command, and adjust your path appropriately.
Step 4: Check out your web application in action. Point your browser to http://127.0.0.1:8080/HelloWebApp/ . You should see the Hello Web App web application in it's full glory!
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 1)
Hello World Java Web Application in Eclipse (Part 2)
See Also:
Java Ant - Build Jar with Auto-Incrementing Build Number
Java Ant - Build .War file with Auto-Incrementing Build Number
Building with ANT and Deploying to Tomcat
Download .war and full source code of this and other sample Java Web Applications here.
This is the third of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In Part 1, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In Part 2, I take you through getting the Java jar needed to code Java servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In this part, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
Step 1: Understand the build.xml file. After Part 2, we have a Hello World Java Web Application project in Eclipse named HelloWebApp with the build.xml file shown below. The build file is used by ANT to take all the source code and configuration files in the Web App project, compile them, and package them in a .war file, which Tomcat needs to run your application.
build.xml
<?xml version="1.0"?> <project name="hello" default="deploy_local" basedir="."> <property file="build.properties"/> <path id="classpath"> <fileset dir="${lib.dir}" includes="servlet-api.jar"/> </path> <target name="clean"> <echo>Cleaning the ${build.dir}</echo> <delete dir="${build.dir}"/> <delete dir="${dist.dir}"/> </target> <target name="init" depends="clean"> <echo>Creating the build directory</echo> <mkdir dir="${build.dir}/WEB-INF/classes"/> <mkdir dir="${build.dir}/WEB-INF/lib"/> <mkdir dir="${dist.dir}"/> </target> <target name="compile" depends="init"> <echo>Compile the source files</echo> <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes"> <classpath refid="classpath"/> </javac> </target> <target name="copy" depends="compile"> <copy todir="${build.dir}/WEB-INF"> <fileset dir="${web.dir}/WEB-INF"/> </copy> <copy todir="${build.dir}"> <fileset dir="${web.dir}"/> </copy> <copy todir="${build.dir}/WEB-INF/lib"> <fileset dir="${lib.dir}"> <exclude name="servlet-api.jar"/> </fileset> </copy> </target> <target name="war" depends="copy"> <echo>Building the war file</echo> <war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml"> <fileset dir="${build.dir}"/> </war> </target> <target name="deploy_local" depends="war"> <echo>Deploying .war to local Tomcat</echo> <copy todir="${tomcat.dir}"> <fileset dir="${dist.dir}"> <include name="${project.name}.war"/> </fileset> </copy> </target> </project>
build.properties
project.name=HelloWebApp lib.dir=lib src.dir=src web.dir=web build.dir=build dist.dir=dist tomcat.dir=/Library/Tomcat/webapps
The build file consists of several targets and a properties file and classpath definition. The ANT builder, which comes bundled with Eclipse parses this file and carries out the instructions defined within it. Here, I'll go through the file step-by-step and explain what the ANT builder does.
Line 2: The "deploy_local" target is the default target and will be implemented first. The "basedir", where all the paths will be relative to is defined as "." meaning the directory where the build.xml file is found.
Line 4: The properties file "build.properties" is imported by ANT and stores certain key value pairs used for the build. Mostly directory names and paths are defined in the properties file.
Line 6: The jars in the classpath are used by ANT when compiling the Java source code.
Line 51: Let's skip down to this line because ANT starts with this target because it is defined as the default target. This target depends on the "war" target, which depends on the "copy" target, which depends on the "compile" target, which depends on the "init" target, which depends on the "clean" target. So actually, this means the "clean" target will be implemented first, then the "init" and so on...
Line 10: The "echo" element just prints out to the console what you tell it to. In this case, "Cleaning the ${build.dir}", where "${build.dir}" is replaced with the value "build" from the build.properties file, and "Cleaning the build" is printed out. The "delete" elements delete the "build" and "dist" folders in the project if they exist.
Line 16: The "init" target creates 3 folders in the project: "build/WEB-INF/classes", "build/WEB-INF/lib", and "dist".
Line 23: The "compile" target compiles all the java files found in the src folder and puts them in the "build/WEB-INF/classes" folder. It uses the jars defined on the classpath on line 6.
Line 30: The "copy" target moves files from the "web" and "lib" folders to the "build" folder.
Line 39: We don't copy over the servlet-api.jar because the Tomcat server already contains this jar. After all, that we're the jar came from in the first place.
Line 44: The "war" target takes everything in the "build folder", makes a WAR file and puts it in the "dist" folder.
Line 53: Inside the "deploy_local" target, the war is copied from the "dist" folder to the "/Library/Tomcat/webapps" folder, which is where the Tomcat installation resides.
Step 2: Run ANT to build the project. Right-click on build.xml and choose Run As --> Ant Build.
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View after the ANT build:
Step 3: Startup Tomcat. Now that the Tomcat installation has the WAR file in it's webapps folder, you need to start Tomcat up so it runs the web app. To do this, fire up the terminal and type: "sh /Library/Tomcat/bin/startup.sh". If you're using a non-UNIX-based computer (Windows), you have to start the startup.bat file instead, remove the sh at the front of the command, and adjust your path appropriately.
Step 4: Check out your web application in action. Point your browser to http://127.0.0.1:8080/HelloWebApp/ . You should see the Hello Web App web application in it's full glory!
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 1)
Hello World Java Web Application in Eclipse (Part 2)
See Also:
Java Ant - Build Jar with Auto-Incrementing Build Number
Java Ant - Build .War file with Auto-Incrementing Build Number
Hello World Java Web Application in Eclipse (Part 2)
Want to integrate charts into your webapp? Check out XChart.
Download .war and full source code of this and other sample Java Web Applications here.
This is the second of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In Part 1, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In this part, I take you through getting the Java jar needed to code Java Servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
From part 1, we have a Java project in Eclipse named HelloWebApp with the following directory structure:
HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB_INF
Step 1: Put the servlet-api.jar, which comes with the Tomcat installation, in the lib folder. The jar is found in the lib folder in the Tomcat installation's root directory.
Step 2: Add the jar to the project's build path. Right-click on the jar in the lib folder of the Web App project and select Build Path --> Add to Build Path.
Step 3: Create a Servlet file. Right-click on the com.hello package in the src folder and select New --> Class. Name it HelloServlet.java. Copy and paste the following code into the file.
Step 4: Create a JSP file. Right-click on the "web" folder and select New --> Other... . A wizard will pop up and you need to find the JSP file type, which is in the Web folder. Name it hello.jsp. Copy and paste the following code into the file.
Step 5: Create another JSP file. Name it index.jsp. Copy and paste the following code into the file.
Step 6: Create an XML file. Right-click on the "WEB_INF" folder and select New --> File. Name it web.xml. Copy and paste the following code into the file.
Step 7: Create another XML file. Right-click on the HelloWebApp project and select New --> File. Name it build.xml. Copy and paste the following code into the file.
Step 8: Create a properties file. Right-click on the HelloWebApp project and select New --> File. Name it build.properties. Copy and paste the following code into the file.
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View:
OK, that's what you need for a fully functional HelloWorld-type Java Web Application that runs on a Tomcat Server. In the next and final part, I will explain the build.xml file, use ANT to build the project and deploy it to the Tomcat server.
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 1)
Hello World Java Web Application in Eclipse (Part 3)
Servlet, JSPs, web.xml, build.xml, build.properties
Download .war and full source code of this and other sample Java Web Applications here.
This is the second of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In Part 1, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In this part, I take you through getting the Java jar needed to code Java Servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
From part 1, we have a Java project in Eclipse named HelloWebApp with the following directory structure:
HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB_INF
Step 1: Put the servlet-api.jar, which comes with the Tomcat installation, in the lib folder. The jar is found in the lib folder in the Tomcat installation's root directory.
Step 2: Add the jar to the project's build path. Right-click on the jar in the lib folder of the Web App project and select Build Path --> Add to Build Path.
Step 3: Create a Servlet file. Right-click on the com.hello package in the src folder and select New --> Class. Name it HelloServlet.java. Copy and paste the following code into the file.
package com.hello; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = 1L; public HelloServlet() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("hello_string", "Hello WebApp!"); request.getRequestDispatcher("/hello.jsp").forward(request, response); } }
Step 4: Create a JSP file. Right-click on the "web" folder and select New --> Other... . A wizard will pop up and you need to find the JSP file type, which is in the Web folder. Name it hello.jsp. Copy and paste the following code into the file.
<%@ 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>Hello Web App</title> </head> <body> <h3><%=(String)request.getAttribute("hello_string")%></h3> </body> </html>
Step 5: Create another JSP file. Name it index.jsp. Copy and paste the following code into the file.
<%@ 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>Index</title> </head> <body> <jsp:forward page="/hello" /> </body> </html>
Step 6: Create an XML file. Right-click on the "WEB_INF" folder and select New --> File. Name it web.xml. Copy and paste the following code into the file.
<?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>HelloWebApp</display-name> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.hello.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Step 7: Create another XML file. Right-click on the HelloWebApp project and select New --> File. Name it build.xml. Copy and paste the following code into the file.
<?xml version="1.0"?> <project name="hello" default="deploy_local" basedir="."> <property file="build.properties"/> <path id="classpath"> <fileset dir="${lib.dir}" includes="servlet-api.jar"/> </path> <target name="clean"> <echo>Cleaning the ${build.dir}</echo> <delete dir="${build.dir}"/> <delete dir="${dist.dir}"/> </target> <target name="init" depends="clean"> <echo>Creating the build directory</echo> <mkdir dir="${build.dir}/WEB-INF/classes"/> <mkdir dir="${build.dir}/WEB-INF/lib"/> <mkdir dir="${dist.dir}"/> </target> <target name="compile" depends="init"> <echo>Compile the source files</echo> <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes"> <classpath refid="classpath"/> </javac> </target> <target name="copy" depends="compile"> <copy todir="${build.dir}/WEB-INF"> <fileset dir="${web.dir}/WEB-INF"/> </copy> <copy todir="${build.dir}"> <fileset dir="${web.dir}"/> </copy> <copy todir="${build.dir}/WEB-INF/lib"> <fileset dir="${lib.dir}"> <exclude name="servlet-api.jar"/> </fileset> </copy> </target> <target name="war" depends="copy"> <echo>Building the war file</echo> <war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml"> <fileset dir="${build.dir}"/> </war> </target> <target name="deploy_local" depends="war"> <echo>Deploying .war to local Tomcat</echo> <copy todir="${tomcat.dir}"> <fileset dir="${dist.dir}"> <include name="${project.name}.war"/> </fileset> </copy> </target> </project>
Step 8: Create a properties file. Right-click on the HelloWebApp project and select New --> File. Name it build.properties. Copy and paste the following code into the file.
project.name=HelloWebApp lib.dir=lib src.dir=src web.dir=web build.dir=build dist.dir=dist tomcat.dir=/Library/Tomcat/webapps
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View:
OK, that's what you need for a fully functional HelloWorld-type Java Web Application that runs on a Tomcat Server. In the next and final part, I will explain the build.xml file, use ANT to build the project and deploy it to the Tomcat server.
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 1)
Hello World Java Web Application in Eclipse (Part 3)
Hello World Java Web Application in Eclipse (Part 1)
Want to integrate charts into your webapp? Check out XChart.
Download .war and full source code of this and other sample Java Web Applications here.
This is the first of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In this part, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In Part 2, I take you through getting the Java jar needed to code Java servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
Step 1: Install the Eclipse IDE for Java EE developers. Once installed, open up Eclipse and go to the Eclipse Workbench. I also recommend tweaking the default Eclipse development settings for a much cleaner and efficient coding experience.
Step 2: Create a new Java Project. Right-click somewhere in the Package Explorer View and select New--->Project... .
On page 1 of the Project Wizard, choose "Java Project".
On page 2 of the Project Wizard, give the project the name: "HelloWebApp".
Finish the wizard.
Step 3: Set up the directory structure of the web app project. For this I followed the recommendations outlined in the Tomcat documentation that comes with the Tomcat installation, except I chose to include a lib folder directly in the project to hold Java jars needed for the project. Create the nested directory structure as follows:
HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB-INF
The src folder holds all your Java source files including any Servlet classes, which you place in the com.hello package. The lib folder holds any 3rd party jars your project depends on. The web folder holds any .jsp or .html pages, Javascript files, and CSS files. The web folder also contains a folder named WEB_INF, which hold the project's web.xml file.
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View.
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 2)
Hello World Java Web Application in Eclipse (Part 3)
Java Web Application Structure in Eclipse
Download .war and full source code of this and other sample Java Web Applications here.
This is the first of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In this part, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In Part 2, I take you through getting the Java jar needed to code Java servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.
Step 1: Install the Eclipse IDE for Java EE developers. Once installed, open up Eclipse and go to the Eclipse Workbench. I also recommend tweaking the default Eclipse development settings for a much cleaner and efficient coding experience.
Step 2: Create a new Java Project. Right-click somewhere in the Package Explorer View and select New--->Project... .
On page 1 of the Project Wizard, choose "Java Project".
On page 2 of the Project Wizard, give the project the name: "HelloWebApp".
Finish the wizard.
Step 3: Set up the directory structure of the web app project. For this I followed the recommendations outlined in the Tomcat documentation that comes with the Tomcat installation, except I chose to include a lib folder directly in the project to hold Java jars needed for the project. Create the nested directory structure as follows:
HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB-INF
The src folder holds all your Java source files including any Servlet classes, which you place in the com.hello package. The lib folder holds any 3rd party jars your project depends on. The web folder holds any .jsp or .html pages, Javascript files, and CSS files. The web folder also contains a folder named WEB_INF, which hold the project's web.xml file.
Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View.
Piece of Cake!!
Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 2)
Hello World Java Web Application in Eclipse (Part 3)
Wednesday, November 11, 2009
Setup Eclipse for Efficient Java Coding
The default Java coding settings in Eclipse are fine until you realize one day that there are some really neat things that Eclipse can do automatically for you that aren't activated by default. Here's how I like to set up my Eclipse development environment after a clean install.
Step 1: Make the text editor show line numbers. This helps immensely when reading stack traces and locating errors in your code. Go to the Eclipse Preferences, drill down to "Text Editors" and check the box next to "Show line numbers".
Step 2: Make the Workspace build, refresh, and save automatically. Refreshing forces the folders and files shown in the Package Manager View to be updated when anything changes in those directory behind the scenes. Drill down to "Workspace" and check the boxes.
Step 3: Automate certain tasks whenever you save. Drill down to "Save Actions" and make your selections. Formatting the source code automatically is one of the greatest timesavers. How it's formatted will be defined in Step 3. Under additional actions, my favorite save action is "Remove unused imports". Organizing imports is very time-saving as well. Each time you new-up a new class in a java file, just hit save and the import for that class will be automatically generated.
Step 4: Set up the Code Formatter. This properly indents your code, adds blank lines where you want them, etc., all automatically. Drill down to "Formatter" and make your selections. There are too many choices here for me to go through and I actually use settings very close to the default. The important thing is that the Formatter was activated in step 3. Your changes to the formatter can be saved and shared between developers on a team so that all code is formatted consistently. Use the export button to export an XML file.
Step 5: Setup code templates. For example, when a new class is created, you can have it add certain comments formatted in the way you like. Drill down to "Code Templates" and make your selections.
Step 6: Setup custom editor templates. For example, I often need a log4j logging member in a class, and instead of typing it all from hand or copying and pasting from another class, an editor template can be created to speed up the process. Drill down to "Java" --> "Editor" --> "Templates" and import the following XML pasted into an XML file.
Step 7: See all your changes in effect. Create a test class, add some fields with different indentations, new up an ArrayList, and save. You should see line numbers. You should see the fields automatically align and the import for the ArrayList be added to the imports. Add a method with a parameter that returns a String, type /** above the method definition, and hit enter. You should see a JavaDoc comment automatically be created according to how you defined it in the code templates.
Piece of Cake!!
Step 1: Make the text editor show line numbers. This helps immensely when reading stack traces and locating errors in your code. Go to the Eclipse Preferences, drill down to "Text Editors" and check the box next to "Show line numbers".
Step 2: Make the Workspace build, refresh, and save automatically. Refreshing forces the folders and files shown in the Package Manager View to be updated when anything changes in those directory behind the scenes. Drill down to "Workspace" and check the boxes.
Step 3: Automate certain tasks whenever you save. Drill down to "Save Actions" and make your selections. Formatting the source code automatically is one of the greatest timesavers. How it's formatted will be defined in Step 3. Under additional actions, my favorite save action is "Remove unused imports". Organizing imports is very time-saving as well. Each time you new-up a new class in a java file, just hit save and the import for that class will be automatically generated.
Step 4: Set up the Code Formatter. This properly indents your code, adds blank lines where you want them, etc., all automatically. Drill down to "Formatter" and make your selections. There are too many choices here for me to go through and I actually use settings very close to the default. The important thing is that the Formatter was activated in step 3. Your changes to the formatter can be saved and shared between developers on a team so that all code is formatted consistently. Use the export button to export an XML file.
Step 5: Setup code templates. For example, when a new class is created, you can have it add certain comments formatted in the way you like. Drill down to "Code Templates" and make your selections.
Step 6: Setup custom editor templates. For example, I often need a log4j logging member in a class, and instead of typing it all from hand or copying and pasting from another class, an editor template can be created to speed up the process. Drill down to "Java" --> "Editor" --> "Templates" and import the following XML pasted into an XML file.
${:import(org.apache.log4j.Logger)}private static final Logger logger = Logger.getLogger(${enclosing_type}.class);
Step 7: See all your changes in effect. Create a test class, add some fields with different indentations, new up an ArrayList, and save. You should see line numbers. You should see the fields automatically align and the import for the ArrayList be added to the imports. Add a method with a parameter that returns a String, type /** above the method definition, and hit enter. You should see a JavaDoc comment automatically be created according to how you defined it in the code templates.
Piece of Cake!!
Installing Eclipse IDE for Java EE Developers on Mac OS X
The Eclipse IDE for Java EE Developers is by far the most popular download in the Eclipse IDE family. Developers use it mainly for developing Java Web Applications, which are made up of primarily JSP, XML, HTML files and Java Servlets. Installing the Eclipse IDE for Java EE Developers can be done in a few simple steps.
Step 1: Go to www.eclipse.org and download the proper version of Eclipse for your operating system. I happen to be developing on a 64-bit Mac running Mac OS X Leopard, so I downloaded the Mac Cocoa 64-bit version.
Step 2: Unzip the downloaded *.tar.gz file (double-click it) and move the "eclipse" folder to the root of your hard drive. Eclipse is installed!
Step 3: In the /eclipse directory, you'll find a Eclipse.app file. Double clicking it will start Eclipse. I prefer to drag this file down to the Dock for easy access.
Step 4: Once Eclipse is started, you'll be greeted by the Eclipse welcome screen.
Step 5: Click on the Workbench icon in the upper right corner to get to the workbench. You'll be asked to choose a workspace. This is where all your Java EE projects will reside.
Piece of Cake!!
Step 1: Go to www.eclipse.org and download the proper version of Eclipse for your operating system. I happen to be developing on a 64-bit Mac running Mac OS X Leopard, so I downloaded the Mac Cocoa 64-bit version.
Step 2: Unzip the downloaded *.tar.gz file (double-click it) and move the "eclipse" folder to the root of your hard drive. Eclipse is installed!
Step 3: In the /eclipse directory, you'll find a Eclipse.app file. Double clicking it will start Eclipse. I prefer to drag this file down to the Dock for easy access.
Step 4: Once Eclipse is started, you'll be greeted by the Eclipse welcome screen.
Step 5: Click on the Workbench icon in the upper right corner to get to the workbench. You'll be asked to choose a workspace. This is where all your Java EE projects will reside.
Piece of Cake!!
Unpack a Java .war file on Mac OX S
Unpacking Java .war file on a Mac is not hard at all. For this demonstration, I'll use the sample.war file that comes with the Tomcat Installation.
Step 1: Fire up the terminal
Step 2: cd to the directory where the .war resides. In this case I moved it from the Tomcat directories to /sample beforehand.
Step 3: Unpack the .war with the following command:
The contents of the .war are unpacked for you and placed in the same folder as the .war file.
Piece of Cake!!
Step 1: Fire up the terminal
Step 2: cd to the directory where the .war resides. In this case I moved it from the Tomcat directories to /sample beforehand.
# cd /sample
Step 3: Unpack the .war with the following command:
# jar -xvf sample.war
The contents of the .war are unpacked for you and placed in the same folder as the .war file.
Piece of Cake!!
Sunday, November 8, 2009
Installing and Running Tomcat on Mac OS X
Using MySQL and Java? Check out an easier way: Yank
Installing Tomcat on Mac OS X should be easy, and it is, if you follow these instructions exactly.
Step 1: Get Tomcat. Go to tomcat.apache.org and download the version of Tomcat you need. Download the "Core" distribution with the tar.gz extension and unzip it (double-click the file).
Step 2: Install Tomcat. Move the contents of the unzipped file to /Library/Tomcat, creating the folder "Tomcat" in "Library" if it is not there already. That's it. Tomcat is installed.
Step 3: Fire up the terminal.
Step 4: Start Tomcat.
# sh /Library/Tomcat/bin/startup.sh
Step 5: Test if it worked. Use your browser to go to: http://127.0.0.1:8080/ You should see the default Tomcat homepage running from your newly installed Tomcat server.
Step 6: Shutdown Tomcat:
# sh /Library/Tomcat/bin/shutdown.sh
Optional steps to deploy a sample .war file!
Step 7: Point your browser to: http://127.0.0.1:8080/docs/appdev/sample/
Step 8: Download the sample .war file found on the above page and place it in /Library/Tomcat/webapps. Tomcat will recognize and load the .war file.
Step 9: Point your browser to: http://localhost:8080/sample/ to view the sample webapp.
By the way, Tomcat can be found in the Activity Monitor with the name "java". If Tomcat ever hangs and you need to kill the process, just use Activity Monitor to do so.
Piece of Cake!!
See also: MacBook Hard Drive Upgrade Instructions
And also: Install MySQL on Mac OS X
Sunday, October 25, 2009
Rock Climbing in Bielatal near Dresden, Germany
Rock climbing in Bielatal is like warping back in time and landing in the days when climbers climbed barefoot, wore Swami belts, and used slings for protecting routes - literally.
The climbing in the Dresden, Germany area has not followed the flashy modern ways of most other climbing venues. No chalk, no bolts, no top-roping, no nuts, no cams. While modern climbing harnesses and shoes are now common sights in Bielatal, you might be chased out of the woods if you're seen using the aformentioned items. Part tradition and part protecting the soft sandstone, the modern metal climbing gear found hanging on people's harnesses in every crag in America is not allowed in Beilatal. Watching the locals climbing with just a rack of slings and biners is a humbling experience.
The climbing in the Dresden, Germany area has not followed the flashy modern ways of most other climbing venues. No chalk, no bolts, no top-roping, no nuts, no cams. While modern climbing harnesses and shoes are now common sights in Bielatal, you might be chased out of the woods if you're seen using the aformentioned items. Part tradition and part protecting the soft sandstone, the modern metal climbing gear found hanging on people's harnesses in every crag in America is not allowed in Beilatal. Watching the locals climbing with just a rack of slings and biners is a humbling experience.
An old-school biner from the DDR!
Heike demonstrates the "fling" technique. Tie the perfect size knot one-handed on lead, and fling it in the crack above your head until it catches.
At the top, there is usually a single monster anchor to secure yourself into and to rappel from. Tradition calls for everyone to shake each others hands and say "Berg Heil!" (salute the mountain!).
Each tower has its own register book in specially created metal boxes on the top, which you must sign. It's very important to document who lead the route and who top-roped. And most importantly, use the supplied straight edge to draw a horizontal line under your party's entry.
Kicking back on the top. Berg Heil!
See also: Diedre 5.7, Squamish - Climbing Beta
and: Ten German Birds
Kicking back on the top. Berg Heil!
See also: Diedre 5.7, Squamish - Climbing Beta
and: Ten German Birds
Subscribe to:
Posts (Atom)