Java

在maven命令行中为插件指定参数

1. 找开插件源码,找到参数的expression. 例如: 引用     /**      * User properties file.      *      * @parameter expression=" ${autoconfig.userProperties}"      */     private File userProperties; 2. 执行时把这个expression作为system property加上去。 引用 mvn install -Dautoconfig.userProperties=/home/kent/myproject/antx.properties

Netty的websocket例子

看这个官方示例: http://netty.io/3.5/xref/org/jboss/netty/example/http/websocketx/server/package-summary.html 不要被类名疑惑。它既包含了服务端,也通过产生js代码、提供了客户端。 java客户端: https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/http/websocketx/client

NIO – Selector代码示例

package player.kent.chen.temp.learnnio.selector; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; public class PlaySelector { public static void main(String[] args) throws IOException, InterruptedException { ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.socket().bind(new InetSocketAddress(17547)); serverChannel.configureBlocking(false); Selector selector = Selector.open();//这个selector将同时用于serverChannel和accept()后针对单个连接的socketChannel serverChannel.register(selector, SelectionKey.OP_ACCEPT); //侦听ACCEPT就绪事件 System.out.println("开始侦听连接…"); while (true) { int n = selector.select(); //阻塞直到有事件发生 if …

NIO – Selector代码示例 Read More »

NIO – Netty代码示例

以下代码代表一个加法。客户端提交加数,服务端回送“和”。 服务端 package player.kent.chen.temp.learnnetty.raw; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.group.ChannelGroup; import org.jboss.netty.channel.group.ChannelGroupFuture; import org.jboss.netty.channel.group.DefaultChannelGroup; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** * 报文格式:request=a+b, response=c * * @author 陈坚 2013年6月18日下午2:47:04 */ public class PlayNettyAddServer …

NIO – Netty代码示例 Read More »

NIO – File Channel代码示例

package player.kent.chen.temp.learnnio; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class PlayFileChannel { public static void main(String[] args) throws IOException { playOutput(); playInput(); } private static void playOutput() throws FileNotFoundException, IOException { FileChannel fc = new FileOutputStream("/home/kent/temp/data.txt").getChannel(); ByteBuffer buff = ByteBuffer.wrap("All the data".getBytes()); fc.write(buff); fc.close(); } private static void playInput() …

NIO – File Channel代码示例 Read More »

NIO – Endian代码示例

package player.kent.chen.temp.learnnio; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class PlayEndian { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.wrap(new byte[20]); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //1, 打印0 49 0 50 0 51 … buffer.rewind(); //让buffer的位置移到最前 buffer.order(ByteOrder.BIG_ENDIAN); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //2, 打印结果将和#1一样,因为java默认的byte order就big endian buffer.rewind(); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.asCharBuffer().put("12345"); printBufferAsArray(buffer); //3,打印49 0 50 0 51 0 … } private static void …

NIO – Endian代码示例 Read More »

NIO – ServerSocketChannel代码示例

package player.kent.chen.temp.learnnio.socket; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class PlayServerSocketChannel { public static void main(String[] args) throws IOException, InterruptedException { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(new InetSocketAddress(17547)); ssc.configureBlocking(false); System.out.println("开始侦听连接…"); while (true) { SocketChannel sc = ssc.accept(); if (sc == null) {//非阻塞模式下,如果没有连接,accept()方法不会阻塞,而是直接返回null Thread.sleep(2000); } else { System.out.println("进来了一个连接。来自:" + sc.socket().getRemoteSocketAddress()); sc.write(ByteBuffer.wrap("尊姓大名: \n".getBytes())); //向客户端要求输入 …

NIO – ServerSocketChannel代码示例 Read More »

web app 的pom.xml 模板

<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>yyy.zzz</groupId> <artifactId>xxx</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>xxx Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> <build> <finalName>xxx</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.15</version> <configuration> <contextPath>/</contextPath> </configuration> </plugin> </plugins> </build> </project>

servlet + jsp常用代码片断

<!–xml中定义servlet–> <servlet> <servlet-name>login</servlet-name> <servlet-class>xxx.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping> <!–JSP中指定编码–> <%@ page contentType="text/html; charset=UTF-8" %> //servlet里放东西到request中 request.setAttribute("userName", user.getName()); //从servlet转到jsp RequestDispatcher view = request.getRequestDispatcher("/view.jsp"); view.forward(request, response); <!–JSP中显示request中的变量–> <div> Hello, <%=request.getAttribute("userName")%> </div>

maven web app目录结构对应的build.xml

maven ant插件生成的build.xml还是会依赖maven 我在maven生成的build.xml上改了改,去掉了maven依赖: <?xml version="1.0" encoding="UTF-8"?> <project name="showcase" default="war" basedir="."> <!– ====================================================================== –> <!– Build environment properties –> <!– ====================================================================== –> <property file="build.properties"/> <property name="build.finalName" value="showcase"/> <property name="build.dir" value="target"/> <property name="build.outputDir" value="${build.dir}/classes"/> <property name="build.srcDir" value="src/main/java"/> <property name="build.resourceDir" value="src/main/resources"/> <!– ====================================================================== –> <!– Defining classpaths –> <!– ====================================================================== –> <path id="build.classpath"> <fileset dir="compile-lib"> <include name="*.jar"/> </fileset> …

maven web app目录结构对应的build.xml Read More »