Posts Tagged ‘java’

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中修改,如上图所示。

Java socket connection timout

March 13th, 2014

There are 2 places to set the timeout option of a socket, at connection and at reading.

  • Socket.connect(SocketAddress endpoint, int timeout) connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs. Here the timeout setting only affects the action of connecting!
  • Socket.setSoTimeout(int timeout) enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

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..." />



July 18th, 2013




  1. 在paoding-dic-home.properties文件中配置自定义词库的位置。
    • 首先配置paoding.dic.home.config-fisrt。其有2个选项:system-env和this。
      • paoding.dic.home.config-fisrt=system-env 表示使用系统变量。如果使用这个,那个需要配置环境变量 PAODING_DIC_HOME 为字典所在目录。
      • paoding.dic.home.config-fisrt=this 表示使用本配置文件里面的配置。如果使用这个,需要在本配置文件中继续配置 paoding.dic.home。
    • 配置paoding.dic.home (如果在上面选择了paoding.dic.home.config-fisrt=this才需要),也有2种方法:使用相对路径和绝对路径
      • paoding.dic.home=classpath:dic 使用相对路径。表示使用本项目的classpath中列出的任意一个文件夹,然后把dic文件夹放置其中。一般在eclipse项目中,把dic文件夹放在源码文件夹(src)中。
      • paoding.dic.home=D:/somepath/dic 使用绝对路径。
  2. 配置好路径之后,可以在dic文件夹中新建任意以.dic为后缀的词库文件。每行一个词。还是注意:必须保存为UTF-8格式。

Simple Example of Java JSON Encoding in Servlet

May 17th, 2013

Here is a simple example showing how to output json in a servlet. I’ll use the “JSON-java” package which is recommended by and can be downloaded at To encode Java objects into a JSON string, we simply use org.json.JSONObject and

import org.json.JSONArray;
import org.json.JSONObject;

 * A simple example of showing how to output json in a Servlet.
@WebServlet(description = "Test servlet showing how to output json in Java", urlPatterns = { "/services/testjson" })
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "nocache");
		PrintWriter out = response.getWriter();

		JSONObject json = new JSONObject();

		// put some value pairs into the JSON object as into a Map.
		json.put("status", 200);
		json.put("msg", "OK");

		// put a "map" 
		JSONObject map = new JSONObject();
		map.put("key1", "val1");
		map.put("key2", "val2");
		json.put("map", map);
		// put an "array"
		JSONArray arr = new JSONArray();
		json.put("arr", arr);

		// finally output the json string		

There is another package called json-simple which is also very handy and simple. More information about json-simple can be found at

Java with UTF-8 encoding (especially with Chinese)

May 5th, 2013

Here are just some memos on how to deal with non-unicode characters (like Chinese) in java.

  • Decode characters from URL in a servlet:
    String s = request.getParameter("mykeywords");
    s = new String(s.getBytes("ISO-8859-1"), "UTF-8");

    But if the uri encoding is specified in server.xml as:
    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,
    there is no need to decode it. Simply use:
    String s = request.getParameter("mykeywords"); and s will be utf-8 encoded.

  • Send response with UTF-8:
  • Connect mysql with UTF-8:
    private static final String DB_URL = "jdbc:mysql://DB_HOST:3306/DB_SCHEMA?useUnicode=true&characterEncoding=utf8";
  • To encode JSP files with UTF-8, put this at the beginning of a JSP file:
    <%@ page contentType="text/html;charset=UTF-8" %>
  • With SpringMVC, for GET requests, set <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,.
    And for POST requests, set this as the first filter in web.xml:
  • And finally, make sure every file itself is stored as UTF-8.

java client with multi entry

August 9th, 2011

The command to execute different jar entry:
java -cp project.jar p1.AnotherEntry

setting java environment under ubuntu

May 20th, 2010

you can set all the environment vars in /etc/environment.

for example:



use bash> source /etc/environment to refresh the system variables.

Use Lucene to index a database

April 11th, 2010

According to Luncene FAQ:

How can I use Lucene to index a database?

Connect to the database using JDBC and use an SQL “SELECT” statement to query the database. Then create one Lucene Document object per row and add it to the index. You will probably want to store the ID column so you can later access the matching items. For other (text) columns it might make more sense to only index (not store) them, as the original data is still available in your database.

For a more high level approach you might want to have a look at LuSql (a specialized tool for moving data from JDBC-accessible databases into Lucene), Hibernate Search, Compass, DBSight, or Solr’s Data Import Handler which all use Lucene internally.

An example: Apache Lucene – Indexing a Database and Searching the Content