我是想在查询记录时记录日志,在Log类获取ip和日志文件路径
但是使用ServletActionContext会出异常,请问改如何解决 

解决方案 »

  1.   

    ServletActionContext.getServletContext()
    这样能取到servletContext?
      

  2.   

    ServletActionContext.getRequest(); 获取方法没错,会不会是别的原因?
      

  3.   

    应该不会,因为如果我把
    String path = ServletActionContext.getServletContext().getRealPath("log") + File.separator + "xzy" + ".log";  
    String ip = ServletActionContext.getRequest().getRemoteAddr();
    这两个改成
    String path ="d:\\xxx.log";
    String ip = "127.0.0.1";
    就没有问题
    这里需要继承什么吗?
      

  4.   

    log4j.properties
    log4j.rootLogger =DEBUG,logFile
    log4j.appender.logFile =org.apache.log4j.RollingFileAppender
    log4j.appender.logFile.File=f:/log/log.log
    log4j.appender.logFile.MaxFileSize=5120KB
    log4j.appender.logFile.MaxBackupIndex=10   
    log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logFile.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
    java code: @SuppressWarnings("unchecked")
    public String execute() throws Exception {
    if (logger.isDebugEnabled()) {
    logger.debug("execute() - start");
    } user = userService.getSingleUser(userName);
    if (password.equals(user.getPassword())) {
    ActionContext.getContext().getSession().put("user", user);
    if (logger.isDebugEnabled()) {
    logger.debug("execute() - end");
    }
    return SUCCESS; } else {
    if (logger.isDebugEnabled()) {
    logger.debug("execute() - end");
    }
    return ERROR;
    } }
      

  5.   

    加入log4j的jar包在src下建一个log4j.properties文件具体内容在上面在需要加log的地方用
    logger.debug("execute() - start");

    logger.debug("execute() - end");
    括起来运行程序 到路径下的log文件就可以看log了如:
    log4j.appender.logFile.File=f:/log/log.log楼主可以修改成自己的路径
      

  6.   

    呵呵,我说错了!
    能帮我看下我的日志代码怎么错了吗?
    不是加log4j
      

  7.   

    实现ServletContextAware这个接口呢?
      

  8.   

    String ip = ServletActionContext.getRequest().getRemoteAddr();   //NullPointException这里换成ServletActionContext.get("request");就ok了
      

  9.   

    实现ServletContextAware也不行
    ServletActionContext.get("request");
    没有这个方法?
      

  10.   

    我给你敲段原文“ActionContext类没有提供类似的getRequest()方法来获取封装了HttpServletRequest
    的Map对象,要得到请求Map对象,需要为get()方法传递“request” ”
      

  11.   

    在MyEclipse就显示错误了
    Cannot make a static reference to the non-static method get(Object) from the 
     type ActionContext
      

  12.   

    晕 原来你用的是ServletActionContext这个是与Servlet耦合的我再看看
      

  13.   

    啊 是取不到IP  我上网看了一圈  说有的防火墙导致getRemoteAddr()不能正常取得IP楼主把防火墙关掉试试
      

  14.   

    啊 是取不到IP  我上网看了一圈  说有的防火墙导致getRemoteAddr()不能正常取得IP楼主把防火墙关掉试试或者你用  .getRemoteHost() 方法取一下试试 
      

  15.   

    谢谢各位!
    应该是我对ssh了解不多的原因
    自己换种方式吧,多学习。