在JSP获取所有参数并写入文件(追加写入)
在JSP获取所有参数并写入文件(追加写入)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String filename = "moParam.text";
String path=request.getRealPath("");
filename = path+"\\"+ filename ;
String conent ="";
java.io.File f = new java.io.File(filename);
//if(!f.exists()) { f.createNewFile(); }
%>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
<meta http-equiv="Cache-control" content="no-cache" />
<title>获取参数</title>
</head>
<body>
<%
java.text.SimpleDateFormat sdfLongTimePlusMill = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS");
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
String nowDate = sdfLongTimePlusMill.format(date);
%>
=================================<%=nowDate %> ==========================================<br/>
<%
String sesstionStr = session.getId();
java.util.Enumeration e1 = request.getParameterNames();
while(e1.hasMoreElements()){
String paramName = (String)e1.nextElement();
String paramValue = (String)request.getParameter(paramName);
// 处理参数名paramName与参数值paramValue
conent = conent+"\\r\\n"+sesstionStr+":"+paramName+" = "+paramValue+"\\r\\n" ;
}
%>
Header<br/>
<%
//获取请求头信息
java.util.Enumeration e2 = request.getHeaderNames();
while (e2.hasMoreElements()) {
String name = (String)e2.nextElement();
String value = request.getHeader(name);
conent = conent+"\r\n"+sesstionStr+":"+name + "=" + value+"\r\n" ;
}
%>
<br/><br/>User-Agent : <%=request.getHeader("User-Agent")%><br/>
<br/><br/>x-up-bear-type : <%=request.getHeader("x-up-bear-type")%><br/>
<br/><br/>x-forwarded-for : <%=request.getHeader("x-forwarded-for")%><br/>
<br/><br/>RemoteAddr : <%=request.getRemoteAddr()%><br/><br/>
文件会自动追加内容,操作完后下载:
<a href="/moParam.text" title="" >下载文件</a>电脑访问,右键文件另存为.
</body>
</html>
<%
conent = conent+"\r\n"+sesstionStr+": IP =" + request.getRemoteAddr()+"\r\n";
conent = conent+"\r\n"+sesstionStr+": User-Agent =" + request.getHeader("User-Agent")+"\r\n";
conent = conent+"\r\n"+sesstionStr+": x-up-bear-type =" + request.getHeader("x-up-bear-type")+"\r\n";
conent = conent+"\r\n"+sesstionStr+": x-forwarded-for=" + request.getHeader("x-forwarded-for")+"\r\n";
%>
<%
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
java.io.FileWriter writer = new java.io.FileWriter(filename, true);
String xconent = "=================================" +nowDate+"=================================" ;
writer.write(xconent+"\r\n"+conent);
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
}
%> --------------------编程问答-------------------- 好像最近越来越多的网友喜欢在 jsp 里完成所有业务逻辑了 --------------------编程问答-------------------- 这是一朋友要写的东西。
如果我还搞个环境比如,MVC,写数据库,mybaits...Ajax.....
我朋友那没有运行环境。
不过,现在框架用多了。真有点怀念JSP。 --------------------编程问答-------------------- 这是分享吗 ? no perfect
补充:Java , Web 开发