【问题标题】:how to get new request object after ajax calledajax调用后如何获取新的请求对象
【发布时间】: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


    【解决方案1】:

    请求是 JSP 工作空间,notajax。 您应该通过响应 i.o. 传递数据。要求。 喜欢:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //... your code here
         response.getOutputStream().write(data);
    
    }
    

    然后你可以根据需要解析响应并获取数据。

    http://www.coderanch.com/t/522069/Servlets/java/Adding-Custom-Data-Response-Headers http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多