Java 如何在 page.redirect 从 servlet 到 jsp 上显示成功消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19972073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show success message on page.redirect from servlet to jsp
提问by Adi
I have a html form in jsp page which on submit is going to servlet ..After executing the functions in servlet i am again redirecting it to the same jsp page from which it has been invoked with a success message to display now on the same jsp page but i don't know how to do this ...
我在 jsp 页面中有一个 html 表单,提交时将转到 servlet ..在 servlet 中执行函数后,我再次将其重定向到同一个 jsp 页面,从该页面中调用成功消息,现在显示在同一个 jsp 上页面,但我不知道该怎么做...
Here is my jsp form code..
这是我的jsp表单代码..
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date">
<label class="button2">Set Hour </label>
<input type="text" name="hour" id="hour">
<label class="button2">Set Minute: </label>
<input type="text" name="minute" id="minute">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/><br/>
<label class="button2">Set File-Path: </label>
<input type="text" name="filepath" id="filepath">
</form>
And here is my servlet redirect code.
这是我的 servlet 重定向代码。
response.sendRedirect("Automail.jsp");
采纳答案by Kerb
As per your requirement I would suggest you to go for ajax.I gave a simple example how to pass data to servlet.Click hereto know more about jquery ajax
根据您的要求,我建议您使用 ajax。我给出了一个如何将数据传递给 servlet 的简单示例。单击此处了解有关 jquery ajax 的更多信息
$.ajax(
{
type: "get",
url: "CallTimer", //Your full URL goes here
data: { name: name1, date: date1,hour:hour1,filepath:filepath1,minute:minute1},
success: function(data, textStatus, jqXHR){
alert("success");
},
error: function(jqXHR){
alert(jqXHR.responseStatus);
}
});
notename-parameter name and name1 parameter value,hour parameter name and hour1 parameter value.Similarily for others.Dont use get action in forms because parameter values will be displayed in the url and also there is a limit of 2048 characters
注意name-参数名称和name1参数值,小时参数名称和小时1参数值。其他类似。不要在表单中使用get操作,因为参数值会显示在url中,并且有2048个字符的限制
回答by Masudul
At Servlet:
在 Servlet:
// You need to set value in session for redirection.
session.setAttribute("msg","Success");
response.sendRedirect("Automail.jsp");
At Automail.jsp
在 Automail.jsp
${msg}
回答by Kerb
In servlet:
在 servlet 中:
response.sendRedirect("Automail.jsp?success=1");
In your jsp:
在你的jsp中:
<c:if test="${param.success eq 1}">
<div> success </div>
</c:if>
回答by Under_stand
In servlet:
在 servlet 中:
session.setAttribute("message","successful");
response.sendRedirect("Automail.jsp");
In JSP:
在 JSP 中:
<c:if test="${not empty message}">
<h3 style='color:green'>${message}</h3>
<c:remove var="message"/>
</c:if>
Don't forget to remove variable after printing to not include message after page refresh.
不要忘记在打印后删除变量以在页面刷新后不包含消息。