Results 1 to 5 of 5
  1. #1
    Rohit Pokhrel is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

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

    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>
    -------session.java-------------

    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,
    Rohit
    Last edited by Rohit Pokhrel; 12-08-2010 at 12:01 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,181
    Rep Power
    20

    Default

    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.

  3. #3
    Rohit Pokhrel is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    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 12:00 PM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,181
    Rep Power
    20

    Default

    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.

  5. #5
    mi14chal is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 10-11-2010, 02:16 AM
  2. Session Variable corruption example..
    By joe131 in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2010, 03:51 PM
  3. how to use user name as a session variable in jsp
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-05-2010, 10:09 AM
  4. Replies: 6
    Last Post: 03-31-2009, 05:27 AM
  5. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •