gpt4 book ai didi

java - 如何在 "re-submit"之后将新的表单参数从 jsp 传递到 servlet?

转载 作者:行者123 更新时间:2023-12-01 04:51:01 24 4
gpt4 key购买 nike

为了简单起见,我有一个带有小表单的 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);


}

最佳答案

问题出在 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);
}

}

关于java - 如何在 "re-submit"之后将新的表单参数从 jsp 传递到 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14959306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com