Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Setup development environment with Eclipse and Tomcat

In this tutorial, we will guide you through step by step instructions how to setup and create development environment for dynamic web application using eclipse and tomcat. Unlike with static Web projects, dynamic Web projects enable you to create resources such as JavaServer Pages and servlets. Dynamic web projects can contain dynamic Java EE resources such as servlets, JSP files, filters, and associated metadata, in addition to static resources such as images and HTML files. Static web projects only contains static resources.

Setup development environment for web application using/with eclipse, maven and tomcat


This tutorial covers step by step guide to set up web application using eclipse WTP (Web Tool Platform), maven and tomcat. This is tested with followings.
  1. Java SE 6 Update 25
  2. Eclipse IDE for Java EE Developers and hence Web Tool Platform 3.0
  3. Maven 3.0.3
  4. Tomcat v 6.0.26
Please follow the below steps.
1.  Download Java and Install.

2.  Download Eclipse Helios IDE which has in built support for WTP.

3.  Download Maven 3.0.3 and follow the Installation Instructions section on the same page.

4.  Download the distribution archive for Tomcat 6.0 and unzip it to the directory you wish to install Tomcat 6.0 These instructions assume you chose C:\Program Files\Tomcat.

5. Open Eclipse and select the workspace (e.g. C:\Development). Go to Help -> Install New Software and install Maven plugin for Eclipse. Provide http://m2eclipse.sonatype.org/sites/m2e in the Location.

6. Click Windows -> Preferences -> Java -> Installed JREs -> Add -> Standard VM -> Directory. Select the directory where Java was installed in step 1 above.

7. Click File -> New -> Other -> Maven -> Maven Project.




8. Click Next and select an archetype as shown below.





9. Click Next and select archetype parameters as below and click Finish.



If following error or exception is encountered, then cause is that there is settings.xml under $USER_HOME/.m2 and that points to url which does not have maven-archetype-webapp archetype.


Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories. Could not resolve artifact Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\vkarmani.OPERATIVE\.m2\repository) Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\vkarmani.OPERATIVE\.m2\repository)


To solve the above problem, remove settings.xml temporarily and allow $MAVEN_HOME/conf/settings.xml to fetch maven-archetype-webapp archetype from default url when Finish button is clicked. After you are done with this, put your settings.xml back under $USER_HOME/.m2


10. Configure pom.xml as below.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany</groupId>
 <artifactId>mywebapp</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>mywebapp Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <build>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-eclipse-plugin</artifactId>
     <version>2.8</version>
     <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>false</downloadJavadocs>
      <wtpversion>2.0</wtpversion>
      <wtpmanifest>true</wtpmanifest>
      <additionalBuildcommands>
       <buildCommand>
        <name>org.springframework.ide.eclipse.core.springbuilder</name>
       </buildCommand>
      </additionalBuildcommands>
      <additionalProjectnatures>
       <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
      </additionalProjectnatures>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
      <compilerVersion>1.6</compilerVersion>
      <source>1.6</source>
      <target>1.6</target>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-enforcer-plugin</artifactId>
     <version>1.0</version>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
     <execution>
      <id>enforce-versions</id>
      <goals>
       <goal>enforce</goal>
      </goals>
      <configuration>
       <rules>
        <requireMavenVersion>
         <version>[3.0,)</version>
        </requireMavenVersion>
        <requireJavaVersion>
         <version>[1.6,)</version>
        </requireJavaVersion>
       </rules>
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

</project>
      
            
                



11. Open command prompt and go to project folder. Run the following commands.
  • mvn clean
  • mvn install
  • mvn eclipse:eclipse
12. Click Window -> Show View -> Servers.

13. Right Click in the empty area of Servers View. Click New -> Server. Select Tomcate v6.0 server and click Next. Select mywebapp under Available section and click Add >. Click Finish.
If following error is encountered then open org.eclipse.wst.common.project.facet.core.xml file under mywebapp/.settings folder and change the version from 3.0 to 2.5 for jst.web facet because Tomcat 6.0 does not support WTP 3.0. i.e.
Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

14. Make sure 'Build Automatically' option is checked under 'Project' menu in Eclipse. Double click on server. Under 'Publishing' section, check the option 'Automatically publish after a build event'.

15. Right click on Tomcat server and click start.If following warning is observed during server start up, then stop the server and double click on the server. Check 'Publish module contexts to separate XML files' under Server Options.
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mywebapp' did not find a matching property.

16. Open http://localhost:8080/mywebapp and you will see 'Hello World!'.

17. For configuration changes in files and major changes to get reflected , always stop the server, right click on server and click on 'Clean' option and restart the server.

Creating Multiple Tomcat Instances


This tutorial explains how to create multiple tomcat instances

What is 'Multiple instances' and why do I need it

Multiple tomcat instaces means, there are more than one tomcat servers running on its own process. Each instance runs on it's own java virtual machine, with it's own configuration. Each instance can have different memory settings, Each instance can be started and shutdown independent of other instances.

How to create multiple tomcat instances

  1. Download latest tomcat (tomcat 7 at the time of writing this tutorial) and extract it wherever you want. We will call it CATALINA_HOME.
  2. Create a new directory at any location and rename it to instance1. This would be our instance 1
  3. Create one another directory for instance2
  4. Copy conf,logs,temp,webapps and work directories from CATALINA_HOME to both of the instance directories that we created above.
  5. Modify the conf/server.xml files in each of the instances and change the ports so that all three ports are different on both of the instances. See this tutorial on How to change tomcat default ports
That's it, you just setup two seperate tomcat instances. Now you have to create a startup script for both of the instances.

startup script for instance 1

set CATALINA_HOME=<the directory where you have extracted tomcat>
set CATALINA_BASE=<path to your instance 1 directory>
start %CATALINA_HOME%\bin\catalina.bat start
Do the same for instance 2. Now you can use this scripts to start your tomcat instances. You can have both of the instances running at the same time too.

Changing default HTTP Port 80 in Tomcat


The default HTTP port is 8080 and it is configured in the HTTP connector. Open the TOMCAT-HOME/conf/server.xml file and search for the following.
 
       <connector port="8080" protocol="HTTP/1.1" 
                  connectiontimeout="20000" 
                  redirectport="8443"/>  
Now let's say you want to change the port to 80, which is the standard HTTP port. You can do this by changing the value of port attribute as show below.
 
       <connector port="80" protocol="HTTP/1.1" 
                  connectiontimeout="20000" 
                  redirectport="8443"/>  

java.net.BindException: Address already in use:8080

Some time you may encounter this error when trying to start tomcat server. This is because the port 8080 is already being used by some other application. The solution is to change the tomcat port from 8080 to some thing else as explained above.