日期:2014-05-16  浏览次数:20336 次

JSP获取所有参数并追加写入文件

JSP获取所有参数并追加写入文件

?

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String filename = "moParam.text";
    String path=request.getRealPath("");
    filename = path+"\\"+ filename ;
    String conent ="";
    
%>
<?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> 
    <%=filename %> | Parameter:<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);
	    writer.write(conent);
	    writer.close();
	} catch (java.io.IOException e) {
	    e.printStackTrace(); 
	} finally {
	     
	}

%>

?