1

I start learning jsp application, and sometimes i got the error message "something cannot be resolved...". For example, this is my last report:

An error occurred at line: 118 in the jsp file: /functions.jsp
session cannot be resolved
115:             
116:     Boolean isLogged()
117:         {
118:             Boolean loginSuccess = (Boolean)session.getAttribute("myApp.loginSuccess");
119:             if (loginSuccess == null) 
120:             {
121:                 return false;

These lines refer to a function, that checks the success of the login procedure. So i have two questions:

  1. How can i fix the problem in this situation?
  2. What's the reason of these messages, that sometimes disappears with no cause?
optimusfrenk
  • 1,271
  • 2
  • 16
  • 34

1 Answers1

2

You need to cast the returned value of getAttribute() method.

Boolean loginSuccess = (Boolean)session.getAttribute("myApp.loginSuccess");

You can't use implicit object variables directly into method body (declaration). You should have to avoid Java code in JSPs (Declaration, Scriptlet and Expression). Alternatively you can use use Servlet/Filter.

To fix this issue declare a reference variable of HttpSession in declaration block.

<%!
  HttpSession sess;

  Boolean isLogged(){
     Boolean loginSuccess = (Boolean)sess.getAttribute("myApp.loginSuccess");
     if (loginSuccess == null) 
        ...
  }
%>

and assign the reference of session object to sess variable before you call the isLogged method.

e.g

<%
 sess=session;
 if(isLogged()){
   //code
 }
%>
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186