【问题标题】:Close button in jsp page not working after response is redirected from servlet to jsp响应从 servlet 重定向到 jsp 后,jsp 页面中的关闭按钮不起作用
【发布时间】:2018-12-28 19:39:00
【问题描述】:

我的 jsp(Welcome.jsp) 页面上有一个提交按钮,我通过该按钮将请求发送到 myservlet(Import Parts)。我在 servlet 中进行了一些验证,验证失败后,我将用户重定向到 error.jsp 页面,该页面给出了合适的消息。这个error.jsp 还有一个关闭按钮,在我将响应转发到error.jsp 后该按钮不起作用。

如果我直接在浏览器中启动error.jsp并尝试单击关闭按钮,它可以工作。

// Below is the piece of code written in my Servlet on doPost() method which perform some validation and upon failing the validation check redirects user to error.jsp page.

if(!UserDetails.checkValidUser(asession))  //a Validation method returning true or false
            {
                logger.info("User do not have necessary role");
                request.setAttribute("error", EMSImportConstants.NO_PRIVILEGE_USER_ERR_MESSAGE);
                RequestDispatcher rd = request.getRequestDispatcher("/Error.jsp");
                rd.forward(request, response);
            }

//Below is my error.jsp page code

<% 
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
<title>Error in EMS BOM Import</title>
        <link href="<%=basePath %>/css/netapps.css" rel="stylesheet" type="text/css" /> 
</head>
<body bgcolor="#ffffff" class="borderless">
<form method="post" action="Error.jsp">

 <center>
            <div id="banner">
                <div id="logo"></div>

                <div id="rightbg">
                    <div id="bannerTitle"><br>EMS BOM Import</div>
                </div>
                <div id="bannerImage"> </div>
            </div>
</center>
<br />
<br />
<br />
<br />
<center>
<font size = "2px" color="red">
<%=request.getAttribute("error") %>
</font>
<table>
<tr>
<td align="center"><input type="button" value="Close"  onclick="window.close();"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>


I click on close button but nothing happens.

【问题讨论】:

  • 尝试去掉 servlet 中的 if 以查看行为。你收到什么样的消息?
  • If 语句在这里没有引起问题,因为行为保持不变。如果我删除 if,我将被重定向到我的 error.jsp 页面,在该页面上关闭按钮仍然不起作用。但是,如果我直接运行 error.jsp 页面并尝试单击关闭按钮,它会关闭浏览器窗口,但是当我从我的 servlet 转发到这个 error.jsp 时,相同的按钮不起作用。
  • 这仍未得到答复,任何帮助将不胜感激。

标签: java jsp servlets


【解决方案1】:

表单中,要提交详细信息,input 类型应为 submit 而不是 button。以这种方式更改您的代码。它应该工作:)

<form>
  <td align="center"><input type="submit" value="Close" onclick="window.close();" /></td>
</form>

【讨论】:

  • 代码的流程是——Welcome.jsp——>用户点击welcome.jsp上的提交按钮,请求进入Servlet。 -> 我从 servlet 调用一个返回 true/false 的方法-> 如果为 false,我将用户重定向到包含关闭按钮的 error.jsp 页面。我不明白为什么我需要在 error.jsp 中提交按钮,因为我没有从 error.jsp 页面发送任何请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2012-05-18
  • 2015-07-24
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
相关资源
最近更新 更多