gpt4 book ai didi

java - 重新提交相同请求时将请求属性设置为 null

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

我有这样的场景:

我需要将附件上传到服务器。我有一个 jsp 页面 和一个处理我的请求的 servlet 。在上传文件之前,我需要检查重复的文件名,以便在未通知用户的情况下不会替换文件。因此,在我的 servlet 中,我正在执行适当的检查并将 request attribute 设置为 true,以便当我将请求转发到 jsp 页面 并看到该属性设置为true,我向用户显示一个对话框(使用 jquery),以便用户选择要执行的操作(同时保留、覆盖、取消)。

我唯一担心的是,当刷新页面时,即提交相同的请求,在属性设置为 true 后,属性仍然为 true,因此刷新时我正在加载对话框。当然,我不希望这样的事情发生。

有什么可以帮助我解决这个问题吗?这是我的代码。

UploadFile.java - 我的 Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
request.setAttribute("duplicate", null);

if (!isDuplicate(file)){
// .. handle uploading
}
else
{
// if duplicate
request.setAttribute("duplicate", "true");

ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/ticketsform.jsp");
dispatcher.forward(request, response);
}

response.sendRedirect("ticketform.jsp");
}

ticketform.jsp

<% 
String isDuplicate = (String)request.getAttribute("duplicate");

if (isDuplicate != null)
{ %>
<script type="text/javascript">
duplicateFilesOption(); // call js function to display dialog
</script>
<% } %>

感谢您提前提供的任何帮助:)

最佳答案

您可以将以下内容添加到您的 jsp.jsp 中。

这将告诉浏览器重新发送请求而不是重新显示结果。

<head>   
<meta HTTP-EQUIV=Expires CONTENT="0">
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<head>

关于java - 重新提交相同请求时将请求属性设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853650/

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