【发布时间】:2014-10-11 07:56:24
【问题描述】:
我正在使用 JSP 和 SERVLET 开发 WEB PROJECT。
我正在尝试使用调用的 AJAX 上传文件。 文件已成功上传。 但是,当调用控制器(servlet 文件)的 ajax 向 jsp 文件发送请求和响应对象时。
JSP 文件
$(document).ready(function(){
$(':file').change(function(){
var fileObj = this.files[0];
var form = $('#mOBJ');
var fd = new FormData();
fd.append( 'file', fileObj);
$.ajax({
url:form.attr('action'),
type:form.attr('method'),
data:fd,
processData: false,
contentType: false,
async:false,
}).done(function(){
alert('ajax complete');
/////////////////////////////////////// //////////////// 在这里,我得到了旧的请求值。 我想要在调用 ajax 后新的请求和响应对象。
var Check2Bool = <%=context.getAttribute("comeFromUploadTemp")%>;
var check2 = <%=request.getAttribute("comeFromUploadTemp")%>;
alert(Check2Bool + " " + check2);
}).fail(function() {
alert( "error" );
$('#ldiv').hide();
});
});
小服务程序
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
/**
* Display XML file to the user
*/
TemplateVO template = null;
TemplateUtil util = new TemplateUtil();
//Prepare XML file path
String path=(String) context.getAttribute(Constants.USER_DIR);
String strFilePath = null;
//Upload XML file
if(ServletFileUpload.isMultipartContent(request)){
try{
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
request.setAttribute("FileName",name);
String path1 = path + File.separator + "data-in";
strFilePath = FileUtil.createFileOnFileSystem(item,path1,name,"xml");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
//set variables into Request Object
template = util.readXML(strFilePath);
context.setAttribute("comeFromUploadTemp", true);
request.setAttribute("comeFromUploadTemp", true);
request.getRequestDispatcher("mappingObject.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
【问题讨论】:
标签: java jquery ajax jsp servlets