2012-05-09 182 views
5

2 jsp pages and one Servlet到JSP页面。我从数据库中的servlet获取数据并发送结果result.jsp页我在哪里显示结果。但我想在result.jsp添加Back button,通过点击back button我想去index.jsp,但问题是,当我点击后退按钮每次消息来了Confirm form submission,它让我感到愤怒。我如何避免这种Confirm form submission?也许它会在servlet中完成处理。去通过点击返回按钮

的index.jsp

<form method="post" action="Student"> 

<input type="text" name="studentname"/> 
<input type="submit" value="search"/> 

</form> 

学生的servlet

String student_name=request.getParameter("studentname"); 

    -------fetching data from database------------ 
    -------------------sending to result.jsp------ 

    String nextJSP = "/result.jsp"; 
     RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP); 
     dispatcher.forward(request,response); 

result.jsp中

//displaying data here 

<form action="index.jsp"> 

<input type="submit" value="back"/>// a back button is here which is going to index.jsp 
</form> 

编辑

result.jsp我要去到result1.jsp另一页类似下面

result.jsp我写了下面的:

<a href="result1.jsp.jsp?a code=<%out.println(student_name);%>"><%out.println(student_name);%></a> 

通过单击上面的超链接我去result1.jsp

我想在这里补充一个后退按钮(在result1.jsp),然后点击我想要做的Result.jsp到,但单击后退按钮时,我得到每次Confirm form submission后。我写了下面的result1.jsp

<input type="button" value="Back" onclick="javascript:history.go(-1)"> 

不过我得到这一信息Confirm form submission。我怎样才能避免这种情况?我想去result.jsp直接出这个消息。这怎么可能?

+0

的背面(硬的index.jsp)一些答案中给出。 **为了防止浏览器回滚触发第二个表单提交**,在表单提交验证后处理'sendRedirect'到结果页面。这是一种常见的技术。 –

回答

2

,你也可以用这个去一个页面返回

<button type="button" name="back" onclick="history.back()">back</button> 
0

您可以使用一个共同的button

<input type="button" value="Back" onclick="javascript:history.go(-1)"> 

随着history.go(-1),您的浏览器将只需从缓存中读取显示前一页,它不会将您的资料重新提交到服务器,因此没有Confirm form submission会发生。

编辑

我错了!

所有你需要的是一个客户端重定向,你可以写这样的事情在你result.jsp它处理从index.jsp提交表单后:

response.sendRedirect("result.jsp"); 

所以,你需要一个参数来帮助您在result.jsp到判断它是否是一个简单的显示请求或表单提交,这样的事情:

String action = request.getParameter(action); 
if("submit".equals(action)) { 
    // handle form data 
    response.sendRedirect("result.jsp"); 
} else { 
    // other code to display content of result. 
} 

在你index.jsp,这将b Ë是这样的:

.... 
<form action="result.jsp?action=submit" ...> 
+0

谢谢你的回答,但你可以稍微解释一下为什么'onclick =“javascript:history.go(-1)”'?这是什么意思? – saroj

+0

它实际上是'window.history.go(-1)',其中window是浏览器的当前窗口,请注意,您总是可以省略'window'。这个特性是通过你的浏览器实现的,'history.go(-1)'意味着'在窗口的历史中向后走1步。' – neevek

+0

不,不,它仍然是'确认表单提交'实际上,我编辑了我的问题 – saroj

1

如果你想有一个后退按钮的index.jsp,为什么不只是做一个正常的链接试试这个

<button type="button" 
    name="back" 
    onclick='window.location='<your_path>/index.jsp'>back</button> 
1

<a href="index.jsp">Back</a> 

只有两到摆脱了消息,正常的链接或window.location的方式。如果前一页始终相同,则不需要使用复杂的功能客户端。如果页面可能不同,只需将链接发布到result.jsp并使用它实际打印后端链接!

编辑:

previous.jsp

<form method="post" action="Student"> 
<input type="hidden" name="back" value="previous.jsp" /> 
<input type="text" name="studentname"/> 
<input type="submit" value="search"/> 
</form> 

result.jsp中

out.println("<a href=\"" + request.getParameter("back") + "\">Back</a>"); 
+0

刚刚尝试了您的代码... out.println(“Back”); 只是这条线会做的伎俩。 – Jayanta

0

我经历过的形式提交确认的Safari(不带IE/Chrome浏览器/ FF)。

解决方法是使用“get”方法提交表单。当然,这只适用于“小”形式(最大2048 K)。

2

你可以写下面的代码,它可以让你去index.jspresult.jsp

<a href="index.jsp">Back</a> 
1

上这是为了创建一个使用该方法的GoBack()一GoBack的按钮,最简单的方法。这与单击浏览器顶部使用的“后退按钮”相同。

<!DOCTYPE html> 
    <html> 
    <head> 
    <script> 
    /* The back() method loads the previous URL in the history list. 
     This is the same as clicking the "Back button" in your browser. 
    */ 
    function goBack() { 
     window.history.back() 
    } 
    </script> 
    </head> 
    <body> 

    <button onclick="goBack()">Go Back</button> 

    <p>Notice that clicking on the Back button here will not result in any action, because there is no previous URL in the history list.</p> 

    </body> 
    </html> 
0

试试这个

<a href="${pageContext.request.contextPath}/index.jsp" class="btn btn-success"> 
    Back 
</a> 

与bootstrap.css从here