gpt4 book ai didi

java - 如何在jsp页面中使用if条件比较两个字符串?

转载 作者:行者123 更新时间:2023-11-29 07:48:53 25 4
gpt4 key购买 nike

Jsp代码:

<% 
String usermailid=(String)session.getAttribute("Username");
System.out.println("value of session==="+usermailid.equals("null"));
String empty=null;

if(usermailid.equals("null")) {

%>



<% }

else
{%>


<%}%>

输出:

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)

在上面的代码中,我想比较两个字符串。但是当我比较它们时,当两个值都为 null 并将值显示为 false 时,我会得到空指针异常?所以谁能告诉我比较两个字符串的确切方法是什么???

最佳答案

使用简单检查

if (usermailid != null)

关于java - 如何在jsp页面中使用if条件比较两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com