1import org.apache.lucene.analysis.*;
2import org.apache.lucene.analysis.cn.*;
3import org.apache.lucene.document.*;
4import org.apache.lucene.index.*;
5import java.sql.*;
6import oracle.jdbc.pool.*;
7
8public class Index
{
9 private String url="jdbc:oracle:thin:@//192.168.0.l:1521/Test";
10 private String user="terry";
11 private String password="dev";
12 private Connection con=null;
13 private Statement st=null;
14 private ResultSet rs=null;
15 private String indexUrl="E:\\ArticleIndex";
16
17 private ResultSet getResult() throws Exception
{
18 OracleDataSource ods=new OracleDataSource();
19
20 ods.setURL(this.url);
21 ods.setUser(this.user);
22 ods.setPassword(this.password);
23
24 this.con=ods.getConnection();
25 this.st=this.con.createStatement();
26 this.rs=this.st.executeQuery("SELECT * FROM Article");
27
28 return this.rs;
29 }
30
31 public void createIndex() throws Exception
{
32 ResultSet rs=this.getResult();
33
34 Analyzer chineseAnalyzer=new ChineseAnalyzer();
35 IndexWriter indexWriter=new IndexWriter(this.indexUrl,chineseAnalyzer,true);
36 indexWriter.setMergeFactor(100);
37 indexWriter.setMaxBufferedDocs(100);
38
39 java.util.Date startDate=new java.util.Date();
40
41 System.out.println("开始烦引时_"+startDate);
42
43 executeIndex(rs,indexWriter);
44
45 indexWriter.optimize();
46
47 indexWriter.close();
48
49 java.util.Date endDate=new java.util.Date();
50
51 System.out.println("索引l束旉Q?+endDate);
52 System.out.println("p费:"+(endDate.getTime()-startDate.getTime())+"ms");
53 }
54
55 private void executeIndex(ResultSet rs,IndexWriter indexWriter) throws Exception
{
56 int i=0;
57
58 while(rs.next())
{
59 int id=rs.getInt("ID");
60 String title=rs.getString("TITLE");
61 String info=rs.getString("CONTENT");
62
63 Document doc=new Document();
64
65 Field idField=new Field("ID",Integer.toString(id),Field.Store.YES,Field.Index.NO,Field.TermVector.NO);
66 Field titleField=new Field("Title",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
67 Field infoField=new Field("Content",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
68
69 doc.add(idField);
70 doc.add(titleField);
71 doc.add(infoField);
72
73 indexWriter.addDocument(doc);
74
75 i++;
76 }
77
78 this.close();
79
80 System.out.println("共处理记录:"+i);
81 }
82
83 private void close() throws Exception
{
84 this.rs.close();
85 this.st.close();
86 this.con.close();
87 }
88}
查找Q?
1import java.io.*;
2import org.apache.lucene.analysis.cn.*;
3import org.apache.lucene.search.*;
4import org.apache.lucene.store.*;
5import org.apache.lucene.document.*;
6import org.apache.lucene.queryParser.QueryParser;
7
8import java.util.*;
9
10public class Search
{
11
12 private static final String indexUrl="E:\\ArticleIndex";
13
14 public static void main(String[] args) throws Exception
{
15/**/ /*建立索引代码Q查找时注释*/
16 //Index index=new Index();
17
18 //index.createIndex();
19
20
21
22
23 File indexDir=new File(indexUrl);
24 FSDirectory fdir=FSDirectory.getDirectory(indexDir);
25
26 IndexSearcher searcher=new IndexSearcher(fdir);
27
28//对中文徏立解析(必须Q?br>29
QueryParser parser=new QueryParser("Title",new ChineseAnalyzer());
30 Query query=parser.parse("李湘");
31
32 Date startDate=new Date();
33 System.out.println("索开始时_"+startDate);
34
35 Hits result=searcher.search(query);
36
37 for(int i=0;i<result.length();i++)
{
38 Document doc=result.doc(i);
39
40 System.out.println("内容Q?+doc.get("Content"));
41 }
42
43 Date endDate=new Date();
44
45 System.out.println("共有记录Q?+result.length());
46 System.out.println("p费:"+(endDate.getTime()-startDate.getTime()));
47 }
48
49}
l测试,建立索引文g大概׃11分钟。一般情况下Q和用SQL执行LIKE查询差不多?
当然Q这只是我的_略试。最q一阶段Q我会对Luceneq行代码深入研究?
JDBC TM入门指南
http://www.zebcn.com/html/200411\103.html
Java E序~码规范
http://www.zebcn.com/html/200411\104.html
JavaBean入门
http://www.zebcn.com/html/200411\105.html
对JAVA语言的十个常见误?
http://www.zebcn.com/html/200411\114.html
析JAVA的XML~程
http://www.zebcn.com/html/200411\115.html
Java技巧:列表排序
http://www.zebcn.com/html/200411\129.html
Java异常处理
http://www.zebcn.com/html/200411\130.html
Java中初学者比较爱出错的运问?
http://www.zebcn.com/html/200411\131.html
cL释文档编写方?
http://www.zebcn.com/html/200411\132.html
对JAVA语言的十个常见误?
http://www.zebcn.com/html/200411\133.html
析JAVA的XML~程(to:初学者们)
http://www.zebcn.com/html/200411\134.html
关于H口的操作详?
http://www.zebcn.com/html/200411\135.html
Java 语言中的 return 语句
http://www.zebcn.com/html/200411\136.html
Javaq接各种数据库的实例
http://www.zebcn.com/html/200411\137.html
在Java中实现回调过E?
http://www.zebcn.com/html/200411\138.html
Java 中对文g的读写操作之比较
http://www.zebcn.com/html/200412\166.html
[原创]Java的文件读和写
http://www.zebcn.com/html/200412\167.html
java-在Java中读写Excel文g
http://www.zebcn.com/html/200412\171.html
实战JMS (?
http://www.zebcn.com/html/200412\172.html
取得旉的函?
http://www.zebcn.com/html/200412\177.html
JAVA-如何实现TIMER功能
http://www.zebcn.com/html/200412\179.html
数据库访问简单实?
http://www.zebcn.com/html/200412\180.html
数据库操作装到Javabean
http://www.zebcn.com/html/200412\187.html
用Java~写扫雷游戏Q-代码思想
http://www.zebcn.com/html/200412\190.html
Hibernate事务处理机制
http://www.zebcn.com/html/200412\191.html
jboss 4.0 中JSP调用EJB的简单例?
http://www.zebcn.com/html/200412\192.html
JSP WEBServer的实现原?
http://www.zebcn.com/html/200412\193.html
Spring 入门(一个简单的例子)
http://www.zebcn.com/html/200501\204.html
使用Java生成Pdf文档
http://www.zebcn.com/html/200501\205.html
JAVA生成JPG~略?
http://www.zebcn.com/html/200501\206.html
学习J2ME~程需要掌握的七种技?
http://www.zebcn.com/html/200501\210.html
W一个EJB3.0范例
http://www.zebcn.com/html/200502\213.html
Java RMI 单示?
http://www.zebcn.com/html/200502\215.html
[原创]java初学者之l验ȝ
http://www.zebcn.com/html/200502\217.html
Tomcat配置技巧Top 10
http://www.zebcn.com/html/200502\218.html
学习Java?0个基本概?
http://www.zebcn.com/html/200502\222.html
java常用的加?解密,数字{{API
http://www.zebcn.com/html/200502\227.html
一U简单的strutsU连菜单实现Ҏ
http://www.zebcn.com/html/200502\228.html
2005q??日Java新品发布公告
http://www.zebcn.com/html/200504\232.html
Javaq接各种数据库的实例
http://www.zebcn.com/html/200504\241.html
Java常见问题集锦Q来自Sun中国官方站)
http://www.zebcn.com/html/200504\243.html
java 文g操作大全
http://www.zebcn.com/html/200504\249.html
J2EE目危机【翻译?
http://www.zebcn.com/html/200505\254.html
JAVAҎ字证书的常用操作
http://www.zebcn.com/html/200510\275.html
三种整合Struts 应用E序与Spring的方?
http://www.zebcn.com/html/200511\278.html
[分n]Ant?
http://www.zebcn.com/html/200511\282.html
Struts 的动态复选框
http://www.zebcn.com/html/200511\289.html
集成 Struts、Tiles ?nbsp;JavaServer Faces
http://www.zebcn.com/html/200511\293.html
J2ME应用E序内存优化三招
http://www.zebcn.com/html/200512\338.html
一个生成无重复数字的代?
http://www.zebcn.com/html/200512\351.html
RSS 开发教E?
http://www.zebcn.com/html/200512\355.html
java写的贪吃蛇游?
http://www.zebcn.com/html/200512\360.html
应用Java技术开发WAP应用E序
http://www.zebcn.com/html/200512\361.html
web框架Jakarta Tapestry 4.0-rc-3 发布
http://www.zebcn.com/html/200512\366.html
校验获取w䆾证信息的JAVAE序
http://www.zebcn.com/html/200512\368.html
模式实践Q观察者模式与Spring
http://www.zebcn.com/html/200512\369.html
XMLd/存取属性的Java工具cd
http://www.zebcn.com/html/200512\370.html
JAVA写的四则混合q算
http://www.zebcn.com/html/200512\371.html
Web框架RIFE/Laszlo 1.3.1 发布
http://www.zebcn.com/html/200601\372.html
使用 JSF 架构q行设计
http://www.zebcn.com/html/200601\376.html
用java做的滚动的正弦曲U?
http://www.zebcn.com/html/200601\378.html
EJB3.0和Spring比较
http://www.zebcn.com/html/200601\379.html
Struts 中常见错?
http://www.zebcn.com/html/200601\380.html
漫谈Java数据库存取技?
http://www.zebcn.com/html/200601\381.html
Java多线E程序设?
http://www.zebcn.com/html/200601\385.html
JAVA中的旉操作
http://www.zebcn.com/html/200601\386.html
12个最重要的J2EE最佛_?
http://www.zebcn.com/html/200601\389.html
Java中的异步|络~程
http://www.zebcn.com/html/200601\394.html
Java I/O重定?
http://www.zebcn.com/html/200601\395.html
构徏高性能J2EE应用?0个技?
http://www.zebcn.com/html/200601\397.html
javaE序得到域名对应的所有IP地址
http://www.zebcn.com/html/200601\398.html
Java语言~程中更新XML文档的常用方?
http://www.zebcn.com/html/200601\401.html
HTTP代理如何正确处理Cookie
http://www.zebcn.com/html/200601\405.html
作者: fafile 2006-5-18 09:57 回复此发a
JavaMail 发送HTML邮g
http://www.zebcn.com/html/200602\411.html
JSP数据cd
http://www.zebcn.com/html/200602\412.html
Java5 多线E实?
http://www.zebcn.com/html/200602\415.html
JDK5.0?1个主要新特征
http://www.zebcn.com/html/200602\419.html
q用cd机制简化Struts应用E序开?
http://www.zebcn.com/html/200602\420.html
?nbsp;Struts 实现动态单选按?
http://www.zebcn.com/html/200602\421.html
JDO技术分析及企业应用研究
http://www.zebcn.com/html/200602\422.html
一个J2EE目的最工具集
http://www.zebcn.com/html/200602\425.html
一个实现MD5的简z的javac?
http://www.zebcn.com/html/200602\429.html
服务器与览器的会话
http://www.zebcn.com/html/200602\434.html
使用Spring JMS化异步消息处?
http://www.zebcn.com/html/200603\443.html
采用HttpServlet 实现web文g下蝲
http://www.zebcn.com/html/200603\444.html
Spring AOP实际应用一?
http://www.zebcn.com/html/200603\445.html
JNI中文处理问题结
http://www.zebcn.com/html/200603\446.html
java获取windowspȝ|卡mac地址
http://www.zebcn.com/html/200603\454.html
Swing vs. SWT 之调用堆栈性能比较
http://www.zebcn.com/html/200603\462.html
Using SVN with Ant
http://www.zebcn.com/html/200603\463.html
mudE序及内附的dom4j解析xml源代?
http://www.zebcn.com/html/200603\464.html
myeclipse中J2EE目之间的组l结?
http://www.zebcn.com/html/200603\465.html
初学者如何开发出高质量的J2EEpȝ
http://www.zebcn.com/html/200603\467.html
WebLogic Server 理最佛_?
http://www.zebcn.com/html/200603\474.html
WebLogic Server 性能调优
http://www.zebcn.com/html/200603\475.html
化WebLogic 8.1目的配|?
http://www.zebcn.com/html/200603\476.html
WebLogic域配|策?手动和模杉K项
http://www.zebcn.com/html/200603\477.html
JDBC中获取数据表的信?
http://www.zebcn.com/html/200603\479.html
Java 开发中遇到的ؕ码问?
http://www.zebcn.com/html/200603\484.html
易的http客户端附源代?
http://www.zebcn.com/html/200603\486.html
JDBC 4.0规范之目?
http://www.zebcn.com/html/200603\487.html
java的各U排序算?
http://www.zebcn.com/html/200603\495.html
用Java实现Web服务?
http://www.zebcn.com/html/200603\496.html
使用DBMS存储q程
http://www.zebcn.com/html/200603\497.html
Java剖析工具YourKit Java Profiler 6.0-EAP1 发布
http://www.zebcn.com/html/200603\3253.html
用Java快速开发Linux GUI应用
http://www.zebcn.com/html/200603\3260.html
java 理论与实? ?nbsp;typedef 反模?
http://www.zebcn.com/html/200604\3262.html
使用 Struts Validator (1)
http://www.zebcn.com/html/200604\3263.html
使用 Struts Validator (2)
http://www.zebcn.com/html/200604\3264.html
使用 Struts Validator (3)
http://www.zebcn.com/html/200604\3265.html
使用 Struts Validator (4)
http://www.zebcn.com/html/200604\3266.html
使用 Struts Validator (5)
http://www.zebcn.com/html/200604\3267.html
使用 Struts Validator (6)
http://www.zebcn.com/html/200604\3268.html
查询数据库后q回Iterator
http://www.zebcn.com/html/200604\3278.html
DOM属性用法速查手册
http://www.zebcn.com/html/200604\3279.html
论J2EEE序员的武功修ؓ
http://www.zebcn.com/html/200604\3280.html
让window服务q程中自动加载MYSQL服务
http://www.zebcn.com/html/200604\3287.html
J2EE应用E序异常处理框架
http://www.zebcn.com/html/200604\3289.html
Java2下Applet数字{具体实现Ҏ
http://www.zebcn.com/html/200604\3290.html
使用tomcat4.1.31和mysql 配置数据?
http://www.zebcn.com/html/200604\3291.html
用java得到本机所有的ip地址
http://www.zebcn.com/html/200604\3292.html
使用Socketq接I越CMWAP代理
http://www.zebcn.com/html/200604\3293.html
JavadExcel方式Ҏ
http://www.zebcn.com/html/200604\3294.html
<p>
<a href='#' onclick='javascript:viewnone(more1)'> d附g </a>
<div id='more1' style='display:none'>
<input type="file" name="attach1" size="50"javascript:viewnone(more2)>
</span>
</div>
<div id='more2' style='display:none'>
<input type="file" name="attach2" size="50"'>
</div>
</p>
js
<SCRIPT language="javascript">
function viewnone(e){
e.style.display=(e.style.display=="none")?"":"none";
}
</script>
方式?q种方式的动态多文g上传是实C的,很简单的Q不说废话看code
html
<input type="button" name="button" value="d附g" onclick="addInput()">
<input type="button" name="button" value="删除附g" onclick="deleteInput()">
<span id="upload"></span>
js
<script type="text/javascript">
var attachname = "attach";
var i=1;
function addInput(){
if(i>0){
var attach = attachname + i ;
if(createInput(attach))
i=i+1;
}
}
function deleteInput(){
if(i>1){
i=i-1;
if(!removeInput())
i=i+1;
}
}
function createInput(nm){
var aElement=document.createElement("input");
aElement.name=nm;
aElement.id=nm;
aElement.type="file";
aElement.size="50";
//aElement.value="thanks";
//aElement.onclick=Function("asdf()");
if(document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
}
function removeInput(nm){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null)
return false;
return true;
}
</script>
方式三:动态多文g上传Q只是在oFileInput.click();q个地方Q这样做׃能上传这个文件了Q因为发现它在上传之时就把这个input中的文g|空了。很可能是ؓ了安全着惛_Q?br>另外q有一点就是说Qclick()只有在ie中才能正常运行?br>虽说q种方式最l没能实C传,但还是留下来参考,看看是否有h可以真正实现上传?br> html
<A href="javascript:newUpload();">d附g</A>
<TABLE width="100%" border="0" cellpadding="0" cellspacing="1">
<TBODY id="fileList"></TBODY>
</TABLE><DIV id="uploadFiles" style="display:block"></DIV>
js
<SCRIPT language="javascript">
//---新徏上传
function newUpload(){
var oFileList = document.getElementById("fileList");
var fileCount = oFileList.childNodes.length + 1;
var oFileInput = newFileInput("upfile_" + fileCount);
if(selectFile(oFileInput)){
addFile(oFileInput);
}
}
//----选择文g
function selectFile(oFileInput){
var oUploadFiles = document.getElementById("uploadFiles");
oUploadFiles.appendChild(oFileInput);
oFileInput.focus();
oFileInput.click();//不能q样做,可能是ؓ了安全着惛_Q?br>var fileValue = oFileInput.value;
if(fileValue == ""){
oUploadFiles.removeChild(oFileInput);
return false;
}
else
return true;
}
//---新徏一个文件显C列?br>function addFile(oFileInput){
var oFileList = document.getElementById("fileList");
var fileIndex = oFileList.childNodes.length + 1;
var oTR = document.createElement("TR");
var oTD1 = document.createElement("TD");
var oTD2 = document.createElement("TD");
oTR.setAttribute("id","file_" + fileIndex);
oTR.setAttribute("bgcolor","#FFFFFF");
oTD1.setAttribute("width","6%");
oTD2.setAttribute("width","94%");
oTD2.setAttribute("align","left");
oTD2.innerText = oFileInput.value;
oTD1.innerHTML = '<A href="javascript:removeFile('+ fileIndex + ');">删除</A>';
oTR.appendChild(oTD1);
oTR.appendChild(oTD2);
oFileList.appendChild(oTR);
}
//---U除上传的文?nbsp;
function removeFile(fileIndex){
var oFileInput = document.getElementById("upfile_" + fileIndex);
var oTR = document.getElementById("file_" + fileIndex);
uploadFiles.removeChild(oFileInput);
fileList.removeChild(oTR);
}
//---创徏一个file input对象q返?br>function newFileInput(_name){
var oFileInput = document.createElement("INPUT");
oFileInput.type = "file";
oFileInput.id = _name;
oFileInput.name = _name;
oFileInput.size="50";
//oFileInput.setAttribute("id",_name);
//oFileInput.setAttribute("name",_name);
//oFileInput.outerHTML = '<INPUT type=file id=' + _name + ' name=' + _name + '>';
//alert(oFileInput.outerHTML);
return oFileInput;
}
</SCRIPT>
posted on 2007-01-26 17:21 重归本垒QBNBNQ?/a> 阅读(1656) 评论(4) ~辑 收藏 引用 所属分c? JS
呵呵Q我的方法不知道和你的三U方法有没有可比性,个h感觉q不错! @施伟 施伟 的做法,是不是还是不能解冻I先选择了一个文Ӟ提交服务器之后这个file input域的值又被自动清I的问题Q?nbsp; 回复 更多评论 评论
# re: 几种js实现的动态多文g上传 2007-01-27 13:20 施伟
做一?d附g 然后做一个type为file的input框,把此框和span定位重叠h 把file框透明度设|ؓ0 卛_全看不到Q但是确实存在。这个时候点span的时候就是在点这个file?但是看不到file框子 是不是实C呢? 然后再结合你W二U的方式l框~号 动态增加就可以实现多文件上传了 ?
呵呵 我在我的E序里面q样实现?很好?如果有兴讨论到我blog留言 或者发邮gl我?多交。。?
回复 更多评论 # re: 几种js实现的动态多文g上传 2007-01-29 18:00 重归本垒QBNBNQ?/a>
呵呵Q施伟,你这样做Q如果实CQ那么比我的Ҏ更胜一{了Q我以前也这栯虑q,只是觉的好麻烦,而没有去实现它!
另外Q还非常谢谢你能x我的Qolg!
回复 更多评论 # re: 几种js实现的动态多文g上传 2007-02-12 17:12 路过?/a>
# re: 几种js实现的动态多文g上传 2007-06-04 11:28 sangern
初学JSP?写了一些工具函数因Z太会用JAVA下的正则表达式也只能q么写啦!
发出来让大家批评批评提点意见!有几个函C是自己写的希望爱挑剌的朋友?
下留?我是新手我怕谁,脸皮不行的h水^也上不去呀.d..
package mxzc.web.strctrl;
public class StringCtrl
{/********************************************
public synchronized String HTMLcode(String TXTcode) 功能:文本替换
public synchronized String Unhtmlcode(String str) 功能:(不完?反文?
替换
public synchronized String Unhtmlcodea(String str) 功能:反文本替?br>public synchronized boolean emailcheck (String email) 功能:查一个字
W串是否W合E-Mail
public synchronized boolean isemailstr(String email) 功能:查一个字
W串是否W合E-Mail
public synchronized boolean isqqstr(String qq) 功能:查一个字W串?
否符合QQ
public synchronized boolean isnumstr(String num) 功能:查一个字W串
是否Z数字?br>public synchronized String userstrlow(String user) 功能:替换用户名中
不合法的部分
public synchronized boolean userstrchk(String user) 功能:查字W串?
否符合用户名法则
public synchronized boolean istelstr(String tel) 功能:查字W串是否
为TEL
public synchronized boolean urlcheck(String url) 功能:查字W串是否
为URL
public synchronized String isotogbk(String iso) 功能:ISO9006-1码{?
为GBK
public synchronized String gbktoiso(String gbk) 功能:GBK码{换ؓ
ISO9006-1
public synchronized String dostrcut(String oldstr,int length) 功能:?
汉字长换?英文按半个字?
public synchronized String inttodateshow(int datenum) 功能:?900q至
旉的秒数换为日期字W串
public synchronized String nowdateshow() 功能:昄当前日期
public synchronized java.util.Date inttodate(int datenum) 功能:秒?
转换为日?br>public synchronized int datetoint() 功能:时间换Z1900q至今的U?
?br>public synchronized int datetoint(java.util.Date d) 功能:时间换Z
1900q至旉的秒?br>public synchronized String overlengthcut(String str,int length) 功能:
截取前几个字W?单位为汉字字?br>public synchronized String replace(String str,String suba,String subb)
功能:字符串替?br>*********************************************/
private static final String isostr="ISO8859-1";
private static final String gbkstr="GBK";
public StringCtrl()
{
}
public synchronized boolean emailcheck (String email)
{
if(email==null)return false;
if(email.length()<6)return false;
if(email.indexOf("@")<2)return false;
if(email.indexOf(".")<4)return false;
if(email.endsWith(".")||email.endsWith("@"))return false;
if(email.lastIndexOf("@")>email.lastIndexOf(".")-1)return false;
if(email.lastIndexOf("@")!=email.indexOf("@"))return false;
String[] lowstr={"\@#","\"","\n","&","\t","\r","<",">","/","\\","#"};
for(int i=0;i<lowstr.length;i++)if(email.indexOf("lowstr")>0)return
false;
return true;
}
public synchronized boolean isemailstr(String email)
{
if(email==null)return false;
if(email.indexOf("@")==-1||email.indexOf(".")==-1||email.length()<6)
return false;
return true;
}
public synchronized boolean isqqstr(String qq)
{
if(qq==null)return false;
if(qq.length()>12)return false;
if(qq.length()<5)return false;
for(int i=0;i<qq.length();i++)
if(!(((int)qq.charAt(i))<=57&&((int)qq.charAt(i))>=48))return false;
return true;
}
public synchronized boolean isnumstr(String num)
{
if(num==null)return false;
if(num.length()<1)return false;
for(int i=0;i<num.length();i++)
if(!(((int)num.charAt(i))<=57&&((int)num.charAt(i))>=48))return false;
return true;
}
public synchronized String userstrlow(String user)
{
String newuserstr=user.trim();
char[] lowstr=
{@#\@#@#,@#\"@#,@#\n@#,@#&@#,@#\t@#,@#\r@#,@#<@#,@#>@#,@#/@#,@#\\@#,@##
@#};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],@#+@#);
return newuserstr;
}
public synchronized boolean userstrchk(String user)
{
String newuserstr=user.trim();
char[] lowstr=
{@#\@#@#,@#\"@#,@#\n@#,@#&@#,@#\t@#,@#\r@#,@#<@#,@#>@#,@#/@#,@#\\@#,@##
@#,@#~@#,@#`@#,@#!@#,@#@@#,@#$@#,@#%@#,@#^@#,@#*@#,@#(@#,@#)@#,@#-
@#,@#_@#,@#+@#,@#=@#,@#|@#,@#?@#,@#,@#,@#;@#,@#.@#};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],@#+@#);
return (user.equals(newuserstr))?true:false;
}
public synchronized boolean istelstr(String tel)
{
if(tel==null)return false;
if(tel.length()<1)return false;
if(tel.length()>32)return false;
for(int i=0;i<tel.length();i++)
if(!(((int)tel.charAt(i))<=57&&((int)tel.charAt(i))>=48))if(tel.charAt
(i)!=@#-@#)return false;
return true;
}
public synchronized boolean urlcheck(String url)
{
if(url==null)return false;
if(url.length()<10)return false;
String urls=url.toLowerCase();
if(!urls.startsWith("http://"))return false;
if(url.indexOf("<")>0||url.indexOf(">")>0)return false;
return true;
}
public synchronized String isotogbk(String iso)throws Exception
{
if(iso!=null)return (new String(iso.getBytes(isostr),gbkstr));
if(iso.length()<1)return "";
return null;
}
public synchronized String gbktoiso(String gbk)throws Exception
{
if(gbk!=null)return (new String(gbk.getBytes(gbkstr),isostr));
if(gbk.length()<1)return "";
return null;
}
public synchronized String HTMLcode(String TXTcode)
{
String newstr="";
if(TXTcode==null)return "";
newstr=TXTcode;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,"\"",""");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"\@#","'");
return newstr;
}
public synchronized String Unhtmlcode(String str)
{
String newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
//newstr=replace(newstr,""","\"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,""","\"");
//newstr=replace(newstr,"<","<");
//newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","\@#");
return newstr;
}
public synchronized String Unhtmlcodea(String str)
{
String newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,""","\"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","\@#");
return newstr;
}
public synchronized String dostrcut(String oldstr,int length)
{
int i=0;
int j=0;
int k=0;
String newstr="";
if(oldstr==null)return "";
if(length<=0)return "";
for(i=0;i<oldstr.length();i++)
{
if(oldstr.charAt(i)==@#\n@#)j=0;
else if(((int)(oldstr.charAt(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
newstr=newstr.concat(oldstr.substring(k,i)+"\n");
k=i;
j=0;
}
}
newstr=newstr.concat(oldstr.substring(k)+"\n");
return newstr;
}
public synchronized String inttodateshow(int datenum)
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
String datestr="";
java.util.Date d;
d=new java.util.Date((long)(datenum)*1000);
java.util.Calendar ds=java.util.Calendar.getInstance();
ds.setTime(d);
year=ds.get(java.util.Calendar.YEAR);
month=ds.get(java.util.Calendar.MONTH);
day=ds.get(java.util.Calendar.DATE);
hour=ds.get(java.util.Calendar.HOUR_OF_DAY);
minute=ds.get(java.util.Calendar.MINUTE);
second=ds.get(java.util.Calendar.SECOND);
datestr=Integer.toString(year)+"/"+Integer.toString(1+month)
+"/"+Integer.toString(day);
return datestr;
}
public synchronized String nowdateshow()
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
String datestr="";
java.util.Calendar ds=java.util.Calendar.getInstance();
year=ds.get(java.util.Calendar.YEAR);
month=ds.get(java.util.Calendar.MONTH);
day=ds.get(java.util.Calendar.DATE);
hour=ds.get(java.util.Calendar.HOUR_OF_DAY);
minute=ds.get(java.util.Calendar.MINUTE);
second=ds.get(java.util.Calendar.SECOND);
datestr=Integer.toString(year)+"/"+Integer.toString(1+month)
+"/"+Integer.toString(day);
return datestr;
}
public synchronized java.util.Date inttodate(int datenum)
{
int year=0;
int month=0;
int day=0;
String datestr="";
java.util.Date d;
d=new java.util.Date((long)(datenum)*1000);
return d;
}
public synchronized int datetoint()
{
java.util.Date d=null;
long ds=0;
d=new java.util.Date();
ds=d.getTime();
return (int)(ds/1000);
}
public synchronized int datetoint(java.util.Date d)
{
long ds=0;
ds=d.getTime();
return (int)(ds/1000);
}
public synchronized String overlengthcut(String str,int length)
{
int i=0;
int j=0;
if(str==null)return "";
if(length<0)return "";
if(str.length()<=length)return str;
for(i=0;i<str.length();i++)
{
if(((int)(str.charAt(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
return str.substring(0,i);
}
}
return str;
}
public synchronized String replace(String str,String suba,String subb)
{
String newstr="";
int start=0;
int offset=0;
int subalength=0;
int strlength=0;
if(str==null||suba==null||subb==null)return str;
if(suba.equals(subb))return str;
if(str.length()<suba.length()||str.length()<subb.length())return str;
if(str.length()>0&&suba.length()>0&&subb.length()>0)
{
subalength=suba.length();
strlength=str.length();
while(true)
{
if(str.indexOf(suba)<0)break;
if(offset>strlength)break;
start=str.indexOf(suba,offset);
if(start<offset)break;
newstr=newstr.concat(str.substring(offset,start));
newstr=newstr.concat(subb);
offset=start+subalength;
}
newstr=newstr.concat(str.substring(offset));
return newstr;
}
else
{
return str;
}
}
}
Java是目前最行的面向对象的~程语言之一QJava支持UTF-8、ISO-8
859-1、GBK{各U字体编码,可笔者发现Java中字体编码的问题仍难倒了不少E序员,|上虽然也有不少关于在Java中如何正显CZ文的文章Q但都不够全面,W者特意ȝ如下?
影响Java中字体编码正显C的有几个因? 1)数据?/b>的连接方? 2)|页中用的字体~码; 3)数据库里存放数据的字体编? 4)Java的缺省字体编码。如果在~程中遇C能正显CZ文时Q要先弄清楚以上几项所使用的字体编码,再分析找出原因,卛_解决问题?
众所周知QJSP是Java的一U,和网|养I而网也有自q中文~码pȝQ所以JSP处理中文要比UJava的类文g更ؓȝ。本文的试数据库是MySQL3.2Q数据库q接驱动是用org.gjt.mm.mysql.DriverQ这里主要讨论UTF-8和GBK的显C? GB2312是GBK的一个子集,Java中可以用GBK来代替GBpd)。我们先来研IJSP中字体编码问题, 下面W一到第六点是针对JSP?因ؓ从数据库里读Z文数据与写入中文数据有所区别Q咱们分别说明,前三Ҏ从读取数据库到显C在|页Q后三点是从|页输入数据到存入数据库)Q第七到W九炚w对纯Java的类文g?以下rs表示ResultSet的一个实例,是执行Select语句之后产生的数据集?
一、数据库q接方式使用UTF-8
在连接数据库的驱动后面加上这句参数useUnicode=true&characterEncoding=
UTF-8Q例如jdbc:mysql://localhost/DBVF?autoReconnect=true&useUnicode=
true&characterEncoding=UTF-8Q从数据库里d中文昄在用GBK的JSP的网里Q如果数据库里存攄字体~码是UTF-8Q在JSP中?str=new String(rs.getBytes(1),"UTF-8")或者str=rs.getString(1)Q可以正显CZ文。如果数据库里存攄是GBK数据Q那么JSP中也要用str=new String(rs.getBytes(1),"GBK")来显C正的中文。值得注意的是如果面使用UTF-8Q数据库里存攄是UTF-8Q也可以用str=new String(rs.getBytes(1),"GBK")正确昄中文。如果网|UTF-8Q而数据库里存攄是GBKQ无法直接显CZ文,需?步{? str=new String(rs.getBytes(1),"GBK"); 再str=new String(str.getBytes("UTF-8"),"GBK")Q才可以正确昄中文?
二、数据库q接方式使用GBK
在连接数据库的驱动后面加上这句参数useUnicode=true&characterEncoding=
GBK,例如jdbc:mysql://localhost/DBVF?autoReconnect=true&UseUnicode=true&
characterEncoding=GBKQ从数据库里d中文Q显C在使用GBK的JSP的网里Q如果数据库里存攄字体~码是UTF-8Q在JSP中一定要使用 str=new String(rs.getBytes(1),"UTF-8")Q才正确昄中文。如果数据库里存攄是GBK数据Q那么JSP中也要用str=new String(rs.getBytes(1),"GBK") 或者直接用str=rs.getString(1)Q即可显C正的中文?如果|页是UTF-8Q而数据库里存攄是GBKQ只能用str=new String(rs.getString(1).getBytes("UTF-8"),"GBK")的方法来昄中文; 如果|页是UTF-8Q而数据库里存攄是UTF-8Q可用str=new String(rs.getBytes(1),"GBK") 或者rs.getString(1)Ҏ来显CZ文?
三、用缺省数据库q接方式
q接数据库的驱动后面没有q句参数useUnicode=&characterEncoding=Q例如jdbc:mysql://localhost/DBVF?autoReconnect=trueQ没有参数useUnicode=true&characterEncodingQ表CZ用默认的ISO-8895-1~码?
1. 从数据库里读Z文,昄在GBK的网里。如果数据库里存攄字体~码是UTF-8Q在JSP|页中一定要使用语句 str=new String(rs.getBytes(1),"UTF-8") 或者str= new String(rs.getString(1).getBytes("ISO-8859-1"),"UTF-8")Q才可正显CZ文。如果数据库里存攄是GBK数据Q那么JSP中也要用str=new String(rs.getBytes(1),"GBK")或str=new String(rs.getString(1).getBytes("ISO-8859-1"),"GBK") 昄正确的中文?
2. 如果|页是UTF-8,不能直接正确昄GBKQ需?步{换,str=new String(rs.getBytes(1),"GBK")Q再str=new String(str.getBytes("UTF-8"),"GBK") 才可以正显CZ文。如果数据库里存的是UTF-8Q直接用str=new String(rs.getBytes(1),"GBK")或者str=new String(rs.getString(1).getBytes("ISO-8859-1"),"GBK")可以显CZ文了?
以上是读取数据库里中文正显C在|页上,下面三点是如何正存入数据库?
四、数据库q接方式使用UTF-8~码
JSP中要把网输入的中文存入数据库,通常有一个提?Submit)的过E,是用str=request.getParameter("username")Q然后执行update或者insert语句来存入数据库。如何赋值给str很重要,而且q里中文输入与网|使用的字体编码有兟?
1?|页使用UTF-8Q用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8")或者str= new String(request.getParameter("username").getBytes(),"UTF-8")Q都可以使得存到数据库里的数据是UTF-8~码?
2. |页使用GBKQ用str= new String(request.getParameter("username").getBytes(),"GBK")Q那么存入数据库的是UTF-8~码?
3. 值得注意的是使用UTF-8的数据库q接方式不能存得GBK?
五、数据库q接方式使用GBK~码
1. 输入使用GBK|页Q存到数据库里是GBK的方? str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK") 或者str= new String(request.getParameter("username").getBytes(),"GBK")?
2. |页使用GBKQ想存入UTF-8到数据库里,要分2? 先str=new String(request.getParameter("username").getBytes(),"GBK")Q再str=new String(str.getBytes("UTF-8"),"GBK")卛_?
3. |页使用UTF-8Q而且使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK") 或者str= new String(request.getParameter("username").getBytes(),"UTF-8")Q那么存到数据库里的数据是UTF-8~码?
4. |页使用UTF-8Q而且使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8")Q那么存到数据库里的数据是GBK~码?
六、数据库q接方式使用~省Q即不用参数useUnicode和characterEncoding
1. |页使用GBKQ如果用str= request.getParameter("username")或者str= new String(request.getParameter("username").getBytes())Q那么在数据库里的数据是GBK码。网用UTF-8 和用str= request.getParameter("username")Q则存入数据库是UTF-8~码?
2. 如果使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"))Q那么根据网|供的字体~码而存到数据库里,比如是UTF-8的网,那么存到数据库中是UTF-8~码Q如果用GBK|页Q那么存到数据库里的字就是GBK~码?
3. 如果使用str= new String(request.getParameter("username").getBytes("UTF-8"),"UTF-8")q一U组合能存到正确的数据外Q其他存到数据库里的数据则都是ؕ码或者错误码。在q个UTF-8l合的特例中Q网用的是GBKQ则存放到数据库里就是GBKQ网用UTF-8Q那么存到数据库里的是UTF-8?
4. |页是GBK的要存得UTF-8Q一定需?? company=new String(request.getParameter("company").getBytes(),"GBK")和company=new String(company.getBytes("UTF-8"))?
5. |页是UTF-8的,不能存得GBK在数据库里,一句话Q改变数据库q接方式不能存得GBK码?
以上所有的都是ZJSP|页和数据库交换数据Q下面讨Z下纯JAVA~程下的字体~码转换?
七、数据库q接方式使用UTF-8~码
1. 数据库里的中文是UTF-8Q可以{换ؓGBK,但不能把GBK存入数据库?
2. 数据库是GBKQ如果{换ؓUTF-8Q用content=new String(rs.getBytes(2),"GBK")直接content存入数据库就可ؓUTF-8?
八、数据库q接方式使用GBK~码
1. 数据库里的中文是UTF-8Q如果{换ؓGBKQ用content= new String(rs.getString(2).getBytes(),"UTF-8"),再直接用update或者insert语句插入到数据库Q即存得GBK。如果用content= new String(rs.getString(2).getBytes(),"GBK")或者content= new String(rs.getString(2).getBytes())Q再存入数据库即存得q是UTF-8~码?
2. 数据库里的中文是GBKQ如果{换ؓUTF-8Q用content= new String(rs.getString(2).getBytes("UTF-8"))或者content= new String(rs.getString(2).getBytes("UTF-8"),"GBK")Q再直接使用update或者insert语句插入到数据库,卛_得UTF-8?
3. 如果某个String是GBKQ要转换为UTF-8Q也是用content= new String(GBKstr.getBytes("UTF-8"))或者content= new String(GBKstr.getBytes("UTF-8"),"GBK"); 如果某个String是UTF-8Q要转换为GBKQ应该用new String(UTFstr.getBytes("GBK"),"UTF-8")?
九、数据库q接方式使用~省,即不跟参?
1. str2=new String(GBKstr.getBytes("UTF-8"),"ISO-8859-1")Q可以将数据库里的GBK~码转换为UTF-8?
2. dUTF-8然后存入UTF-8Q则用str1=new String(UTFstr.getBytes(),"ISO-8859-1")或者str1=new String(UTFstr.getBytes("GBK"),"ISO-8859-1")?
3. 不能实现数据库里的UTF-8转换为GBK?
如果采用UTF-8的数据库q接方式或者缺省数据连接方式,那么无法UTF-8转ؓGBKQ而GBK的数据库q接方式可以实现UTF-8和GBK的相互{换。徏议大安用GBK的数据连接方式?/p>
??br> ~码表示?br> ??br> 表现形式
�-
-
Unused
-
-
HorizontalTab
space
-
Linefeed
space
-
-
Unused
-
-
Space
space
!
-
Exclamationmark
!
"
"
Quotationmark
"
#
-
Numbersign
#
$
-
Dollarsign
$
%
-
Percentsign
%
&
&
Ampersand
&
'
-
Apostrophe
'
(
-
Leftparenthesis
(
)
-
Rightparenthesis
)
*
-
Asterisk
*
+
-
Plussign
+
,
-
Comma
,
-
-
Hyphen
-
.
-
Period(fullstop)
.
/
-
Solidus(slash)
/
0-9
-
Digits(0-9)
0-9
:
-
Colon
:
;
-
Semi-colon
;
<
<
Lessthan
<
=
-
Equalssign
=
>
>
Greaterthan
>
?
-
Questionmark
?
@
-
Commercialat
@
A-Z
-
UppercaseA-Z
A-Z
[
-
Leftsquarebracket
[
\
-
Reversesolidus(backslash)
\
]
-
Rightsquarebracket
]
^
-
Caret
^
_
-
Horizontalbar
_
`
-
Acuteaccent
`
a-z
-
Lowercasea-z
a-z
{
-
Leftcurlybrace
{
|
-
Verticalbar
|
}
-
Rightcurlybrace
}
~
-
Tilde
~
-t
-
Unused
-
Non-breakingspace
¡
¡
Invertedexclamation
¡
Centsign
n
n
Poundsterlingsign
n
]
]
Generalcurrencysign
]
Yensign
&brVBar;
Brokenverticalbar
Sectionsign
t
t
UMLaut(dierisis)
t
©
©
Copyright
©
ª
ª
Feminineordinal
ª
J
J
Leftanglequote,guillemotleft
J
R
R
Notsign
R
Softhyphen
]
]
ReGISteredtrademark
]
t
t
Macronaccent
t
Degreesign
u
u
Plusorminus
u
̔
̔
Superscripttwo
̔
Superscriptthree
´
´
Acuteaccent
´
µ
µ
Microsign
µ
¶
¶
Paragraphsign
¶
·
·
Middledot
·
T
T
Cedilla
T
¹
¹
Superscriptone
¹
º
º
Masculineordinal
º
Rightanglequote,guillemotright
Fraction(onequarter)
Fraction(onehalf)
¾
¾
Fraction(threequarters)
¾
H
H
Invertedquestionmark
H
À
À
CapitalA,graveaccent
À
Á
Á
CapitalA,acuteaccent
Á
Â
Â
CapitalA,circumflexaccent
Â
Ã
Ã
CapitalA,tilde
Ã
Ä
Ä
CapitalA,umlaut(dierisis)
Ä
Å
Å
CapitalA,ring
Å
Æ
Æ
CapitalAEdipthong(ligature)
Æ
Ç
Ç
CapitalC,cedilla
Ç
È
È
CapitalE,graveaccent
È
É
É
CapitaE,acuteaccent
É
Ê
Ê
CapitalE,circumflexaccent
Ê
&EUML;
Ë
CapitalE,umlaut(dierisis)
Ë
Ì
Ì
CapitalI,graveaccent
Ì
Í
Í
CapitalI,acuteaccent
Í
Î
Î
CapitalI,circumflexaccent
Î
Ï
Ï
CapitalI,umlaut(dierisis)
Ï
Ð
Ð
CapitalEth,Icelandic
Ð
Ñ
Ñ
CapitalN,tilde
Ñ
Ò
Ò
CapitalO,graveaccent
Ò
Ó
Ó
CapitalO,acuteaccent
Ó
Ô
Ô
CapitalO,circumflexaccent
Ô
Õ
Õ
CapitalO,tilde
Õ
Ö
Ö
CapitalO,umlaut(dierisis)
Ö
×
×
Multiplysign
×
Ø
Ø
CapitalO,slash
Ø
Ù
Ù
CapitalU,graveaccent
Ù
Ú
Ú
CapitalU,acuteaccent
Ú
Û
Û
CapitalU,circumflexaccent
Û
Ü
Ü
CapitalU,umlaut(dierisis)
Ü
Ý
Ý
CapitalY,acuteaccent
Ý
Þ
Þ
CapitalThorn,Icelandic
Þ
ß
ß
Smallsharps,German(szligature)
ß
à
à
Smalla,graveaccent
à
֙
֙
Smalla,acuteaccent
֙
â
â
Smalla,circumflexaccent
â
ã
ã
Smalla,tilde
ã
&aUML;
ä
Smalla,umlaut(dierisis)
ä
å
å
Smalla,ring
å
æ
æ
Smallaedipthong(ligature)
æ
ç
ç
Smallc,cedilla
ç
؈
؈
Smalle,graveaccent
؈
é
é
Smalle,acuteaccent
é
^
^
Smalle,circumflexaccent
^
ë
ë
Smalle,umlaut(dierisis)
ë
ì
ì
Smalli,graveaccent
ì
T
T
Smalli,acuteaccent
T
î
î
Smalli,circumflexaccent
î
ï
ï
Smalli,umlaut(dierisis)
ï
ð
ð
Smalleth,Icelandic
ð
ñ
ñ
Smalln,tilde
ñ
ò
ò
Smallo,graveaccent
òò
Q
Q
Smallo,acuteaccent
Q
ô
ô
Smallo,circumflexaccent
ô
õ
õ
Smallo,tilde
õ
ö
ö
Smallo,umlaut(dierisis)
ö
÷
÷
Divisionsign
÷
ø
ø
Smallo,slash
ø
ù
ù
Smallu,graveaccent
ù
ú
ú
Smallu,acuteaccent
ú
]
]
Smallu,circumflexaccent
]
ü
ü
Smallu,umlaut(dierisis)
ü
ý
ý
Smally,acuteaccent
ý
V
V
Smallthorn,Icelandic
V
ÿ
ÿ
Smally,umlaut(dierisis)
ÿ
Ҏ上表Q我们来?个应用实例:
© 2000 Some Co., Inc.Q用©表示版权标识信息?
© 2000 Some Co., Inc.Q用©同样标识版权标识信息?
对不被信ȝ数据q行~码要优于过滤不被信ȝ数据。在客户端浏览器需要显CZ些特D字W的情况下,q种处理方式会恢复特D字W的庐山真面目。当ӞҎ有不被信ȝ数据q行~码Q是个相当耗费资源的工作。根据需要,Web开发者可以在~码和过?U方法中q行权衡Q选择一U或者合用?
该文章{载自1024kQhttp://www.1024k.cn/web/2007/200701/15407.html
?在块U别元素所包含的内容中应考虑的特D字W有Q?
<Q引入一个标?
&Q引入一个字W实?
>Q结束一个标?
?在属性g应考虑的特D字W有Q?
"Q在以双引号包裹属性值的情况下,"标记了属性值的l尾?
'Q在以单引号包裹属性值的情况下,'标记了属性值的l尾?
I白字符Q在属性值没有被M引号包裹的情况下Q空白字W标C属性值的l尾Q比如空根{tab?
&Q当需要在属性g引入字符实体Ӟ需要?amp;?
?搜烦引擎pȝ会在搜烦l果面中包含一个再ơ运行搜索的链接Q这个链接中会包含编码过的搜索查询字W串。这U情况下应考虑的特D字W有Q?
I格、tab以及换行W:它们标记了URL的结?
&Q它标记一个字W实体,或者一个独立的CGI参数?
非ASCII字符Q就是ISO-8859-1~码中ASCII码大?28的字W,它们不允许在URL中用?
%Q无论服务器对由escape~码的HTTP参数如何解码Q都必须qo出字W??
??之间的内容中应考虑的特D字W有Q分P圆括P大括号以及换行符?
?在服务器端脚本中应考虑的特D字W有Q!
?其他情况下应考虑的特D字W有Q?。但注意Q现有的d事gq没有利用过q个字符Q但作ؓ一个全面考虑Q这里还是列出来?
另外Q我们还要重视一U情况:其他附加字符也可能包含在Ҏ字符列表中。MQWeb开发者必d应用E序的输入内容进行检查,以确定哪些字W会影响E序的执行?
该文章{载自1024kQhttp://www.1024k.cn/web/2007/200701/15408.html
问题分析
Web面包含文本和HTML标记Q它们由服务器徏立,被客L解释。HTML标记一般被服务器特D对待。例如,“<”一般指CZ个HTML标记的开始,“<P>”可以媄响页面的昄格式Q<Script>可以脚本代码引入浏览器而执行?
对于静态页面,服务器可以完全控制它在客L如何解释。但是对于动态页面,服务器就不可能完全控制它在客L的解释行Z。这样就产生了问题:如果动态页面中包含了不可信的内容,那么无论是服务器端还是客LQ就不能保证是否会发生安全问题?
现在Q几乎所有的商用Web服务器就会徏立动态页面。最典型的一个例子就是我们经常要使用的搜索引擎,它接受用L查询内容后,搜烦数据库,然后动态内容写入一个页面模板,最后显C给用户包含搜烦l果的页面。这U情况下Q检查动态内Ҏ否包含了Ҏ字符非帔R要,例如“<”。如果包含了Ҏ字符Q用L的浏览器可能将之误解ؓHTML标记或者引入执行程序,而不是当做文本信息显C出来。危险也׃生于此!如果不对动态页面进行特D字W的查,那么d者就有可能在交互面的输入表单中写入些特D字W串Q从而导致输出页面执行非法行为。这U例子很多,比如我们~写一个留a,却不对输入内容进行特D字W的校验Q那么攻击者就有可能填写特D字W,最l导致留aK面的非正常工作,例如填写一D|意代码:d@环JavaScript脚本、重复打开H口的JavaScript脚本?
应对措施分析
通常Q不被信ȝ内容主要来自以下几个斚wQURL参数Q表单输入元素,Cookies和数据库查询。要减轻q些斚w可能D的攻击,采取如下的步骤:
1、ؓ每个由服务器产生的Web面明确地设|字W集~码
2、鉴别特D的字符
3、对动态输出内容编?
4、过滤动态输出内容中的特D字W?
5、检查cookies ?
以下详细分析q?个步骤?
W一步:明确地设|字W集~码
字符集编码就是页面的字符~码体系Q在览器中可以通过“查?~码”来转换Q?
很多Web面都省略了字符集编码设|,也就是说在页面源代码头部HTTP一节中没有定义charset参数。早期的HTML版本中,如果没有定义charset参数Q字W集~码默认ؓISO-8859-1。但实际上,许多览器都有各自的默认字符集编码。因此,HTML版本4规定Q如果没有指定charset参数QQ何可能的字符集编码都可以使用Q这׃赖于用户的浏览器U类了?
如果Web服务器不能指定用哪个字W集~码Q那么它׃能区分出Ҏ字符。没有指定字W集~码的Web面之所以可以在大多数时间中工作良好Q这是因为在大多数的字符集编码类别中Q同一字符对应一个小?28的字节倹{对于大?28的特D字W,例如“<”,采?6位字W编码方案处理。一些浏览器能够识别q执行这U编码方案,但同Ӟd者也可能据此使用恶意脚本Q防范隑ֺ加大Q因为服务器可能完全不能了解哪个字节代表哪个Ҏ字符。例如,字符集UTF-7为“<”和?gt;”提供了可选编码,几种行的浏览器一般将它们看做标记的v始和l束字符。ؓ了不造成服务器和客户端的字符~码的不一_Web服务器应该明设|字W集Q以认插入的数据是否ؓҎ字符~码的后l字节。比如,下面的代码强行设|了面使用ISO-8859-1字符集编码:
QHTML>
QHEAD>
QMETA http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
QTITLE>HTML SAMPLEQ?TITLE>
Q?HEAD>
QBODY>
QP>This is a sample HTML page
Q?BODY>
Q?HTML>
该文章{载自1024kQhttp://www.1024k.cn/web/2007/200701/15409.html
W一ơ尝试用:
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.5hope.com
DcumentRoot "G:\5hope
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.5hope.com:88/
ProxyPassReverse / www.5hope.com:88/
</VirtualHost>
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
本以栯|多站点搞定了Q结果发现只识别W一个站点,讉K别的站点都是q个站的内容Q折腾了一上午Q没成功?
W二ơ尝试用:
<VirtualHost *:80>
#d了这个属?*********
ProxyPreserveHost On
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "D:\tomcat5.0.28\conf\workers.properties"
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.openria.cn
DirectoryIndex index.html index.htm index.jsp
JkMount /* ajp13
JkAutoAlias "D:\tomcat-5.0.28\Webapps\ria"
<Directory "D:\tomcat-5.0.28\webapps\ria">
Options Indexes FollowSymLinks
allow from all
</Directory>
</VirtualHost>
q回l过查官方资料,发现了一个属性,叫ProxyPreserveHost OnQ试了一下,是可以用实现多个虚拟的asp站点了,但是和我的tomcat站点定义冲突Q访问不了jsp站,又不行,只好再找?
W三ơ尝试用:
NameVirtualHost *:80
<VirtualHost *:80>
ProxyPreserveHost On
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "D:\tomcat5.0.28\conf\workers.properties"
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.openria.cn
DirectoryIndex index.html index.htm index.jsp
JkMount /* ajp13
JkAutoAlias "D:\tomcat-5.0.28\webapps\ria"
<Directory "D:\tomcat-5.0.28\webapps\ria">
Options Indexes FollowSymLinks
allow from all
</Directory>
</VirtualHost>
l过反复看文档,q回l于搞定了,原来是没有吧"Use name-based virtual hosting."打开Q去掉NameVirtualHost *:80前面?号就可以了,真是晕啊?
ȝ成功了,看来有问题还需要看官方资料Q网友的资料q是不完整啊Q通过自己的努力,发现新的U烦Q?
ProxyPreserveHost On
NameVirtualHost *:80
q也是自q收获啊,希望q些l历能帮助需要他的h?
再次庆祝一下,自己的网站终于要开通了Q欢q访问:www.openria.cn
该文章{载自1024kQhttp://www.1024k.cn/web/2007/200701/15431.html