Posts Tagged ‘tomcat’

Memo: log4j in tomcat (备忘:Tomcat中配置log4j)

March 9th, 2015

In a tomcat project, we can configure log4j to write logs in a file with:
This will put all the log files in the /log dir under tomcat root dir.

Here is a full version of the sample

### direct log messages to stdout ###
# log message format: 10:48:40,399 INFO Test:36 - [3, 2, 1]
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct log messages to file ###
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
# log message format: INFO main wp.newsbag.Test - message ...
#log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.rollingFile.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c:%L - %m%n
#log4j.rootLogger=debug, stdout
log4j.rootLogger=debug, rollingFile

Location of tomcat work dir in eclipse (Eclipse中tomcat工作目录)

March 9th, 2015

The default dir for eclipse to publish we apps in tomcat is WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmpX, where X could be 0, 1, 2, depending on how servers you have configured. Of course, the dir can be changed as shown in the Server project, as shown in the pic below:

Configure work dir for tomcat in Eclipse


备忘:Eclipse中tomcat的默认工作目录为:WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmpX,X可能为0,1,2….。当然,这个目录可以在Server Project中修改,如上图所示。

Memo: tomcat configurations

December 7th, 2013

Here are some common used configurations of tomcat.

  • Add a new tomcat host. In server.xml, add these lines:
    		<Host name="" appBase="/var/www/domain_webapps_dir"
                    unpackWARs="true" autoDeploy="false">
                    <Context path="" docBase="/var/www/domain_webapps_dir/proect_name" reloadable="true" />
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                            prefix="xxx_access_log." suffix=".txt"
                            pattern="%h %l %u %t "%r" %s %b" />

Ant build script for java web project with tomcat on ubuntu

December 7th, 2013

Here is a sample ant build script for java web project with tomcat on ubuntu. It should be put under the root directory of the web project.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectName" default="war" basedir=".">
	<property environment="env" />
	<property name="" value="ProjectName" />
	<property name="webapp.dir" value="/data/public/JavaWebapps" />
	<property name="tomcat.lib" value="/usr/share/tomcat7/lib" />
	<property name="ant.lib" value="/usr/share/ant/lib" />
	<property name="dist.dir" value="${basedir}/war" />
	<property name="webRoot.dir" value="${basedir}/WebContent" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<!-- Init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		<fileset dir="${tomcat.lib}">
			<include name="*.jar" />
		<!-- ant lib  -->
		<fileset dir="${ant.lib}">
			<include name="*.jar" />

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	<target name="clear" description="Clear the project">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${}/${}.war" />
		<delete dir="${}/${}" />

	<target name="init" depends="clear" description="Init the directories">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />

	<target name="compile" depends="init" description="Compile source files">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			target="1.7" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
		<echo message="end compile..." />

	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 

	<target name="war" depends="compile" description="Package the project as .war file.">
		<echo message="begin packing war..." />
		<war destfile="${dist.dir}/${}.war" basedir="${webRoot.dir}" 
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
		<echo message="end war..." />

	<target name="deploy" depends="war" description="Deploy the war to tomcat">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${}.war" todir="${webapp.dir}" />
		<echo message="end deploy..." />


Configure Tomcat 7 & Eclipse on Ubuntu

June 28th, 2013

Install Eclispe

Either via sudo apt-get install eclipse and then install Web Tools Platform for JavaEE, or download Eclipse for JavaEE.

Install tomcat

We can also install tomcat in 2 ways:
1. sudo apt-get tomcat7
2. download tomcat7 and extract it into folder, such as ~/Prog/tomcat

Integrate Eclipse and Tomcat

When I run the project with “Run On Server” and follow the instruction of “Define a new server” with tomcat v7, I always get this error message: “Cannot create a server using the selected type”. Well, here is the solution:

And then somehow I encounter this error: Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete., and tells that it’s because eclipse failed to copy the conf files into eclipse tomcat conf. so:
1. first sudo chmod -R 777 ~/Prog/tomcat7/conf;
2. and delete the server in Eclipse Perferences -> Server -> Runtime Environment, re-add it;
3. and then re-define the server at “Run on Server”.

Install Lucene Solar with Tomcat on Windows

May 7th, 2013

Installing the example solar web application is actually super easy. Here we suppose you already installed the Tomcat sevlet container on your computer TOMCAT_HOME (for example: D:\prog\apache-tomcat-7.0.35). If not, you can download it here and install it (at TOMCAT_HOME).

The next step is to install Solar:

  1. download Solr at:
  2. extract the solar package into, for example, solr-4.3.0
  3. copy the sample “Solr Home” directory solr-4.3.0\example\solr\ (Note that it’s the folder ‘example\solr\’ itself, not the content under it!!!) into the home directory of tomcat: TOMCAT_HOME if you start tomcat with Tomcat Monitor or into TOMCAT_HOME\bin if you start tomcat with TOMCAT_HOME\bin\startup.bat. Note that the solr hoe directory must be put in the right directory. This is very important!!! Otherwise you will get such an exception: “HTTP Status 500 – {msg=SolrCore ‘collection1′ is not available due to init failure: Could not load config for solrconfig.xml …… ”
    In a word, the solr home directory should be put in the Java current working directory. You can also “Configure the servlet container such that a JNDI lookup of “java:comp/env/solr/home” by the Solr webapp will point to the Solr home”.
  4. copy the solr war file (solr-N.N.N.war) under solr-4.3.0\dist\ as solr.war into the tomcat webapps directory TOMCAT_HOME/webapps. Tomcat will automatically deploy it.
  5. After solr.war is extracted, copy the SLF4j logging jars from solr-4.3.0/example/lib/ext into TOMCAT_HOME/lib or TOMCAT_HOME/webapps/solr/WEB-INF/lib. If this step is ignored, this exception will be thrown out: org.apache.catalina.core.StandardContext filterStart
    SEVERE: Exception starting filter SolrRequestFilter
    org.apache.solr.common.SolrException: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see:

OK. That’s it! Now locate your web browser to http://localhost:8080/solr (Change the port if necessary) and you will see the admin page of solr.

If there are exceptions, check your tomcat logs (such as localhost.YYYY-MM-DD.log) under TOMCAT_HOME\logs\.

PS. actually to install solar on windows is not so much different from that on any other OS. :)

More details can be found on the Solr Wiki: