简体   繁体   中英

how to get session().getAttribute() from Servlet doGet in JSP with Struts

I cannot get the session Attribute from Servlet to JSP page. My Servlet:(columnChartServlet.java)

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

My 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>

my 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>

after logged in my admin.jsp will open in the url of login.do.

my web.xml config for servlet

<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> 

when i run application admin.jsp shows the print value as null.

you will be able to retrieve data if you use like below in jsp

session.getAttribute("myname");

and in your servlet try to use like below:

request.getSession(false).setAttribute("myname", "Steve");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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