?? dlog_velocityservlet.java
字號:
/*
* DLOG_VelocityServlet.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Winter Lau
* http://dlog4j.sourceforge.net
*
*/
package com.liusoft.dlog4j.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.view.servlet.VelocityLayoutServlet;
import com.liusoft.dlog4j.Globals;
import com.liusoft.dlog4j.TextCacheManager;
import com.liusoft.dlog4j.util.RequestUtils;
/**
* 對VelocityLayoutServlet進行擴展,主要進行編碼的自動處理
* @author liudong
*/
public class DLOG_VelocityServlet extends VelocityLayoutServlet {
private String encoding;
public void init() throws ServletException {
super.init();
encoding = getInitParameter("encoding");
if(encoding==null)
encoding = Globals.ENC_UTF_8;
//初始化大文本緩存管理器
initTextCacheManager();
}
private void initTextCacheManager(){
String dir = getInitParameter("text-cache-dir");
String cache_dir;
if(dir==null){
cache_dir = System.getProperty("java.io.tmpdir");
}
else{
if(dir.startsWith(Globals.LOCAL_PATH_PREFIX))
cache_dir = dir.substring(Globals.LOCAL_PATH_PREFIX.length());
else if(dir.startsWith("/"))
cache_dir = getServletContext().getRealPath(dir);
else
cache_dir = dir;
}
if(!cache_dir.endsWith(File.separator))
cache_dir += File.separator;
if(dir==null){
cache_dir += "dlog";
cache_dir += File.separator;
cache_dir += "text";
}
TextCacheManager.init(cache_dir);
}
protected void doRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setBufferSize(8192);
HttpServletRequest request;
if (RequestUtils.isMultipart(req)) {
//文件表單的編碼處理
request = req;
request.setCharacterEncoding(encoding);
} else {
//自動編碼處理
String enc = req.getCharacterEncoding();
if (req instanceof RequestProxy)
request = req;
else if (encoding.equalsIgnoreCase(enc))
request = req;
else
request = new RequestProxy(req, encoding);
}
super.doRequest(request, res);
}
/**
* 自定義Velocity的錯誤處理辦法
* @param req
* @param res
* @param excp 捕獲的異常信息
*/
protected void error(HttpServletRequest req,
HttpServletResponse res,
Exception excp) throws ServletException
{
Throwable t = excp;
if(excp instanceof MethodInvocationException)
t = ((MethodInvocationException)excp).getWrappedThrowable();
try{
if(t instanceof ResourceNotFoundException){
super.log("Velocity Template not found: " + req.getRequestURL().toString());
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if(t instanceof IllegalAccessException){
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
StringBuffer log = new StringBuffer("ERROR:Unknown Velocity Error,url=");
log.append(req.getRequestURL());
if(req.getQueryString()!=null){
log.append('?');
log.append(req.getQueryString());
}
log.append('(');
log.append(new Date());
log.append(')');
super.log(log.toString(), t);
log = null;
req.setAttribute(PageContext.EXCEPTION, t);
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}catch(IOException e){
System.err.println("Exception occured in VelocityServlet.error");
e.printStackTrace(System.err);
}
return;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -