/*
taglib的主要作用就是:对一些需要重复利用的代码段进行封装,并设置该代码段可能用到的属性,提高代码的利用率。 taglib主要有三个部分构成:
实现代码段的.java文件;
标签库描述文件.tld;
web.xml的配置。
下面首先介绍标签实现文件,这里以DecorTag.java实现的标签为例:
*/
//DecorTag.java
/*
* DecorTag.java
*
* Created on 2007年3月17日, 上午8:25
*/
package com.wlmzfx.servlet;
import javax.servlet.jsp.*;//jsp类
import javax.servlet.jsp.tagext.*;//标签类库
import java.io.IOException;
//这里实现一个用html表格显示一个装饰框
public class DecorTag extends TagSupport{
//这些字段用来控制外观
String align;//对齐方式
String title;//标题
String titleColor;//标题前景色
String titleAlign;//标题对齐方式
String color;//方框背景色
String borderColor;//边框颜色
String margin;//边框与内容之间的像素
String borderWidth;//边框宽度的像素值
//以下方法用来设置各种属性值,是必不可少的,在jsp页面使用标签时,属性将转化为方法调用
public void setAlign(String value){
this.align = value;
}
public void setTitle(String value){
this.title = value;
}
public void setTitleColor(String value){
this.titleColor = value;
}
public void setTitleAlign(String value){
this.titleAlign = value;
}
public void setColor(String value){
this.color = value;
}
public void setBorderColor(String value) {
this.borderColor = value;
}
public void setMargin(String value){
this.margin = value;
}
public void setBorderWidth(String value){
this.borderWidth = value;
}
public void setPageContext(PageContext context){
//以超类保存页面环境对象,下面的Tag()要用到,这很重要
super.setPageContext(context);
//设置属性的默认值
align = “cente”;
title = null;
titleColor = “White”;
titleAlign = “left”;
color = “lightblue”;
borderColor = “black”;
margin = “20″;
borderWidth = “4″;
}
/**
*此方法再遇到<decor:box>标签时调用
**/
public int doStartTag() throws JspException{
try{
//从PageContext对象获取输出流并传给setPageContext()方法
JspWriter out = pageContext.getOut();
out.print(”<div align=’”+align+”‘>”+”<table bgcolor=’”+borderColor+”‘”+”<border=’0′ cellspacing=’0′”+”cellpadding=’”+borderWidth+”‘”);
if(title != null)
out.print(”<tr><td align=’”+titleAlign+”‘>”+”<font face=’helvetica’ size=’+1′ “+”color=’”+titleColor+”‘><br>”+title+”</b></font></td><td>”);
out.print(”<tr><td<table bgcolor=’”+color+”‘”+”border=’0′ cellspacing=’0′”+”cellpadding=’”+margin+”‘><tr><td>”);
}catch(IOException e){
throw new JspException(e.getMessage());
}
//返回值告诉Jsp类处理标签主题
return EVAL_BODY_INCLUDE;
}
//在遇到</decor:box>结束标签时调用
public int doEndTag()throws JspException{
try{
JspWriter out = pageContext.getOut();
out.println(”</td></tr></table><td><tr><table></div>”);
}catch(IOException e){
throw new JspException(e.getMessage());
}
//返回值指示继续处理Jsp页面
return EVAL_PAGE;
}
}
//decor_1_0.tld(放在WEB-INF/tids/decor_1_0.tld
//一般以后缀_1_0来表示tld文件的版本,实际上这也是个xml文件
<?xml version=”1.0″ encoding=”UTF-8″?>
<taglib version=”2.0″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd”>
<tlib-version>1.0</tlib-version><!–文件版本–>
<short-name>decor</short-name><!–库名–>
<uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</uri><!–唯一标识符–>
<tag><!–首先定义了标签库的一个标签–>
<name>box</name><!–首先定义标签名,实现类–>
<tagclass>com.wlmzfx.servlet.DecorTag</tagclass>
<!–定义标签库的每一个属性–>
<attribute>
<name>align</name><!–属性–>
<required>false</required><!–不是必须–>
<rtexprvalue>true</rtexprvalue><!–应有<%= %>值–>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bordercolor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>margin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>borderWidth</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleColor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleAlign</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!– A validator verifies that the tags are used correctly at JSP
translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
–>
<!– A tag library can register Servlet Context event listeners in
case it needs to react to such events. Listener entries look
like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
–>
</taglib>
然后需要在web.xml中配置该标签,在web.xml中添加下面的内容:
<taglib>
<!–看到标签库唯一标识符时–>
<taglib-uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</taglib-uri>
<!–使用标签库描述文件的本地副本–>
<taglib-location>tlds/decor_1_0.tld</taglib-location>
</taglib>
下面以一个例子来说明该标签库的用法:
在任何jsp页面中加入下面的代码:
<!–这里仅仅定义了title,color,margin属性–>
<decor:box title=”LogOut when Done” color=”red” margin=”100″>
<!–这里添加表格的内容,这里可以继续使用<decor:box>标签,但是同样必须以</decor:box>结束–>
内容在这里
</decor:box>
|
JNDI 操作文件系统
环境: JDK1.6
Jar包准备: fscontext.jar、providerutil.jar
下载:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#7110-jndi-1.2.1-oth-JPR
JNDI 操作 File System - 小奋斗--浪子 - 小奋斗 浪子
--------------------------------------------------------------------------------------------------------------------------------------------------
操作案例代码:
package com.nice.files;
import java.util.Hashtable;
import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import com.sun.jndi.fscontext.RefFSContext;
public class FileSystemTest {
public static InitialContext createContext(String fileURL) throws NamingException{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL,fileURL);
return new InitialContext(env);
}
// 增加
public static void addDirectory(Context context,String baseName,String name)
throws Exception{
RefFSContext refCtx = (RefFSContext)context.lookup(baseName);
refCtx.createSubcontext(new CompositeName(name));
}
// 删除
public static void deleteDirectory(Context context,String baseName,String name)
throws Exception{
RefFSContext refCtx = (RefFSContext)context.lookup(baseName);
refCtx.destroySubcontext(name);
}
// 修改
public static void modifyDirectory(Context context,String baseName,String oldName,
String newName)throws Exception{
RefFSContext refCtx = (RefFSContext)context.lookup(baseName);
refCtx.rename(oldName, newName);
}
// 查找是否存在
public static boolean findDirectory(Context context,String baseName,String name)
throws NamingException{
RefFSContext refCtx = (RefFSContext)context.lookup(baseName);
try{
RefFSContext subRefCtx = (RefFSContext)refCtx.lookup(name);
if(subRefCtx!=null){
System.out.println("存在 : " + name);
return true;
}
}catch(NameNotFoundException e){ }
System.out.println("不存在 : " + name);
return false;
}
// 搜索
public static void searchFile(Context context)throws Exception{
String nameInSpace = context.getNameInNamespace();
System.out.println("nameInSpace : " + nameInSpace);
NamingEnumeration<NameClassPair> nEnum= context.list(nameInSpace);
int total = 0;
while(nEnum.hasMoreElements()){
NameClassPair pair = nEnum.nextElement();
if("javax.naming.Context".equals(pair.getClassName())){
System.out.println("目录: " + pair.getName());
}else if("java.io.File".equals(pair.getClassName())){
System.out.println("文件: " + pair.getName());
}
total ++ ;
}
System.out.println("数量: " + total);
}
public static void searchBinddingFile(Context context)throws Exception{
String nameInSpace = context.getNameInNamespace();
System.out.println("nameInSpace : " + nameInSpace);
NamingEnumeration<Binding> nEnum= context.listBindings(nameInSpace);
int total = 0;
while(nEnum.hasMoreElements()){
Binding bind = nEnum.nextElement();
if("com.sun.jndi.fscontext.RefFSContext".equals(bind.getClassName())){
System.out.println("目录: " + bind.getName());
}else if("java.io.File".equals(bind.getClassName())){
System.out.println("文件: " + bind.getName());
}
total ++ ;
}
System.out.println("数量: " + total);
}
/**
* @param args
*/
public static void main(String[] args)throws Exception {
InitialContext context = createContext("file:///c:/html/");
RefFSContext refCtx = (RefFSContext)context.lookup("/images/images");
String nameInSpace = context.getNameInNamespace();
// 增加
//addDirectory(context,nameInSpace+"/images/images/","test123");
// 修改
//modifyDirectory(context,nameInSpace+"/images/images/","test","test2");
// 删除
//deleteDirectory(context,nameInSpace+"/images/images/","test");
// 查找
findDirectory(context,nameInSpace,"test");
// 搜索
searchFile(context);
searchBinddingFile(context);
searchFile(refCtx);
searchBinddingFile(refCtx);
}
}
|
/*客户端通过键盘录入用户名。
服务端对这个用户名进行校验。
如果该用户存在,在服务端显示xxx,已登陆。
并在客户端显示 xxx,欢迎光临。
如果该用户存在,在服务端显示xxx,尝试登陆。
并在客户端显示 xxx,该用户不存在。
最多就登录三次。
*/
import java.io.*;
import java.net.*;
class LogClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.0.2",11011);
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufin =
new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int i = 0;i<3;i++)
{
String line = bufr.readLine();
if(line == null)
break;
out.println(line);
String info = bufin.readLine();
System.out.println("info:"+info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}
class UserThread implements Runnable
{
private Socket s ;
public UserThread(Socket s)
{
this.s = s;
}
public void run()
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected.");
try
{
// BufferedReader bufr = null;
for(int i = 0 ;i<3;i++)
{
BufferedReader in =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String username = in.readLine();
if(username==null)
break;
PrintWriter out =
new PrintWriter(s.getOutputStream(),true);
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
boolean flag = false;
String line = null;
while((line= bufr.readLine())!=null)
{
if(line.equals(username))
{
flag = true;
break;
}
}
if(flag)
{
System.out.println(username + "已登录。");
out.println(username + "欢迎光临!");
bufr.close();
break;
}
else
{
System.out.println(username + ",尝试登陆。");
out.println(username + ",该用户不存在。");
bufr.close();
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"校验失败!");
}
}
}
class LogServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(11011);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
|
package lixg.J2se.IO;
import java.io.*;
//运用io流拷贝文件
class CopyPic {
public static void main(String[] args) throws IOException {
long star = System.currentTimeMillis();
InputStream in = new FileInputStream("D:\\java\\workspace2eclipse\\OA_29_Lixg\\src\\lixg\\J2se\\IO\\record.gif");
OutputStream out = new FileOutputStream("D:\\java\\workspace2eclipse\\OA_29_Lixg\\src\\lixg\\J2se\\IO\\record2.gif");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1)// 等价于:read(b,0,b.length)
{
out.write(b, 0, len);
}
in.close();
out.close();
long end = System.currentTimeMillis();
System.out.println("时间:" + (end - star));
}
}
|