【问题标题】:How to avoid servlet responding with new html page?如何避免 servlet 响应新的 html 页面?
【发布时间】:2014-02-26 12:42:25
【问题描述】:

以下是我的 servlet 的 get() 和 post() 方法。我对 servlet 很陌生。当我从客户端调用时,此 servlet 创建一个新页面并覆盖客户端中的所有 html 元素。我想做的只是留在我的html页面中,让servlet独自完成他的工作。

public void doPost(HttpServletRequest request, 
               HttpServletResponse response)
              throws ServletException, java.io.IOException {
      // Check that we have a file upload request
      isMultipart = ServletFileUpload.isMultipartContent(request);

      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("c:\\temp"));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(request);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fieldName = fi.getFieldName();
            String fileName = fi.getName();
            String contentType = fi.getContentType();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( filePath + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            fi.write( file ) ;

         }
      }

   }catch(Exception ex) {
       System.out.println(ex);
   }
   }
   public void doGet(HttpServletRequest request, 
                       HttpServletResponse response)
        throws ServletException, java.io.IOException {

        throw new ServletException("GET method used with " +
                getClass( ).getName( )+": POST method required.");
   } 

【问题讨论】:

  • @sᴜʀᴇsʜᴀᴛᴛᴀ 更改标题并感谢您的回答。
  • 很高兴为您提供帮助 :) 如果您觉得有帮助,请将其标记为答案。

标签: java html jsp servlets


【解决方案1】:

如果您希望 servlet 进行处理并显示 html 页面,那么您可以在 servlet 中的 try 块末尾使用 RequestDispatcher。因此,当单击 html 页面中的任何操作时,它将转到 servlet 并执行适当的操作,然后再次重定向到 html。

RequestDispatcher rd = request.getRequestDispatcher("index.html");
            rd.include(request, response);

【讨论】:

    【解决方案2】:

    我想做的只是留在我的html页面中,让servlet独自完成他的工作。

    这是使用 Asynchronous call AKA Ajax 的完美候选。

    学习 AJAX。

    http://api.jquery.com/jquery.ajax/

    How to use Servlets and Ajax?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2016-05-08
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      相关资源
      最近更新 更多