本帖最后由 yangfuchao418 于 2010-06-27 17:05:41 编辑

解决方案 »

  1.   

    这个不行,这个是第三方软件,我要的是利用jsp本身的代码
      

  2.   

    第三方软件?呵呵,那你就用java读取文件,每次将内容追加到文件尾部
      

  3.   

    logger.info("值");jsp 中post 获的值,你会吗?
      

  4.   

    不会,本人对jsp一点不懂,所以才来发贴求助
      

  5.   


    我是要把post数据包的内容或者post包里面的参数保存到一个日志文件里,关键是获取post提交的参数的值,并且保存到文件,文件操作只占核心1/4
      

  6.   

    获取post提交的参数值?用request.getParameter("filename");获取不到?不理解。
      

  7.   


    别人都说了。别人不懂JSP。
      

  8.   

    你想得出什么东西到底?
    用户上传的参数?还是head里面的信息?
      

  9.   

     用jsp获取撒
       这个简单一看就OK
      

  10.   

    是的,request.getParameter("filename");这样取到值,再把值写入文件再说一下,我不懂JSP,所以麻烦帮我写出来了...
      

  11.   

    [code=JSP]<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@page import="java.io.File"%>
    <%@page import="java.io.PrintWriter"%>
    <%@page import="java.io.FileOutputStream"%>
    <%@page import="java.text.SimpleDateFormat"%>
    <%@page import="java.io.FileWriter"%>
    <%@page import="java.io.IOException"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     String logtpath = request.getSession().getServletContext().getRealPath("")+"/1.log"; //得到tomcat容器下web路径
     String filename = request.getParameter("");
     filename = "c:\\xxx.txt";
     String time ="20000909";
     SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     StringBuffer context = new  StringBuffer("记录时间:");
     context.append(sd.format(new Date()));
     context.append("\n");
     context.append("filename:");
     context.append(filename);
     context.append("\n");
     context.append("time:");
     context.append(time);
     context.append("-----------------------------------------------");
      File outFile = new File(logtpath); 
          try{
          if(!outFile.exists()){ 
            //如果没有这个文件 先创建
    outFile.createNewFile();
    PrintWriter ow = new PrintWriter(new FileOutputStream(outFile)); 
        ow.write(context.toString());
        ow.close(); 
          }else{ 
             //如果有这个文件,则追加内容至结尾
           FileWriter writer = new FileWriter(outFile, true);
           writer.write("\n"+context);
           writer.close();
          }
          }catch(IOException e){
           e.printStackTrace();
          }
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'MyJsp.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        This is my JSP page. <br>
      </body>
    </html>
    [/code]
      

  12.   

    String filename = request.getParameter("filename"); 
    String time= request.getParameter("time"); 
    替换掉那几行,虽然这样写了,但建议楼主不要这样写!文件操作写在前台是不理智的
      

  13.   

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
    <%@page import="java.io.File"%> 
    <%@page import="java.io.PrintWriter"%> 
    <%@page import="java.io.FileOutputStream"%> 
    <%@page import="java.io.FileWriter"%> 
    <%@page import="java.io.IOException"%> 
    <% 
    String logtpath = "/root/1.log"; //linux系统下的保存文件路径String filename = request.getParameter("filename");
    String time= request.getParameter("time");  context.append(filename); 
    context.append(":"); 
    context.append(time); 
    context.append("/n"); File outFile = new File(logtpath); 
          try{ 
          if(!outFile.exists()){ 
          //如果没有这个文件 先创建 
    outFile.createNewFile(); 
    PrintWriter ow = new PrintWriter(new FileOutputStream(outFile)); 
        ow.write(context.toString()); 
        ow.close(); 
          }else{ 
            //如果有这个文件,则追加内容至结尾 
          FileWriter writer = new FileWriter(outFile, true); 
          writer.write("\n"+context); 
          writer.close(); 
          } 
          }catch(IOException e){ 
          e.printStackTrace(); 
          } 
    %> 虽然我不会jsp,但是我懂得其他语言,所以看的懂,就粗略的改了下
    获取提交的filename和time,保存到系统的/root/1.log(我用的linux系统,所以这个路径)
    保存的文件的内容实例为:
    c:\1.txt:20090901
    c:\2.txt:20090902以上是我改了之后的期望效果xiayuqijava麻烦你看我这样是否有错误
      

  14.   

    String logtpath = "/root/1.log"; //linux系统下的保存文件路径  确定下这里的路径,如果想写死存储在服务器上某个地址的话,就写完整地址 .StringBuffer context = new StringBuffer();  //字符串对象, 这里要创建StringBuffer对象,不然你下面那些context.append(filename); .....会报错
    context.append("/n"); 
     这个只是换行符,如果需要换行写成\n,不需要就删掉
      

  15.   

    恩,路径是对的
    \n刚才笔误,写成/n了下面是我的最终版本,麻烦大侠再看看最终版本:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
    <%@page import="java.io.File"%> 
    <%@page import="java.io.PrintWriter"%> 
    <%@page import="java.io.FileOutputStream"%> 
    <%@page import="java.io.FileWriter"%> 
    <%@page import="java.io.IOException"%> 
    <% 
    String logtpath = "/root/1.log";String filename = request.getParameter("filename");
    String time= request.getParameter("time");
    StringBuffer context = new StringBuffer();
    context.append(filename); 
    context.append(":"); 
    context.append(time); File outFile = new File(logtpath); 
          try{ 
          if(!outFile.exists()){ 
          //如果没有这个文件 先创建 
    outFile.createNewFile(); 
    PrintWriter ow = new PrintWriter(new FileOutputStream(outFile)); 
        ow.write(context.toString()); 
        ow.close(); 
          }else{ 
            //如果有这个文件,则追加内容至结尾 
          FileWriter writer = new FileWriter(outFile, true); 
          writer.write("\n"+context); 
          writer.close(); 
          } 
          }catch(IOException e){ 
          e.printStackTrace(); 
          } 
    %> 
      

  16.   

    基本没问题了,路径正确,参数能得到的话就可以写入1.log了,另外\n换行直接打开log文件是看不到换行的,只有在程序读取时\n显示了换行
      

  17.   

    刚才<%@page import="java.text.SimpleDateFormat"%> 被我删了,这个你看我还需要不
      

  18.   

    也就是StringBuffer context = new StringBuffer();用到<%@page import="java.text.SimpleDateFormat"%>不,如果不用到,我就不需要这个东东了
      

  19.   

    使用log4j,你在后台获取参数值,这个应该很简单,然后你可以使用log4j,将值可以保存制定的文件中,这个可以查看1og4j的配置
      

  20.   

    已经测试成功,另外在linux下,路径/root/1.log要写成//root//1.log
    /n在windows下显示是不换行,但是是linux下的换行符,用windows下的写字板打开1.log可以实现换行的效果