How to check for a session in a jsp page?

akashsuresh
akashsuresh used Ask the Experts™
on
When I give for a session check as given in this page in Websphere ,I get a null pointer exception whether I login using a login page or not.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<%
  if(session.getAttribute("name")==null)
	  %>
	  <jsp:forward page="errorPage.jsp"></jsp:forward>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
<title>Administration Panel</title>

<link href="style.css" rel="stylesheet" type="text/css" ></link>

 <script src="js/jquery-1.js" type="text/javascript"></script>
  	<script type="text/javascript" charset="utf-8">
	//tabbed forms box

		$(function () {
			var tabContainers = $('div#forms > div.innerContent'); // change div#forms to your new div id (example:div#pages) if you want to use tabs on another page or div.
			tabContainers.hide().filter(':first').show();
			
			$('ul.switcherTabs a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('ul.switcherTabs li').removeClass('selected');
				$(this).parent().addClass('selected');
				return false;
			}).filter(':first').click();
		});
	
		$(function () {
			var tabContainers = $('div#menu > div.innerContent'); // change div#forms to your new div id (example:div#pages) if you want to use tabs on another page or div.
			tabContainers.hide().filter(':first').show();
			
			$('ul.switcherTabs a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('ul.switcherTabs li').removeClass('selected');
				$(this).parent().addClass('selected');
				return false;
			}).filter(':first').click();
		});
		
		</script>
</head>

<body>

<div id="container">
    <div id="header">
      <div id="logo">
        <h1><span><a >Ministry of Law</a></span></h1>
        <p>Courts Administration</p>
      </div>
      <div id="userBox">
        <p><img src="images/icon_user.gif" width="16" height="19" alt=" " /> &nbsp;Hello, <a class="white"><% out.println(session.getAttribute("name")); %></a>. |<a href="../login/logout.jsp">Logout</a></p>
       
      </div><!-- end of #userBox -->
      <div id="menu">
        <ul>
          
          <li class="selected">
          <span class="selected-arrow">&nbsp;</span>
          <a href="dash.jsp">Dashboard</a>
          <ul>
              <li class="selected"><a href="../dash/dash.jsp">Send Mail</a></li>
              <li ><a href="../dash/inbox.jsp">Inbox</a></li>
              <li ><a href="../dash/sentbox.jsp">SentBox</a></li>
              <li><a href="../dash/adminchangepassword.jsp">Change Password</a></li>
            </ul>
          
          </li>
                 
          <li><a href="../dash/admincasedash1.jsp">Cases</a></li>
          <li><a href="../dash/adminuserdash1.jsp">Users</a></li>    
           <li><a href="../dash/adminlawyerdash1.jsp">Lawyers</a>    
                  
          </li>
          <li><a href="../dash/adminjudgedash1.jsp">Judges</a></li>
        </ul>
       
      </div><!-- end of #menu -->
      
  </div><!-- end of  #header -->
      <hr />    
<div id="content">
        <div id="leftBox">
          <div id="pageIntro">
            <h2>Welcome to Indian Courts Admin Panel</h2>
            
          </div>
          <div class="contentBox" id="forms">
            <div class="contentBoxTop">
              <h3>Courts Messaging System</h3>
                <ul class="switcherTabs">
                    <li class="selected"><a href="#box-1">Send Mail</a></li><!-- the link location of this tab refers to the id of the related div -->
                   
                </ul>
            </div>
            <div class="innerContent" id="box-1">
              <form id="form1" name="form1" method="post" action="../dash/messageprocess.jsp">
                From
                <br />
                <label class="smallInput" id="fromadd"><% out.println(session.getAttribute("name")); %></label>
                 
                <p>Recipient<br />
                  <label  class="mediumInput">
                    <input type="text" name="toadd" size="127"/>
                  </label>
                  </p>
                <p>Subject <br />
                  <label  class="largeInput">
                    <input type="text" name="msgsubject" size="127"/>
                  </label>
                  <br />
                  </p>
                <p>Message<br />
                  <label class="textarea">
                    <textarea name="msg" rows="6" cols="80"></textarea>
                  </label>
                  </p>
                <p>
			<input name="" type="image" src="images/bt_submit.gif" />
                  </p>
              </form>
            </div><!--end of #box-1-->
            <div class="innerContent" id="box-2" style="display:none">
            Box-2 content goes here
            </div>
            
          </div>
<hr />
          
          <hr />
          <div class="contentBox">
            <div class="contentBoxTop">
              <h3></h3>
            </div>
            <div class="innerContent">
              
            </div><!--end of #box-3-->
          </div>
<hr />
      </div><!-- end of #leftBox -->
        

        
  </div><!-- end of #content -->
<br class="clearFix" />
</div><!-- end of #container -->
<hr />
<div id="footerWrap">
  <div id="footer">
  
      <div id="footRight">
        <div id="footStats">
        
        </div>
        Copyright 2010 Trojans<br />
          <span class="white">The Trojans</span>
              </div>
  </div>
  <br class="clearFix" />
</div>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
your doing correct but your not open { } in the pgm

>>>>>>

<%
  if(session.getAttribute("name")==null)
          %>

Author

Commented:
I did not get what you said,sir.What should I do?
what your checking in the line

>>
<%
  if(session.getAttribute("name")==null)
          %>
your using if loop  but ur not open the open baranthesh

like
<%
  if(session.getAttribute("name")==null)  {
          %>
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

like
<%
  if(session.getAttribute("name")==null) {   i added this
          %>

Author

Commented:
Ok i tried it,now its working.But why is it not going to error page when it is null
its from ur screen(jsp page)  not in java . here your not handle that  exception  like java throw or throws
so it will show in the browser itself . ...

Author

Commented:
Anyway to make it call the error page if session is null?
depend upon ur desgin only it will call. as i mentioned above ur not handle that !! so it wont go..

eg: Now your asking like this only

your creating a class called a . but ur not calling the class .  why a class values is printed ..

I think ur understand what i am saying
why a class values is not printed

Author

Commented:
Yes I understand,I need to write a code to handle the error.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial