如何通过Struts在JSP中从Servlet doGet获取session()。getAttribute()

问题描述 投票:1回答:1

我无法从Servlet到JSP页面获取会话属性。我的Servlet:(columnChartServlet.java)

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().setAttribute("myname", "Steve");
    request.getSession().setAttribute("age", "50");
}

我的JSP(admin.jsp)

<head>
<%@page import="com.cos.sms.actions.columnChartServlet" %>
<jsp:useBean id="chartMast" class="com.cos.sms.actions.columnChartServlet" scope="session" />
</head>
<body>
<%= request.getAttribute("myname") %>
<%= request.getAttribute("age")  %>
<%= request.getSession().getAttribute("myname") %>
<%= request.getSession().getAttribute("age") %>

</body>

我的Struts-config

<action input="/jsp/login.jsp" path="/login" name="loginForm"  type="com.cos.sms.actions.LoginAction" scope="session" parameter="method" >
    <forward name="admin" path="/jsp/admin.jsp"/>   
    <forward name="normal" path="/view_info.do?method=getDropdownSelectData"/>
    <forward name="diplayloginjsp" path="/jsp/login.jsp"/>
</action>

登录我的admin.jsp后,将在login.do的URL中打开。

我的servlet的web.xml配置

<servlet>
    <servlet-name>columnChartServlet</servlet-name>
    <servlet-class>com.cos.sms.actions.columnChartServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>columnChartServlet</servlet-name>
    <url-pattern>/columnChartServlet</url-pattern>
</servlet-mapping> 

当我运行应用程序admin.jsp时,将打印值显示为null。

java jsp servlets struts
1个回答
0
投票

如果您像下面在jsp中那样使用,您将能够检索数据

session.getAttribute(“ myname”);

并且在您的servlet中尝试如下使用:

request.getSession(false).setAttribute(“ myname”,“ Steve”);

© www.soinside.com 2019 - 2024. All rights reserved.