action 执行servlet怎样用ajax让它保持在当前页面
主要用来往服务器上传文件的
jsp:
<form id="form1" name="form1" method="POST" action="<%=request.getContextPath()%>/FileUploadServlet" enctype="multipart/form-data" class="L5form" target= "_blank ">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="FieldLabel" width="15%" nowrap="nowrap">文件上传</td>
<td class="FieldInput" width="35%" nowrap="nowrap">
<input name="file" type="file" size="20" title="选择要上传的文件" style="width:90%">
</td>
</tr>
</table>
</form>
js:
function save(){
document.form1.submit();
}
servlet:
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7744625344830285257L;
private ServletContext sc;
private String savePath;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
doPost(request, response);
}
public void init(ServletConfig config) {
// 在web.xml中设置的一个初始化参数
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String Msg="";
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
//System.out.println("上传文件的大小:" + item.getSize());
//System.out.println("上传文件的类型:" + item.getContentType());
// item.getName()返回上传文件在客户端的完整路径名称
// System.out.println("上传文件的名称:" + item.getName());
File tempFile = new File(item.getName());
//上传文件的保存路径
File file = new File