session handling in JSF

Hi,

I have a pageindex.jsp which contains username & password text box & a button.

If username password correct then I am going inside onwelcome.jsp page.

But if user directly try to access url as,

http://localhost:8080......./welcome.jsp (i.e without doing login)

I am getting error as,

org.apache.jasper.JasperException: javax.servlet.jsp.JspException: java.lang.NullPointerException

onwelcome.jsp I have included my session.jsp page as,

<%@ include file="session.jsp" %>

and mysession.jsp page code contians,

<%@ page language="java" %>

<%@ page import="com.LoginForm" %>

<%

if(session==null)

{

response.sendRedirect("index.jsp");

}

LoginForm login=(LoginForm)session.getAttribute("loginFormBean");

if(login==null)

{

response.sendRedirect("index.jsp");

}

else{

String user_name = login.getUserName();

if(user_name=="" || user_name==null){

response.sendRedirect("index.jsp");

}

}

%>

can anybody please help me in this why I am getting above error.

Thanks

Sandip Patil

[1244 byte] By [sandip_03_jsfa] at [2007-11-26 17:35:01]
# 1

if (user_name == "" || user_name == null) {}

The first expression (in theory! this one is wrong) requires that the user_name should not be null. So swap the both expressions with each other.

Further on, this expression is wrong, because this is not the way to compare Strings. Remember, a String is a object and not a datatype (like int, long, boolean, etc). Well, it just has a method to compare with other strings: string.equals(anotherString). So do:

if (user_name == null || user_name.equals("")) {}

This is basic debugging work however. NullPointerExceptions are relativele easy to solve. Use your IDE gently.

BalusCa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi BalusC,

Thanks for your response.

I have changed code for comaparing string.

But stll I am getting same error.

even I tried to redirect to another page as,

FacesContext context = FacesContext.getCurrentInstance();

if(session==null) {

context.getExternalContext().redirect("index.jsp");// instead response.sendRedirect("index.jsp"); }

LoginForm login=(LoginForm)session.getAttribute("loginFormBean");

if(login==null) {

context.getExternalContext().redirect("index.jsp"); }

else{

String user_name = login.getUserName();

if (user_name == null || user_name.equals("")) {

context.getExternalContext().redirect("index.jsp");}

}

Thanks

Sandip

sandip_03_jsfa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
can anybody help me in this.ThanksSandip
sandip_03_jsfa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Just do some basic debugging work. Sysout the variables to console, or add a logger, or (un)comment pieces of code, etc..etc..
BalusCa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks for yoyr response.

Sorry I am here confused regarding nullpointer exception.Means my response object getting null or something else ?

As in session.jsp i am writing as,

if(session==null)

{

response.sendRedirect("index.jsp");

}

LoginForm login=(LoginForm)session.getAttribute("loginFormBean");

if(login==null)

{

response.sendRedirect("index.jsp");

}

else{

String user_name = login.getUserName();

if(user_name==null || user_name.equals("")){

response.sendRedirect("index.jsp");

}

}

Thanks

Sandip

sandip_03_jsfa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

One more thing here you can check.

I work with Apache tomcat6 and every time I change the code which involves application

objects, they get invalidated in the server memory and you get null pointer errors

depending how your global objects get initialised. In this case I try this.

Either goto tomcat manager and restart the website using http://localhost:8080 link

Or restart tomcat server itself for me on linux it is "/etc/init.d/tomcat restart"

Roharaa at 2007-7-9 0:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...