Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to check whether a variable exists, like isset() in php

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
I want to check to see if a variable on an include page exists, is there an easy way to do this? at the oment I have:

Code:
						<%
						 if (pageHeader != null) {
						 %>
						 	<%= pageHeader %>
						 <%
						 } else {
						 %>
							 Doc ID Tool
						 <%
						 }
						%>
 
Sorry to bump but does anyone have anyone have any ideas? I am really stuck on this one. Basically I have a page include below a String declaration. In the page include (the common head file) I output the string delaration in the header tab. But I need a way to see if the String variable exists, if it doesnt, output a generic header title.

Thanks!
 
You cannot check if a variable exists in Java - its not a scripting langauge, but a compiled language - so if your String variable does not exist when you attempt to reference it, your JSP will not compile.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
One way to achieve what you want is to do something like :

Code:
<%
String blah = null;
%>

<%@ include file="header.inc" %>

<%
if (blah != null) {
  out.println(blah);
} else {
   out.println("Hello");
}
%>

However, in header.inc, you would have do replace :

String blah = "something";

with

bla = "something";

in order to avoid compilation errors (redefining a variable name).

You would also have to make sure that in all JSPs that used that header, that they declared 'String blah = null;' before including the page.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Or actually, a sneakier way would be :

In header.inc, do not actually use a declared variable, but use the session object ...

Instead of

String bla = "something";

say :

session.setAttribute("blah", "something");

Then in your JSP :

Code:
                        <%
                         String blah = session.getAttribute("blah");
                         if (blah != null) {
                         %>
                             <%= blah %>
                         <%
                         } else {
                         %>
                             Doc ID Tool
                         <%
                         }
                        %>


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
When I do this:

Code:
<%
if (blah != null) {
  out.println(blah);
} else {
   out.println("Hello");
}
%>

I get the following :

Code:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 71 in the jsp file: /includes/common/header.jsp

Generated servlet error:
    [javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.5\work\Catalina\localhost\sqmdocuments\org\apache\jsp\includes\common\header_jsp.java:129: cannot find symbol
symbol  : variable pageHeader
location: class org.apache.jsp.includes.common.header_jsp
						 if (pageHeader != null) {
                                                     ^



An error occurred at line: 71 in the jsp file: /includes/common/header.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\work\Catalina\localhost\sqmdocuments\org\apache\jsp\includes\common\header_jsp.java:130: cannot find symbol
symbol  : variable pageHeader
location: class org.apache.jsp.includes.common.header_jsp
						 	out.print(pageHeader);
                                                                  ^
2 errors


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:128)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
	org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:413)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:437)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:555)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
Whcih solution are you trying ? That error is barfing because there is no variable called "pageHeader" set.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I am trying

Code:
<%
	String pageHeader = "Log New Document";
		
%>
<%@ include file="includes/common/header.jsp" %>

Then

Code:
						<%
						 if (pageHeader != null) {
						 	out.print(pageHeader);
						 } else {
							out.print("Doc ID Tool");
						 } 
						%>

In the header
 
Are both snippets in the same file? Which is its name?

Cheers,
Dian
 
Well I've just tried it and it works fine.

1) page1.jsp :

Code:
<%

String pageHeader = null;

%>

<%@ include file="header.inc" %>

<%

if (pageHeader == null) {
	out.println("no header present");
} else {
	out.println(pageHeader);
}
%>

2) header.inc :

Code:
<%

pageHeader = "header set in include file";

%>

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
No, the if is ont he include page:

page1.jsp:

Code:
<%

String pageHeader = null;

%>

<%@ include file="header.inc" %>

include.jsp

Code:
<%

if (pageHeader == null) {
    out.println("no header present");
} else {
    out.println(pageHeader);
}
%>
 
I don't understand

Shouldn't you include include.jsp instead of header.inc?

Cheers,
Dian
 
Hi,

What sedj has mentioned works fine. Here is one more example of IF statement in include file. The include file is not a jsp page it is .inc file.

Out put of below code is Hello World!

one.jsp
Code:
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body>
  <%
    String pageHeader = "Hello World!";
  %>
 <%@ include  file="include.inc"%>
  </body>
</html>

include.inc
Code:
<%
  if((null != pageHeader) || (!"".equals(pageHeader))){
    out.println(pageHeader);
  }else{
    out.println("No Header");
  }
%>

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top