📄 actionservlet.java
字号:
package com.easyjf.web;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.easyjf.web.config.*;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.util.SimplePool;
/**
*
* <p>Title:EasyJWeb核心Servlet</p>
* <p>Description: EasyJWeb核心Servlet,所有的.ejf访问都将由该Servlet处理
* 用户必须在web.xml文件指定扩展名为.ejf的访问都指向该类或其子类。 </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public class ActionServlet extends HttpServlet
{
static final long serialVersionUID=8880L;
private static final Logger logger = (Logger) Logger.getLogger(ActionServlet.class.getName());
public static final String DEFAULT_OUTPUT_ENCODING = "utf-8";
public static final String SERVLET_CONTEXT_KEY = ServletContext.class.getName();
private static SimplePool writerPool = new SimplePool(60);
private String defaultContentType;
private boolean warnOfOutputStreamDeprecation = true;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
Globals.CONFIG_FILE_FULL_PATH=getServletConfig().getServletContext().getRealPath(Globals.CONFIG_FILE);
Globals.APP_BASE_DIR=getServletConfig().getServletContext().getRealPath("/");
WebConfig.getInstance().init();
initTemplate(config);
}
/**
* 初始化模板
* @param config
* @throws ServletException
*/
protected void initTemplate(ServletConfig config) throws ServletException
{
Velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, getServletContext());
Properties p = new Properties();
if(WebConfig.getInstance().getTemplateBasePath()==null || WebConfig.getInstance().getTemplateBasePath().equals(""))WebConfig.getInstance().setTemplateBasePath(Globals.DEFAULT_TEMPLATE_PATH);
String realTemplatePath=WebConfig.getInstance().getTemplateBasePath();
File file=new File(WebConfig.getInstance().getTemplateBasePath());
if(!file.exists())realTemplatePath=config.getServletContext().getRealPath(WebConfig.getInstance().getTemplateBasePath());
p.setProperty("file.resource.loader.path",realTemplatePath);
//System.out.println(realTemplatePath);
try
{
Velocity.init(p);
}
catch(Exception e)
{
logger.error("ActionServlet: PANIC! unable to init() - "+e);
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doRequest(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doRequest(request, response);
}
/**
* 处理用户请求
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Map map=new HashMap();
map.put(ActionContext.HTTP_REQUEST,request);
map.put(ActionContext.HTTP_RESPONSE,response);
ActionContext.setContext(new ActionContext(map));
if(!UserConnectManage.checkLoginValidate(request.getRemoteAddr(),"guest"))
{
info(request,response,new Exception("您对页面的刷新太快,请等待"+UserConnectManage.getWaitInterval()/1000+"秒后再刷新页面!"));
return;
}
if(WebConfig.getInstance().isDebug()){//调试模式每次都要初始化配置文件
WebConfig.getInstance().init();//初始化配置文件
initTemplate(getServletConfig()); //初始化模版
}
Context context = null;
try
{
setContentType(request, response);
RequestProcessor rp=new RequestProcessor(this);
rp.process(request,response);
}
catch (Exception e)
{
logger.error("ActionServlet: Exception processing the template: "+e);
error(request, response, e);
}
finally
{
requestCleanup(request, response, context);
}
}
protected void requestCleanup(HttpServletRequest request,
HttpServletResponse response,
Context context)
{
}
protected void setContentType(HttpServletRequest request,
HttpServletResponse response)
{
response.setContentType(defaultContentType);
}
/**
* 输出系统框架错误信息提示
* @param request
* @param response
* @param e
* @throws ServletException
*/
protected void error(HttpServletRequest request,
HttpServletResponse response,
Exception e)
throws ServletException
{
try
{
StringBuffer html = new StringBuffer();
String title=request.getCharacterEncoding()!=null?"EasyJWeb框架错误":"EasyJWeb Framework error";
html.append("<html>\n");
html.append("<head><title>"+title+"</title></head>\n");
html.append("<body>\n");
html.append(title+":\n<br>");
Throwable cause = e;
String why = cause.getMessage();
html.append("<font color=red>");
if (why != null && why.trim().length() > 0)
{
html.append(why);
html.append("\n<br>详细请查询<a href='http://www.easyjf.com/' target='_blank'>http://www.easyjf.com</a>\n");
}
if (cause instanceof MethodInvocationException)
{
cause = ((MethodInvocationException)cause).getWrappedThrowable();
}
html.append("</font>");
StringWriter sw = new StringWriter();
cause.printStackTrace(new PrintWriter(sw));
html.append("<pre>\n");
html.append(sw.toString());
html.append("</pre>\n");
html.append("</body>\n");
html.append("</html>");
if(request.getCharacterEncoding()!=null)response.setContentType("text/html; charset="+request.getCharacterEncoding());
getResponseWriter(response).write(html.toString());
}
catch (Exception e2)
{
logger.error("ActionServlet: Exception while printing error screen: "+e2);
throw new ServletException(e);
}
}
protected void info(HttpServletRequest request,
HttpServletResponse response,
Exception e)
throws ServletException
{
try
{
StringBuffer html = new StringBuffer();
String title=request.getCharacterEncoding()!=null?"EasyJWeb框架友情提示!:-)":"EasyJWeb Framework Friendly Info!";
html.append("<html>\n");
html.append("<head><title>"+title+"</title></head>\n");
html.append("<body>\n");
html.append(title+":\n<br>");
Throwable cause = e;
String why = cause.getMessage();
html.append("<font color=red>");
if (why != null && why.trim().length() > 0)
{
html.append(why);
html.append("\n<br>详细请查询<a href='http://www.easyjf.com/' target='_blank'>http://www.easyjf.com</a>\n");
}
if (cause instanceof MethodInvocationException)
{
cause = ((MethodInvocationException)cause).getWrappedThrowable();
}
html.append("</font>");
html.append("</body>\n");
html.append("</html>");
if(request.getCharacterEncoding()!=null)response.setContentType("text/html; charset="+request.getCharacterEncoding());
getResponseWriter(response).write(html.toString());
}
catch (Exception e2)
{
logger.error("ActionServlet: Exception while printing error screen: "+e2);
throw new ServletException(e);
}
}
protected Writer getResponseWriter(HttpServletResponse response)
throws UnsupportedEncodingException, IOException
{
Writer writer = null;
try
{
writer = response.getWriter();
}
catch (IllegalStateException e)
{
if (this.warnOfOutputStreamDeprecation)
{
this.warnOfOutputStreamDeprecation = false;
}
String encoding = response.getCharacterEncoding();
if (encoding == null)
{
encoding = DEFAULT_OUTPUT_ENCODING;
}
writer = new OutputStreamWriter(response.getOutputStream(),encoding);
}
return writer;
}
public static SimplePool getWriterPool() {
return writerPool;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -