自定义JSP标签Taglib

1、AuthorTag.java

package com.ats.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class AuthorTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	private String version="v1.0";

	@Override
	public int doStartTag(){
		JspWriter out=pageContext.getOut();
		try {
			out.print("<div>AuthorTag "+version+" by NEOHOPE</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag(){
		return EVAL_PAGE;
	}
	
	public String getVersion()
	{
		return version;
	}
	
	public void setVersion(String version)
	{
		this.version=version;
	}
}

2、IterateTag.java

package com.ats.taglib;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class IterateTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;

	private Iterator it;
	private String type;
	private String name;
	public void setCollection(Collection collection) {
		if (collection.size() > 0)
			it = collection.iterator();
	}
	
	public void setType(String type)
	{
		this.type=type;
	}
	
	public void setName(String name)
	{
		this.name=name;
	}

	@Override
	public int doStartTag() {
		
		JspWriter out=pageContext.getOut();
		try {
			out.print("<table>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (it == null)
			return SKIP_BODY;
		else
			return continueNext(it);
	}

	@Override
	public int doAfterBody() {
		return continueNext(it);
	}

	@Override
	public int doEndTag() {
		try {
			if (bodyContent != null)
				bodyContent.writeOut(bodyContent.getEnclosingWriter());
		} catch (Exception e) {
			System.out.println(e);
		}
		
		JspWriter out=pageContext.getOut();
		try {
			out.print("</table>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return EVAL_PAGE;
	}

	protected int continueNext(Iterator it) {
		if (it.hasNext()) {
			pageContext.setAttribute(name, "<tr><td>"+it.next()+"</td></tr>", pageContext.PAGE_SCOPE);
			return EVAL_BODY_TAG;
		} else {
			return SKIP_BODY;
		}
	}
}

3、IterateTagTEI.java

package com.ats.taglib;

import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;

public class IterateTagTEI extends TagExtraInfo {
	public IterateTagTEI() {
		super();
	}

	@Override
	public VariableInfo[] getVariableInfo(TagData data) {
		return new VariableInfo[] { new VariableInfo(
				data.getAttributeString("name"),
				data.getAttributeString("type"), true, VariableInfo.NESTED) };
	}
}

4、LoopTag.java

package com.ats.taglib;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class LoopTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;

	private int total = 0;
	private int count = 0;

	@Override
	public int doStartTag() {
		try {
			if (total > 0)
				return EVAL_BODY_TAG;
			else
				return SKIP_BODY;
		} catch (Exception e) {
			e.printStackTrace();
			return SKIP_BODY;
		}
	}

	@Override
	public int doAfterBody() {
		try {
			if (total - count>1) {
				count++;
				return EVAL_BODY_TAG;
			} 
			else
			{
				count=0;
				return SKIP_BODY;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return SKIP_BODY;
		}
	}

	@Override
	public int doEndTag() {
		try {
			bodyContent.writeOut(bodyContent.getEnclosingWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	public void setTotal(int total)
	{
		this.total = total;
	}
	
	public int getTotal()
	{
		return total;
	}
}

5、Taglib.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"&#93;
<taglib>
	<tlibversion>1.0</tlibversion>
	<jspversion>1.1</jspversion>
	<shortname>at</shortname>
	<tag>
		<name>AuthorTag</name>
		<tagclass>com.ats.taglib.AuthorTag</tagclass>
		<bodycontent>empty</bodycontent>

		<attribute>
			<name>version</name>
			<required>false</required>
		</attribute>
	</tag>
	<tag>
		<name>LoopTag</name>
		<tagclass>com.ats.taglib.LoopTag</tagclass>
		<bodycontent>jsp</bodycontent>
		<attribute>
			<name>total</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>IterateTag</name>
		<tagclass>com.ats.taglib.IterateTag</tagclass>
		<teiclass>com.ats.taglib.IterateTagTEI</teiclass>
		<bodycontent>jsp</bodycontent>
		<attribute>
			<name>collection</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>name</name>
			<required>true</required>
		</attribute>
		<attribute>
			<name>type</name>
			<required>true</required>
		</attribute>
	</tag>
</taglib>

6、web.xml

<?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"&#93;
  <display-name>TaglibTest</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <jsp-config>
    <taglib>
      <taglib-uri>
        http://www.ats.com/Taglib
      </taglib-uri>
      <taglib-location>
        /WEB-INF/tags/Taglib.tld
      </taglib-location>
	</taglib>
  </jsp-config>
</web-app>

7、index.jsp

<%@ taglib prefix="ats" uri="http://www.ats.com/Taglib" %>

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.Date"%> 
<%@ page import="java.util.ArrayList"%> 

<!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=utf-8">
<title>TaglibTest</title>
</head>

<body>
<ats:AuthorTag version="v1.01"/>
<ats:LoopTag total="3">
<div><%=new Date()%></div>
</ats:LoopTag>

<%
ArrayList<String> t=new ArrayList<String>();
t.add("A");
t.add("B");
t.add("C");
%>

<div>
<ats:IterateTag name="row" type="String" collection="<%=t%>">
<%=row%>
</ats:IterateTag>
</div>

</body>
</html>

Tomcat7安装为Windows Service

Tomcat7采用服务模式运行,主要靠两个EXE和一个BAT文件:
Tomcat7w.exe用于配置、监控服务
Tomcat7.exe用于服务的安装、卸载、更新、运行、停止等
service.bat提供了一些预设的脚本方便大家安装卸载服务

1、Tomcat7w.exe //XX//ServiceName

#服务配置界面
Tomcat7w //ES//ServiceName
#服务监控
Tomcat7w //MS//ServiceName

2、Tomcat7.exe //XX//ServiceName

#命令行运行服务
Tomcat7 //TS//ServiceName
#启动服务
Tomcat7 //RS//ServiceName
#关闭服务
Tomcat7 //SS//ServiceName
#更新服务参数
Tomcat7 //US//ServiceName
#安装服务
Tomcat7 //IS//ServiceName
#删除服务
Tomcat7 //DS//ServiceName

其他可用参数有:

ParameterName Default Description
–Description Service name description (maximum 1024 characters)
–DisplayName ServiceName Service display name
–Install procrun.exe //RS//ServiceName Install image
–Startup manual Service startup mode can be either auto or manual
++DependsOn List of services that this service depend on. Dependent services
are separated using either # or ; characters
++Environment List of environment variables that will be provided to the service
in the form key=value. They are separated using either
# or ; characters. If you need to use either the #
or ; character within a value then the entire value must be
enclosed inside single quotes.
–User User account used for running executable. It is used only for
StartMode java or exe and enables running applications
as service under account without LogonAsService privilege.
–Password Password for user account set by –User parameter
–JavaHome JAVA_HOME Set a different JAVA_HOME than defined by JAVA_HOME environment
variable
–Jvm auto Use either auto (i.e. find the JVM from the Windows registry)
or specify the full path to the jvm.dll.
You can use the environment variable expansion here.
++JvmOptions -Xrs List of options in the form of -D or -X that will be
passed to the JVM. The options are separated using either
# or ; characters. (Not used in exe mode.)
–Classpath Set the Java classpath. (Not used in exe mode.)
–JvmMs Initial memory pool size in MB. (Not used in exe mode.)
–JvmMx Maximum memory pool size in MB. (Not used in exe mode.)
–JvmSs Thread stack size in KB. (Not used in exe mode.)
–StartMode One of jvm, Java or exe. The modes are:

  • jvm – start Java in-process. Depends on jvm.dll, see –Jvm.
  • Java – same as exe, but automatically uses the default Java
    executable, i.e. %JAVA_HOME%\bin\java.exe. Make sure JAVA_HOME is set
    correctly, or use –JavaHome to provide the correct location.
    If neither is set, procrun will try to find the default JDK (not JRE)
    from the Windows registry.
  • exe – run the image as a separate process
–StartImage Executable that will be run. Only applies to exe mode.
–StartPath Working path for the start image executable.
–StartClass Main Class that contains the startup method. Applies to the jvm and
Java modes. (Not used in exe mode.)
–StartMethod main Method name if differs then main
++StartParams List of parameters that will be passed to either StartImage or
StartClass. Parameters are separated using either # or
; character.
–StopMode One of jvm, Java or exe. See –StartMode
for further details.
–StopImage Executable that will be run on Stop service signal. Only applies to
exe mode.
–StopPath Working path for the stop image executable. Does not apply to jvm
mode.
–StopClass Main Class that will be used on Stop service signal. Applies to the
jvm and Java modes.
–StopMethod main Method name if differs then main
++StopParams List of parameters that will be passed to either StopImage or
StopClass. Parameters are separated using either # or
; character.
–StopTimeout No Timeout Defines the timeout in seconds that procrun waits for service to
exit gracefully.
–LogPath %SystemRoot%\System32\LogFiles\Apache Defines the path for logging. Creates the directory if necessary.
–LogPrefix commons-daemon Defines the service log filename prefix. The log file is created in the
LogPath directory with .YEAR-MONTH-DAY.log suffix
–LogLevel Info Defines the logging level and can be either Error,
Info, Warn or Debug. (Case insensitive).
–StdOutput Redirected stdout filename.
If named auto then file is created inside LogPath with the
name service-stdout.YEAR-MONTH-DAY.log.
–StdError Redirected stderr filename.
If named auto then file is created inside LogPath with the
name service-stderr.YEAR-MONTH-DAY.log.
–PidFile Defines the file name for storing the running process id. Actual file is
created in the LogPath directory

3、service.bat安装卸载服务

service.bat install/uninstall/remove ServiceName

4、启动关闭服务

net start ServiceName
net stop ServiceName

CMD常用命令12切换网络

1、切换为DHCP

@netsh interface ip set address name="本地连接" source=dhcp

@netsh interface ip set dns name="本地连接" source=dhcp

2、切换为静态IP

@netsh interface ip set address name="本地连接" source=static addr=xxx.xxx.xxx.xxx mask=255.255.255.0 gateway=xxx.xxx.xxx.xxx

@netsh interface ip set dns name="本地连接" source=static addr=xxx.xxx.xxx.xxx

MuleESB3发布到Tomcat

今天把Mule ESB集成到Tomcat时,出现了下面的错误:

Already in lifecycle phase 'start', cannot fire the same phase twice

解决方法:
把MuleESB官方网站建议的:

<listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>

替换为:

<listener-class>org.mule.config.builders.DeployableMuleXmlContextListener</listener-class>

就可以了

我的设计模式笔记

设计模式
1、设计模式原则
开放封闭原则:一个类,对修改封闭,对扩展开放
依赖反转原则:依赖于抽象而不是具体实现,实现依赖接口,接口不依赖于实现
迪米特法则:尽量减少与其他类的关系,降低类之间耦合度
里氏转换原则:子类必须能替代父类
接口隔离原则:类的依赖建立在最小接口之上,多个小接口优于一个大接口
合成复用原则:聚合优先于继承

2、简单工厂
将创建实例的代码,集中到一个类中,实现一个类创建多种实例。

3、工厂模式
将创建实例的代码,在工厂子类中实现,调用者负责使用哪个工厂。

4、抽象工厂
将工厂抽象为一系列的接口,在每个工厂子类中,用工厂方法实现这些接口。

5、模板方法
创建对象的步骤固定,每个步骤都放到子类中实现。

6、生成器
创建对象的步骤不固定,但组件固定,可以方便的增加各种组件。

7、策略模式
多种算法,在使用时,指定选用的算法。

8、状态模式
多种算法,根据对象的状态,选用对应的算法。

9、装饰者
包装现有对象,增加功能。

10、外观
将一系列复杂接口,重新组成一个简单的接口。

11、迭代器
不暴露细节的前提下,提供遍历
Java、DotNet
12、组合
对象和组,采用相同的方式来实现
菜单
13、代理
提供代理类,间接访问对象,提供访问控制,调用者不知道被代理类的存在
远程代理,虚拟代理
14、桥接
采用桥接类,利用组合访问的方式,处理桥接对象之间的访问

15、适配器
封装原有接口,成为新接口

16、观察者
对象状态改变,通知其他对象

17、访问者
不改变原来实现的前提下,通过访问者,提供新功能(破坏封装)

18、中介者
多个对象之间的访问,改变为对象与中介者之间的访问

19、命令
将请求封装为对象,可以回滚

20、备忘录
记录状态,随时回滚

21、单件
最多只有一个实例

22、蝇量(享元)
同一类的大量实例,共享存储

23、原型
复制复杂对象,而并非重新创建对象

24、解释器
嵌入式语言

25、责任链
消息响应链

26、组合模式
MVC等

好书推荐:
《Head First设计模式》、《大话设计模式》、《23种设计模式》

Eclipse开发插件

ADT:
https://dl-ssl.google.com/android/eclipse/

DLTK:
http://download.eclipse.org/technology/dltk/updates/

PyDev:
http://pydev.org/updates

EPIC:
http://e-p-i-c.sf.net/updates

PDT:
http://download.eclipse.org/tools/pdt/updates/2.0
http://download.eclipse.org/tools/pdt/updates/3.0/milestones/

指定JDK

eclipse.exe -vm D:\JavaJDK\jdk1.7.0_06\bin\javaw.exe