Issue
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.
Solution
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");
Answered By - Md Mahamudul Hasan