求救!!!记录提交的参数的值!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

yangfuchao418 2010-06-27 04:59:15
upload.jsp脚本有2个参数,如下:
filename
time

客户提交upload.jsp脚本,发送的post参数如下:
filename=c:\xxx.txt&time=20000909
等同于get方式的http://www .xxx.com/upload.jspfilename=c:\xxx.txt&time=20000909
不过我这里用的是post,所以代码必须是针对post的

我想要做的是把这2个参数的值记录到1.log这个文件里,请帮我写出这段代码,谢谢了!!
...全文
211 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
tao397145214 2010-06-28
  • 打赏
  • 举报
回复
用jsp获取撒
这个简单一看就OK
dr_lou 2010-06-28
  • 打赏
  • 举报
回复
你想得出什么东西到底?
用户上传的参数?还是head里面的信息?
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wangjin9805 的回复:]

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

别人都说了。别人不懂JSP。
wangjin9805 2010-06-28
  • 打赏
  • 举报
回复
获取post提交的参数值?用request.getParameter("filename");获取不到?不理解。

yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
已经测试成功,另外在linux下,路径/root/1.log要写成//root//1.log
/n在windows下显示是不换行,但是是linux下的换行符,用windows下的写字板打开1.log可以实现换行的效果
Arthur0088 2010-06-28
  • 打赏
  • 举报
回复
使用log4j,你在后台获取参数值,这个应该很简单,然后你可以使用log4j,将值可以保存制定的文件中,这个可以查看1og4j的配置
colin_pxx 2010-06-28
  • 打赏
  • 举报
回复
request.getParameter("filename")啊
xiayuqijava 2010-06-28
  • 打赏
  • 举报
回复
那个只是为了记录上传时间随手写的,用不上都去掉吧
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
也就是StringBuffer context = new StringBuffer();用到<%@page import="java.text.SimpleDateFormat"%>不,如果不用到,我就不需要这个东东了
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
刚才<%@page import="java.text.SimpleDateFormat"%> 被我删了,这个你看我还需要不
xiayuqijava 2010-06-28
  • 打赏
  • 举报
回复
基本没问题了,路径正确,参数能得到的话就可以写入1.log了,另外\n换行直接打开log文件是看不到换行的,只有在程序读取时\n显示了换行
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
恩,路径是对的
\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();
}
%>
xiayuqijava 2010-06-28
  • 打赏
  • 举报
回复
String logtpath = "/root/1.log"; //linux系统下的保存文件路径 确定下这里的路径,如果想写死存储在服务器上某个地址的话,就写完整地址 .

StringBuffer context = new StringBuffer(); //字符串对象, 这里要创建StringBuffer对象,不然你下面那些context.append(filename); .....会报错


context.append("/n");
这个只是换行符,如果需要换行写成\n,不需要就删掉
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
<%@ 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麻烦你看我这样是否有错误
myhope88 2010-06-28
  • 打赏
  • 举报
回复
路过,来学习下
j f
xiayuqijava 2010-06-28
  • 打赏
  • 举报
回复
String filename = request.getParameter("filename");
String time= request.getParameter("time");


替换掉那几行,虽然这样写了,但建议楼主不要这样写!文件操作写在前台是不理智的
xiayuqijava 2010-06-28
  • 打赏
  • 举报
回复
[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]
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dr_lou 的回复:]
你想得出什么东西到底?
用户上传的参数?还是head里面的信息?
[/Quote]


要的是参数
yangfuchao418 2010-06-28
  • 打赏
  • 举报
回复
是的,request.getParameter("filename");这样取到值,再把值写入文件

再说一下,我不懂JSP,所以麻烦帮我写出来了...
加载更多回复(8)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧