如何在 servlet 和 jsp 文件之间共享会话属性?
我有一个表单,提交后将其字段中的数据存储到数据库中。 struts-config 中的转发操作映射回数据插入数据库成功/失败的同一页面。我想在成功完成后提醒用户,因此我在表单的操作类的方法中设置了会话属性(即成功/失败)。然后,一旦再次访问 jsp 页面,我就会获取并打印出该会话属性。
到目前为止,我已经在 Action 类中完成了此操作:
public static void setJavaScriptNotification(HttpServletRequest request, String notificationText) {
HttpSession session = request.getSession(true);
session.setAttribute("notification_javascript", notificationText);
}
在包含表单的 jsp 文件中,我有:
<% String notificationJavaScript = (String) request.getSession().getAttribute("notification_javascript");
pageContext.setAttribute("notification_javascript", notificationJavaScript);
request.getSession().removeAttribute("notification_javascript"); %>
<html>
<head>
<logic:present name="notification_javascript">
<script type="text/javascript" language="JavaScript">
function showAlerts() {
alert("<bean:write name="notification_javascript"/>");
}
</script>
</logic:present>
</head>
<body onload="doPreOnload(); showAlerts();">
当我打印出 jsp 中的会话属性时,我找不到 notification_javascript 属性。我是 HTTP 新手,所以我可能在那里做错了什么。
I have a form which when submitted stores data from it's fields to the database. The action forward in the struts-config maps back to the same page on Success/Failure of data insertion into the database. I would like to alert the user once this is successfully completed, so I set a session attribute(i.e. success/failure) in the method of the action class for the form. I then get and print out this session attribute once the jsp page has been accessed again.
So far I have done this in the Action Class:
public static void setJavaScriptNotification(HttpServletRequest request, String notificationText) {
HttpSession session = request.getSession(true);
session.setAttribute("notification_javascript", notificationText);
}
And in the jsp file that contains the form I have:
<% String notificationJavaScript = (String) request.getSession().getAttribute("notification_javascript");
pageContext.setAttribute("notification_javascript", notificationJavaScript);
request.getSession().removeAttribute("notification_javascript"); %>
<html>
<head>
<logic:present name="notification_javascript">
<script type="text/javascript" language="JavaScript">
function showAlerts() {
alert("<bean:write name="notification_javascript"/>");
}
</script>
</logic:present>
</head>
<body onload="doPreOnload(); showAlerts();">
When I print out the session attributes in the jsp, I can't find the notification_javascript attribute. I'm new to HTTP, so I could be doing something wrong there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是,则 session.getAttribute("notification_javascript") 将完成这项工作。
request.setAttribute() 与 session.setAttribute()
request.setAttribute() 将使密钥在下一页中可用。
session.setAttribute() 将使密钥在所有页面中可用。
After setting notification_javascript in session in setJavaScriptNotification() do the request is forwarded to jsp where notification_javascript is accessed.
If yes, then session.getAttribute("notification_javascript") will do the job.
request.setAttribute() vs session.setAttribute()
request.setAttribute() will make the key available in following page.
session.setAttribute() will make the key available in all pages.