http://chaoji-liangbin.blog.163.com/blog/static/25239212201061192240909/
对应的js代码:
function deleteSelectItem(oSelect)
{
var delItem = "";
for(var i=0; i<oSelect.options.length; i++)
{
if(i>=0 && i<=oSelect.options.length-1 && oSelect.options[i].selected)
{
delItem = oSelect.options[i].value;
oSelect.options[i] = null;
i --;
}
}
var http_request = false;
send_request("DeleteFile.jsp?path="+delItem);
//向服务器发起XMLHTTP请求
function send_request(url)
{
//初始化,指定处理函数,发送请求的函数
http_request = false;
//开始初始化XMLHTTPRequest对象
if(window.ActiveXObject)
{ //IE浏览器
try{
http_request = new ActiveXObject('Msxm12.XMLHTTP');
}catch(e)
{
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e)
{
alert(e);
}
}
//alert("IE");
}else if(window.XMLHttpRequest)
{ //Mozilla浏览器
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType)
{ //设置MIME类别
http_request.overrideMimeType('text/xml');
}
}
if(!http_request){ //有异常,创建对象实例失败
window.alert("不能创建XMLHTTPRequest对象");
return false;
}
http_request.onreadystatechange = processRequest; //这里只是指向方法,并未执行方法
url = encodeURI(url);
url = encodeURI(url);//两次,很关键
//确定发送请求的方式和URL以及是否执行以下代码
http_request.open("POST",url,true);
http_request.setrequestheader("Content-Type","application/x-www-form-urlencoded");
http_request.send(null);
}
//处理返回信息的函数
function processRequest(){
if(http_request.readyState == 4){ //判断对象状态
if(http_request.status==200){ //信息已经成功返回
var returnStr = http_request.responseText;
document.getElementById("feedback").innerHTML=returnStr;
}else{
alert("你所请求的页面有异常");
}
}
}
}
DeleteFile.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.File"%>
<%@page import="java.net.URLDecoder"%>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<%
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String filepath = URLDecoder.decode(request.getParameter("path"),"UTF-8");
try {
File f = new File(filepath);
if (f.isFile()) {
f.delete();
out.println("删除 " + f.getName() + " 成功");
} else {
out.println("ERROR,选择的不是一个有效的文件");
}
} catch (Exception e) {
out.println("ERROR,文件读取异常!");
}
%>
FileConfig.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.File"%>
<%!File[] configfiles;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>配置文件管理</title>
<link href="../images/common.css" rel="stylesheet" type="text/css"></link>
<!-- <link href="../images/index.css" rel="stylesheet" type="text/css" />-->
<style type="text/css">
<!--
ol,ul ,li {
list-style: none;
}
select {
width: 200px;
}
img {
border: 0;
}
.clear {
clear: both;
height: 1px;
margin-top: -1px;
width: 100%;
}
/*此上面代码是初始CSS模板,下面是新写CSS布局框架代码*/
#header ,#centers ,#footer {
width: 100%;
margin: 0 auto;
clear: both;
font-size: 18px;
font-weight: bold;
}
#header {
height: 20px;
line-height: 32px;
}
#centers {
padding: 8px 0;
height: 160px;
}
#footer {
}
#centers .c_left {
float: left;
width: 200px;
background: #F7F7F7;
margin: 0,0,0,20%;
}
#centers .c_right {
float: left;
width: 80px;
margin: 0,0,0,2px;
padding: 0px;
}
.button {
border: 1px solid black;
width: 80px;
}
-->
</style>
<script type="text/javascript" src="../js/main.js"></script>
</head>
<body>
<%
try {
String filepath = application.getRealPath("/WEB-INF/classes/");
if (filepath.indexOf("//.//") != -1) {
int i = filepath.indexOf("//.//");
filepath = filepath.substring(0, i)
+ filepath.substring(i + 2, filepath.length());
}
//System.out.println("配置文件路径:"+filepath);
File configdir = new File(filepath + "/config");
if (configdir.isDirectory()) {
configfiles = configdir.listFiles();
}
//System.out.println("configdir:" + configdir.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
%>
<form action="index.jsp" method="post">
<div id="header">
文件管理
</div>
<div id="centers">
<div class="c_left">
<SELECT size="10" id="select" name="select" multiple="single">
<%
if (configfiles != null) {
for (File xmlfile : configfiles) {
%>
<option value="<%=xmlfile.getAbsolutePath()%>">
<%=xmlfile.getName()%>
</option>
<%
}
}
%>
</SELECT>
</div>
<div class="c_right">
<INPUT class="button" type="submit" value=" 打 开 " >
<INPUT class="button" type="button" value=" 删 除 " onClick="deleteSelectItem(document.all.select);">
</div>
</div>
<div id="footer">
<label id="feedback"></label>
</div>
</form>
</body>
</html>
为了支持 send_request("DeleteFile.jsp?path="+delItem);参数可以传中文以及特殊字符(""<>等),有两个地方需要注意,一是 url = encodeURI(url);
url = encodeURI(url);//两次,很关键
两次encodeURI很重要
二是.在发送到得jsp页面里
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String filepath = URLDecoder.decode(request.getParameter("path"),"UTF-8");
这三句话也是非常重要的,得到的字符串才是原始的字符串,连中文和特殊字符都可以传,什么都可以传。