1

为简单起见,我有一个带有小表单的 jsp 文件,其中包含 2 个选择选项、一个文件上传选项和一个提交按钮。servlet 接收值,处理它们(即将文件放入数据库),然后通过请求属性显示消息并重定向回原始 jsp 页面。

我的问题是,当用户尝试从同一个 jsp 页面上传另一个文件时,servlet 正在接收从先前提交中选择的选择值,而不是新的提交尝试。在 servlet 已经重定向回 jsp 之后,同一个用户如何上传另一个具有不同选择选项值的文件?

同样,如何才能清除从第一次提交时打印的成功消息,直到发生另一次成功上传?目前,即使在用户开始选择要提交的新值之后,成功消息也会保留在页面上。

以下是相关代码:

JSP 文件

<body >

<h3 style="color:red">${msg4}</h3>

<form id="uploadForm" method="post" action="UploadServlet"enctype="multipart/form-data">
<br />

<% UserBean currentUser = (UserBean)(session.getAttribute"currentSessionUser"));%>

Course Number:
 <select name="courseNo">
   <option value="CS1">CS1</option>
   <option value="CS2">CS2</option>
 </select> 
<br />
Assignment Number:
 <select name="hwNo" id="hwNo" >
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
 </select> 
<br />
Assignment (must be a zip file): <input type="file" name="dataFile"     id="fileChooser" /><br />

<br /> <input type="submit" value="Upload" />
</form>

<h3 style="color:red">${msg2}</h3>

</body>

相关的 Servlet 代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        HttpSession session = request.getSession(true);
        UserBean currentUser = (UserBean)(session.getAttribute("currentSessionUser"));
        String uName = currentUser.getUsername();


        if (!ServletFileUpload.isMultipartContent(request))
        {return;}


        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(THRESHOLD_SIZE);
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(MAX_FILE_SIZE);
        upload.setSizeMax(REQUEST_SIZE);


        try 
        { 
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) 
            {
                if (item.isFormField()) 
                {
                    String fieldname = item.getFieldName();

                    if(fieldname.equals("courseNo"))
                        courseNo = item.getString();
                    if(fieldname.equals("hwNo"))
                    {
                        hwNo = item.getString();
                        homeworkNo = Integer.parseInt(hwNo);    
                    }   

                Boolean exists = checkIfHWNoExists(homeworkNo, uName);
                if(exists)
                {
                    request.setAttribute("msg4", "You've already made a submission for " +
                                        "assignment number " + homeworkNo +"." +
                                        "<br />If you need to change your submission, " +
                                        "please contact your teacher.");
                    request.getRequestDispatcher("/upload.jsp").forward(request, response);
                    return;
                }

            }

            if (!item.isFormField())
            {
                (item.getName()).getName();
                File storeFile = new File(item.getName());
                String fileName = storeFile.getName();
                if(!fileName.endsWith(".zip"))
                {
                    request.setAttribute("msg", "Must be a zip file!  Try again!");
                    request.getRequestDispatcher("/upload.jsp").forward(request, response);
                    return;
                }


                else
                {
                    String mimeType = new MimetypesFileTypeMap().getContentType(storeFile);
                                                            //for putting file into database
                    putFileIntoDB(uName, homeworkNo, fileName, mimeType, storeFile);


                    request.setAttribute("msg2", "File uploaded successfully!");
                    request.getRequestDispatcher("/upload.jsp").forward(request, response);
                }
            }
        }


            //request.setAttribute("message", "Upload has been done successfully!");

   }
    catch (Exception ex)
    {
        request.setAttribute("message", "There was an error: " + ex.getMessage());
    }

    ("/message.jsp").forward(request, response);


        }
4

1 回答 1

0

问题出在 servlet 的循环中。这是完全删除循环的更正 doPost 方法,因为它不是必需的。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    String courseNo;
    int homeworkNo;

    HttpSession session = request.getSession(true);
    UserBean currentUser = (UserBean)(session.getAttribute("currentSessionUser"));
    String uName = currentUser.getUsername();

    if (!ServletFileUpload.isMultipartContent(request))
    {return;}


    try 
    { 
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

        courseNo = items.get(0).getString();
        homeworkNo = Integer.parseInt(items.get(1).getString());    
        if(checkIfHWNoExists(courseNo, homeworkNo, uName))
        {
          request.setAttribute("msg4", "You've already made a submission for Course Number " + courseNo + ", "+
                                    "Assignment number " + homeworkNo +"." +
                                    "<br />If you need to change your submission, " +
                                    "please contact your teacher.");
                request.getRequestDispatcher("/upload.jsp").forward(request, response);
                return;
            }

         else//ok to add new HW, it doesn't exist
         {
          File storeFile = new File(items.get(2).getName());
          String fileName = storeFile.getName();

          if(!fileName.endsWith(".zip"))
          {
              request.setAttribute("msg", "Must be a zip file!  Try again!");
              request.getRequestDispatcher("/upload.jsp").forward(request, response);
          return;
          }

            else
            {  
                String mimeType = new MimetypesFileTypeMap().getContentType(storeFile); //for putting file into database
                      putFileIntoDB(uName, courseNo, homeworkNo, fileName, mimeType, storeFile);

              request.setAttribute("msg2", "File uploaded successfully!");
              request.getRequestDispatcher("/upload.jsp").forward(request, response);
            }
         }

    }//try
    catch (Exception ex)
     {
       request.setAttribute("msg", "There was an error: " + ex.getMessage());
       request.getRequestDispatcher("/upload.jsp").forward(request, response);
      }

}
于 2013-02-20T14:08:08.863 回答