简体   繁体   English

jsp forward / sendredirect问题

[英]jsp forward / sendredirect issue

Given two jsp's: myFirstJsp.jsp , mySecondJsp.jsp 给出两个jsp: myFirstJsp.jspmySecondJsp.jsp

myFirstJsp.jsp receives the submit parameters from another form, stores some values in the DB, and if everything is successful makes a forward to mySecondJsp.jsp , sending a couple of parameters like this: myFirstJsp.jsp从另一个表单接收提交参数,在DB中存储一些值,如果一切都成功,则转发到mySecondJsp.jsp ,发送如下的几个参数:

<jsp:forward page="mySecondJsp.jsp">
<jsp:param name="param1" value='<%=request.getParameter("param1")%>'/>
<jsp:param name="param2" value='<%=request.getParameter("param2")%>'/>
</jsp:forward>

Everything works fine, but if the user press F5 or refresh the page, an exception will occur, since the URL would still be pointing to "myFirstJsp.jsp", so basically a duplicate insert exception will occur, and I want to prevent this. 一切正常,但如果用户按F5或刷新页面,将发生异常,因为URL仍将指向“myFirstJsp.jsp”,因此基本上会发生重复的插入异常,我想阻止这种情况。

The other option available is to use sendRedirect instead of forward, like this: 可用的另一个选项是使用sendRedirect而不是forward,如下所示:

response.sendRedirect( "mySecondJsp.jsp?param1=somevalue&param2=somevalue" );

however, in this case the parameters will be visible to the user, and although normally they won't change them, I need to prevent that scenario too. 但是,在这种情况下,参数对用户是可见的,虽然通常它们不会改变它们,但我也需要防止这种情况。

Is there any way to avoid either situations? 有什么办法可以避免这两种情况吗?

You can use following to set a request parameter in myFirstJsp.jsp and get it on mySecondJsp.jsp: 您可以使用以下命令在myFirstJsp.jsp中设置请求参数并在mySecondJsp.jsp上获取它:

request.setAttribute("param_name","param_value");
request.getAttribute("param_name");

Similarily, you can put the values in session. 类似地,您可以将值放在会话中。

But, if you want to prevent duplication of records in table then you can write a procedure in database to handle this. 但是,如果您想要防止表中的记录重复,那么您可以在数据库中编写一个过程来处理这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM