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
# 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.
# 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
# 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
# 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"