Results 1 to 5 of 5
- 12-08-2010, 08:26 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Accessing session variable from Java Class
Dear all,
I am having problem while accessing session variable from Java Class.
-------index.jsp--------
In index.jsp i am Creating session variable SCHEMA and assigning it value "SC0001"
and finally redirecting to another jsp page Main_page.jsp
Java Code:<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% session.setAttribute("SCHEMA","SC0001"); response.sendRedirect("Main_page.jsp"); %>
-----Main_page.jsp----------
In this page i am trying to fetch session information stored through previous page .I am using my class session() to access the session variable; Here i am facing problem which says java.lang.NullPointerException..
-------session.java-------------Java Code:<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page language="java" import = "MySession.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Session Test</title> </head> <body> <h1><% session mysess=new session(); try{ String sesval=mysess.SessionTest(); out.println("<b>Schema is --" + sesval + "</b>"); } catch(Exception e) { out.println("<b>" + e + "</b>"); } %> </h1> </body> </html>
-----------Java Code:package MySession; import javax.servlet.http.*; public class session { private HttpServletRequest request; private HttpSession session; public String SessionTest() { session = request.getSession(true); String Schema=(String)session.getAttribute("SCHEMA"); return Schema; } }
please advise.
thanks,
RohitLast edited by Rohit Pokhrel; 12-08-2010 at 11:01 AM.
- 12-08-2010, 09:09 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
How does your session class (it should start with a capital letter by the way) get the current request? Clearly it isn't (hence the NPE).
In the constructor for session pass in the request.
ETA: As a general note, JSPs really shouldn't be forwarding or redirecting. They are for display, not flow control or business logic. That's what a Servlet is for.
- 12-08-2010, 09:50 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Thanks Tolls , As per your comment I have Changed my class to start with capital letter and as well I have merged my index.jsp with Main_page.jsp so that JSP is not redirecting any more. PLease have a look at modified Main_page.jsp
-----Java Code:<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page language="java" import = "Excel.*"%> <% session.setAttribute("SCHEMA","SC0001"); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1> <% //Retrieving attributes in Normal way. String Schema = (String)session.getAttribute("SCHEMA"); out.println("<b>" + Schema + "</b>"); //Retriving attributes using Session Class.. Session mysess=new Session(); try{ String sesval=mysess.SessionTest(); //String Schema = (String)session.getAttribute("SCHEMA"); out.println("<b>" + sesval + "</b>"); } catch(Exception e) { out.println("<b>" + e + "</b>"); } %> </h1> </body> </html>
Output:SC0001 java.lang.NullPointerException
-----
Can we actually read session variable attributes through Class file and return it to JSP ?Last edited by Rohit Pokhrel; 12-08-2010 at 11:00 AM.
- 12-08-2010, 10:06 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Your Session is still trying to get data from an HttpRequest object that is null.
I have no idea what you're trying to achieve here with your Session class.
And please use code tags.
- 12-08-2010, 04:07 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
accessing variable/methods of a instantiated object
By hariza in forum AWT / SwingReplies: 5Last Post: 10-11-2010, 01:16 AM -
Session Variable corruption example..
By joe131 in forum Advanced JavaReplies: 1Last Post: 05-10-2010, 02:51 PM -
how to use user name as a session variable in jsp
By mudit222 in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 05-05-2010, 09:09 AM -
accessing a one class's non static variable from another class
By aruna1 in forum New To JavaReplies: 6Last Post: 03-31-2009, 04:27 AM -
how to use session variable in my problem
By Arif Baig in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-27-2008, 07:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks