Use simple check
if (usermailid != null)
Question
Jsp code:
<%
String usermailid=(String)session.getAttribute("Username");
System.out.println("value of session==="+usermailid.equals("null"));
String empty=null;
if(usermailid.equals("null")) {
%>
<% }
else
{%>
<%}%>
output:
Apr 7, 2014 5:39:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.election_005fresults_jsp._jspService(election_005fresults_jsp.java:437)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
In the above code i want to compare two strings.but when ever i comapare them i am getting null pointer exception when both the values are null and showing value as false?? so can anyone tell me what is the exact way of comparing two string???
La solution
Use simple check
if (usermailid != null)
Autres conseils
if(usermailid!=null)
try this it will work.
String x = null;
String x = "null";
both are different
how to compare two strings using if condition inside jsp page?
Do not write scriptlets <% %>
in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?
So, I am posting solution using EL syantax.
${!empty Username}
Here Username
is directly referred from session scope. empty
is operator which checks for null
as well as empty.
You can use <c:if>
or <c:when>
for conditional check.
<c:if test="${!empty Username}">
//Username is not null here
</c:if>
Here you are getting usermailid null and you are trying to compare with String "null". see null is nothing(null) but "null" is a String with null as text.
first remove this line:
System.out.println("value of session==="+usermailid.equals("null"));
u may simply write this:
if(null == usermailid) {
// email id is null
} else {
// proceed ur logic
}