文章关键字 ‘php’

jsp拦截JspWriter实现类似php的ob_get_contents

2009年04月6日,星期一

jsp拦截JspWriter实现类似php的ob_get_contents
php可以通过ob_start和ob_get_contents可以拦截并得到echo等方式输出的内容,并可以方便的进行二次处理。java的jsp也是可以非常方便就实现这样的功能,那就是实现一个自定义的JspWriter.
oscache的功能强大的jsp缓存标签就是这样干的。
前段时间作了个简单的rss输出,由于避免访问量过大影响到数据库和接口调用。
于是需要对生成的rss文本进行缓存。原有的jsp代码都已经写好,大概动就太麻烦了,于是用TextJspWriter继承javax.servlet.jsp.JspWriter,然后在jsp作小改造就搞定了。
php的用法参考: http://cn2.php.net/ob_get_contents
TextJspWriter.java的代码为:
[code]

package com.lizongbo.util;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspWriter;

public class TextJspWriter extends JspWriter {
public String getString() {
return sb.toString();
}

private StringBuilder sb = null;

public TextJspWriter() {
this(8192, true);
}

public TextJspWriter(int bufferSize, boolean autoFlush) {
super(bufferSize, autoFlush);
sb=new StringBuilder(bufferSize);
}

@Override
public void clear() throws IOException {
sb = new StringBuilder();

}

@Override
public void clearBuffer() throws IOException {
sb = new StringBuilder();

}

@Override
public void close() throws IOException {

}

@Override
public void flush() throws IOException {

}

@Override
public int getRemaining() {
return 0;
}

@Override
public void newLine() throws IOException {
sb.append("\n");

}

@Override
public void print(boolean arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(char arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(int arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(long arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(float arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(double arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(char[] arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(String arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(Object arg0) throws IOException {
sb.append(arg0);

}

@Override
public void println() throws IOException {
sb.append("\n");

}

@Override
public void println(boolean arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(char arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(int arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(long arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(float arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(double arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(char[] arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(String arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(Object arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
sb.append(cbuf, off, len);
}

}

[/code]

jsp代码:

[code]
《%//读取缓存
String textStr==rssTextCache.get(cacheKey);
if(textStr==null){
JspWriter oldout=out;
out= new com.lizongbo.util.TextJspWriter();
%》
《% //正常输出rss内容%》

《%
textStr=((com.lizongbo.util.TextJspWriter)out).getString();
out=oldout;//放入缓存中
rssTextCache.put(cacheKey,textStr);
}
%》
《%=textStr%》

[/code]

Tags: Java, jsp, JspWriter, php, 缓存

Related posts

用rawurldecode和iconv解决php的编码问题

2009年01月10日,星期六

我在给wordpress加上记录Combined格式的时候,也加了个从referer里获取搜索关键字的功能(参考javaeye的“您正在搜索”加关键字高亮的功能)。
由于google的url里是标准的UTF-8编码,而百度的url里,一般却是GBK编码的。
当时实现这个功能的时候,导致rawurldecode只对google 的url得到了正确的汉字。
今天在网上重新搜索了相关信息,发现其实是有办法解决的。
解决的方法很简单,代码如下:
[code]
$lzb_searchkeyword=rawurldecode($lzb_searchkeywordstr);
$lzb_searchkeyword=iconv("GBK","UTF-8",$lzb_searchkeyword) ;//
通过这一行就把百度来的GBK汉字转成了UTF-8的,在页面上就可以正常显示了。
$lzb_searchurl='http://www.baidu.com/s?wd='.$lzb_searchkeywordstr;
[/code]

以后文章正文下方的recent related 1 searches的地方,基本不会再出现乱码了。

Tags: iconv, php, rawurldecode, 乱码

Related posts

Java和Php的多国语言资源文件编辑工具

2008年01月31日,星期四

java使用properties文件来存放多国语言信息,编辑java的properties文件,
可以用一些java ide自带的插件,也可以使用可独立运行的langproper,

来自: http://sourceforge.net/projects/popeye

langproper可以方便的切换文本的正常内容和编码后的内容,支持同时将多种语言一起显示,方便对照修改。

下载地址为:

http://jaist.dl.sourceforge.net/sourceforge/popeye/langproper-0.54-source.jar

http://nchc.dl.sourceforge.net/sourceforge/popeye/langproper-0.54.jar

php一般用po文件存放多国语言信息,po的源文件是mo文件,
比较好用的编辑工具是poedit,来自: http://www.poedit.net/download.php#win32

参考: http://www.chedong.com/blog/archives/001115.html
下载地址为:
(windows版) http://nchc.dl.sourceforge.net/sourceforge/poedit/poedit-1.3.9-setup.exe

Tags: java ide, php

Related posts