在我的上一篇文章中说到,将DB4O的DAO类放入到ServeltContext中去,然后我们在struts2的ACTION中要去拿到这个DAO必然要得到servletcontext;
方法:
在ACTION中实现implements ServletContextAware。
然后实现该类的方法:
public static ServletContext context;
public void setServletContext(ServletContext context) {
// TODO Auto-generated method stub
this.context = context;
}
这样就可以在ACTION中得到servletcontext了。为什么在这我要用静态的属性来存放呢?
因为在我的该ACTION中,我用到了拦截器,拦截器的代码如下:
package common.interceptor;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import com.login.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import common.util.db4o.LogMessageDao;
public class LogInterceptor extends AbstractInterceptor {
private Logger logger = Logger.getLogger(this.getClass().getName());
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//logger.info("拦截器-"+this.name+":开始记录日志");
String LogFileName="";
LoginAction action = (LoginAction)invocation.getAction();
//通过拦截器得到了LoginAction的引用。然后通过action.context就可以得到servletcontext了,然后从context取出DAO的实例
String result = invocation.invoke();
HttpServletRequest request = action.getRequest();
// **************日期******************************
java.util.Date date = new java.util.Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strDate = df.format(date); //当天日期
/***获得远程IP***/
String remoteIP=request.getRemoteAddr();
String logstr = action.getPerID()+";登陆IP:"+remoteIP+"登陆时间:"+strDate;
LogFileName=strDate.substring(0,10);
logger.info(logstr);
//改段代码可以将登陆日志记录到文件中Log.writeFile(logstr, request.getRealPath(".") + "\\log\\"+LogFileName+".log",true);
//DB4oUtil db = new DB4oUtil();
LogMessageDao db = (LogMessageDao)action.context.getAttribute("LogDao");
db.save(request.getRealPath(".")+"\\log\\log.db", action.getPerID(),remoteIP);
return result;
}
}
分享到:
相关推荐
Struts2执行流程 1. web.xml 部署描述符 2. FilterDispatcher 实现StrutsStatics, Filter接口 (1)Filter:一个filter是一个对象用于执行过滤任务为每个请求资源(一个servlet或静态内容),或响应一个资源,或两者.过滤...
这是servlet中ServletContext的应用实例,主要就是添加属性和读取属性。
ServletContext代码,开发工具 Myeclipse
ServletContext读取web应用中的资源文件
Ssh获取ServletContext 只需要更改struts-config action中的type就可以了ru :type="com.uo.spring.SpringDelegatingActionProxy" 大家有什么疑问可以直接去我的blog查看http://blog.csdn.net/chen1255/ 《[正解]Ssh...
ServletContext与ServletConfig关系
AnyFo - Util - ... AnyFoAction中的那个类,提供多个方法来访问Servlet中的API,这些方法能提供的功能大致如下: 1.获得HttpServletResponse 2.获得HttpSession 3.获得ServletContext,即俗称的Application
网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...
对servletContext的学习,并使用实例进行了实战。完成了一个登录和统计登录人数的功能。
Struts 的执行 RequestUtils.selectModule(String prefix, HttpServletRequest,ServletContext)方法 RequestUtils.selectModule(HttpServletRequest, ServletContext) getModuleConfig...
在MyEclipse环境下运用Struts2技术访问Servlet API,包括HttpServletRequest、HttpSession和ServletContext
quartz servletcontext quartz servletcontext配置资料quartz servletcontext配置资料
本篇文章是对ServletContext中的常用方法进行了详细的分析介绍,需要的朋友参考下
超全面javaweb教程28天第9天_14_获取ServletContext对象
NULL 博文链接:https://onway417.iteye.com/blog/2194540
超全面javaweb教程28天第9天_17_ServletContext获取资源相关方法
注意:每个servlet有一个ServletConfig,每个Web应用有一个ServletContext.要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变...
介绍 ServletContext对象的一些重要方法的示例代码
博客地址:...存放数据指的是这三个对象在工程中setAttribute(),removeAttribute()的时候,我们的监听器就会知道,然后在对应的方法中执行相应的操作。
* 从ServletContext容器根目录拿去相关资源文件,它被result(type="stream")stream指定类调用,因此在 * result中要指定关联关系<param name="inputName">inputStream * */ System.out.println(...