Java 如何在jsp页面中使用if条件比较两个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22912150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to compare two strings using if condition inside jsp page?
提问by User2413
Jsp code:
JS代码:
<%
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???
在上面的代码中,我想比较两个字符串。但是当我对它们进行比较时,当两个值都为 null 并且将值显示为 false 时,我会收到空指针异常?所以谁能告诉我比较两个字符串的确切方法是什么???
采纳答案by JavaLearner
Use simple check
使用简单检查
if (usermailid != null)
回答by Sanjay Rabari
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.
在这里,您获得了 usermailid null,并且您正在尝试与字符串“null”进行比较。见 null 是空无(null),但“null”是一个字符串,以 null 作为文本。
回答by Sanjay Rabari
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
}
回答by tarkikshah
if(usermailid!=null)
try this it will work.
试试这个它会起作用。
String x = null;
String x = "null";
both are different
两者都不一样
回答by Aniket Kulkarni
how to compare two strings using if condition inside jsp page?
如何在jsp页面中使用if条件比较两个字符串?
Answer
回答
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?
不要<% %>
在 JSP 中编写 scriptlet,因为在 JSP 中不应使用 scriptlet 超过十年。学习 JSP EL、JSTL,并将 servlet 用于 Java 代码。如何避免 JSP 文件中的 Java 代码?
So, I am posting solution using EL syantax.
所以,我正在使用 EL syantax 发布解决方案。
${!empty Username}
Here Username
is directly referred from session scope. empty
is operator which checks for null
as well as empty.
这里Username
直接从会话范围引用。empty
是检查null
以及空的运算符。
You can use <c:if>
or <c:when>
for conditional check.
您可以使用<c:if>
或<c:when>
进行条件检查。
<c:if test="${!empty Username}">
//Username is not null here
</c:if>