2006年5月

Java中正则表达式的运用

1.采用正则表达式进行匹配的例子:

 import java.util.regex.Matcher;
 import java.util.regex.Pattern; 

 reader = new FileReader(filename);
  br = new BufferedReader(reader);

  p_str = ".*(第[^章条边节]+[章边节])(.*)";
  pattern = Pattern.compile(p_str);
  matcher = pattern.matcher(text);
  if (matcher.find())
  {
    str_tmp = matcher.group(1).trim();
    temp = matcher.group(2).trim();
    str_tmp = "<font class="capter1">" + text + "</font>";
  }

2.正则表达式匹配中文的问题:

  ^[u4E00-u9FA5]+$

Java版本的escape和unescape函数

class  EscapeUnescape
{
 public static String  escape (String src)
 {
  int i;
  char j;
  StringBuffer tmp = new StringBuffer();
  tmp.ensureCapacity(src.length()*6);

  for (i=0;i<src.length() ;i++ )
  {

   j = src.charAt(i);

   if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
    tmp.append(j);
   else
    if (j<256)
    {
    tmp.append( "%" );
    if (j<16)
     tmp.append( "0" );
    tmp.append( Integer.toString(j,16) );
    }
    else
    {
    tmp.append( "%u" );
    tmp.append( Integer.toString(j,16) );
    }
  }
  return tmp.toString();
 }

 public static String  unescape (String src)
 {
  StringBuffer tmp = new StringBuffer();
  tmp.ensureCapacity(src.length());
  int  lastPos=0,pos=0;
  char ch;
  while (lastPos<src.length())
  {
   pos = src.indexOf("%",lastPos);
   if (pos == lastPos)
    {
    if (src.charAt(pos+1)=='u')
     {
     ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16);
     tmp.append(ch);
     lastPos = pos+6;
     }
    else
     {
     ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16);
     tmp.append(ch);
     lastPos = pos+3;
     }
    }
   else
    {
    if (pos == -1)
     {
     tmp.append(src.substring(lastPos));
     lastPos=src.length();
     }
    else
     {
     tmp.append(src.substring(lastPos,pos));
     lastPos=pos;
     }
    }
  }
  return tmp.toString();
 }

 public static void main(String[] args)
 {
  String tmp="~!@#$%^&*()_+|=-,./?><;'][{}"";
  System.out.println("testing escape : "+tmp);
  tmp =escape(tmp);
  System.out.println(tmp);
  System.out.println("testing unescape :"+tmp);
  System.out.println(unescape(tmp));
 }
}

Ajax经验总结

1.解决IE的Cache问题:2种途径

  (1)前台在request对象上设置 setRequestHeader()

  req.setRequestHeader("If-Modified-Since","0");

<meta http-equiv=''Expires'' content=''-10''>
<meta http-equiv=''Pragma'' content=''No-cache''>
<meta http-equiv=''Cache-Control'', ''private''>

  (2)后台服务器端返回时,设置相应的http头.

 response.setHeader("Cache-Control", "no-cache");

 response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
  response.setHeader("Pragma","no-cache"); //HTTP 1.0
  response.setDateHeader ("Expires", 0); 

 response.setHeader ("Expires", 0); 
 或者
  SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd   MMM yyyy - HH:mm:ss z"); 
response.setHeader("Expires", dateFormat.format(new Date())); 

2.采用Ajax后,中文乱码的问题:

  WEB服务器采用了TOMCAT5.5,前台的一个JSP页面向后台的一个Servlet提交请求,然后Servlet将中文结果信息通过XML文件out.println到输出流中,结果在前台通过http_request.getresponseXML得到的是乱码.在Servlet中也增加了response.setContentType("text/xml;charset=GBK");但仍然是乱码.

   但这个问题在Resin服务器中就不存在,真郁闷死我了.

   后来只有在网上找了个Escape的JAVA版本,在输出前先Escape一下,然后,在前端的JAVASCRIPT得到数据后在Unescape一下就可以了.

LOG4J的学习

1.LOG4J的配置:采用log4j.properties文件

log4j.rootCategory=warn,A1
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender

#log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=F:/tmp/sogoudoc.txt
#log4j.appender.A1.MaxFileSize=1024KB
#log4j.appender.A1.MaxBackupIndex=1
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p: %c{2}  %d{yyyy-MM-dd HH:mm:ss} %m%n


log4j.logger.log=debug

2.LOG4J的初始化:用一个Servlet来实现它的初始化

package servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.PropertyConfigurator;

public class Log4jInit extends HttpServlet
{
 public void init() throws ServletException
 { 
  // 从Servlet参数读取log4j的配置文件
  PropertyConfigurator.configure(Log4jInit.class.getResource("log4j.properties"));
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 {
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 {
 }
}

 web.xml中增加:

<servlet>
  <servlet-name>Log4jInit</servlet-name>
  <servlet-class>servlet.Log4jInit</servlet-class>
  <init-param>
   <param-name>log4j-config-file</param-name>
   <param-value>log4j.properties</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>

 </servlet>

3.使用LOG4J:

package log;

import org.apache.log4j.*;

public class Log
{
 
 private Logger log = Logger.getLogger(Log.class);
 
 public void testLog()
 {
  try
  {
   log.info("info test");
   log.warn("warn test");
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
 
}

最新文章

最近回复

  • feifei435:这两个URI实际是不一样的
  • zsy: git push origin 分支 -f 给力!
  • 冼敏兵:简单易懂,good fit
  • Jack:无需改配置文件,看着累! # gluster volume se...
  • Mr.j:按照你的方法凑效了,折腾死了。。。。
  • zheyemaster:补充一句:我的网站路径:D:\wamp\www ~~菜鸟站长, ...
  • zheyemaster:wamp2.5(apache2.4.9)下局域网访问403错误的...
  • Git中pull对比fetch和merge | 炼似春秋:[…] 首先,我搜索了git pull和git fe...
  • higkoo:总结一下吧, 性能调优示例: gluster volume s...
  • knowaeap:请问一下博主,你维护的openyoudao支持opensuse吗

分类

归档

其它