2014-11-07 3 views
0

안녕하세요 저는 동적 웹 프로젝트를 진행하고 있습니다.완료라고하는 아약스 이후 컨텍스트 개체가 업데이트되지 않았습니다.

파일은 jsp 페이지에서 선택합니다. 아약스가 호출됩니다. 서블릿으로 이동하십시오. 및 컨텍스트 특성을 설정하십시오.

샘플 코드를 추가합니다. 아약스를 사용하여 컨텍스트 개체를 업데이트하는 방법과 관련된 아이디어가있는 경우 ...

컨텍스트 개체가 업데이트되지 않습니다.

JSP 페이지

<html> 
     <script> 
     $(document).ready(function(){ 
      $(':file').change(function(){ 
       var fileObj = this.files[0]; 
       var fd = new FormData();  
       fd.append('file', fileObj); 
       var form = $('#f1'); 
       alert(<%=context.getAttribute("uploadFile")%>); //false 
       $.ajax({ 
         url:form.attr('action'), 
         type:form.attr('method'), 
         data:fd, 
         processData: false, 
         contentType: false, 
         async:false, 

        }).done(function(data){ 
         alert('ajax complete'); 
         //context object is not updated after ajax called... 
         alert(<%=context.getAttribute("uploadFile")%>); //false 
         <%context = getServletContext();%> 
         alert(<%=context.getAttribute("uploadFile")%>); //false 

        }); 
      }); 
     }); 
    </script> 
    <form name="f1" id="f1" action="/test" method="post"> 
     <input type="file"/> 
    </form> 
</html> 

서블릿 코드

if(ServletFileUpload.isMultipartContent(request)){ 
       try{ 
        List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); 
        for(FileItem item : multiparts){ 
         if(!item.isFormField()){ 
          //File upload logic 
         } 
        } 
       }catch(Exception ex){ 
        ex.printStackTrace(); 
       } 
       context.setAttribute("uploadFile",true); //set context attribute 

답변

0

SerlvetContext는 서버 측 객체입니다. 자바 스크립트는 클라이언트에서 실행되며 이에 대한 액세스 권한이 없습니다. 당신은 당신의 서블릿이 JQuery와 함수에 전달 된 데이터 개체에서 사용할 수 있습니다 응답의 어떤 종류 (일반 문자열, JSON 또는 XML)을 보내도록해야합니다

.done(function(data){ 
alert(data); 

}); 

를 보낼 서블릿에 일부 코드를 추가합니다 응답 :

public void doPost(HttpServletRequest request, 
        HttpServletResponse response) 
      throws ServletException, IOException 
    { 
     // Set response content type 
     response.setContentType("text/html"); 

     PrintWriter out = response.getWriter(); 
     out.println("File Uploaded"); 
    } 
관련 문제