I am developing for google app engine with JSP. And I need to compare two strings by startsWith() method in String class.
Here is the code I am working on.
<%
String artist = "Surendra Perera";
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key songKey = KeyFactory.createKey("songs", 123454);
// Run an ancestor query to ensure we see the most up-to-date
// view of the songs.
Query query = new Query("Song", songKey).addSort("Artist");
//query.addFilter("Artist", Query.FilterOperator.IN, "Milton Mallawarachchi");
List<Entity> songsList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(30));
if(songsList.isEmpty()){
%>
<p>There are no songs</p>
<%
}else{
%>
<ul class="playlist">
<%
for(Entity song : songsList){
if(artist.startsWith(song.getProperty("Artist"))){
%>
<li><a href="<%= song.getProperty("Link") %>"><%= song.getProperty("Title") %>  <span class="comment"><%= song.getProperty("Artist") %></span></a></li>
<% }}} %>
And here is the error i am getting….
HTTP ERROR 500
Problem accessing /search.jsp. Reason:
Unable to compile class for JSP:
An error occurred at line: -1 in the generated java file
[javac] C:\DOCUME~1\SILICO~1\LOCALS~1\Temp\Jetty_127_0_0_1_8888_war____.g0qk00\jsp\org\apache\jsp\search_jsp.java:178: cannot find symbol
[javac] symbol : method startsWith(java.lang.Object)
[javac] location: class java.lang.String
[javac] if(artist.startsWith(song.getProperty("Artist"))){
[javac] ^
[javac] 1 error
Stacktrace:
Caused by:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: -1 in the generated java file
[javac] C:\DOCUME~1\SILICO~1\LOCALS~1\Temp\Jetty_127_0_0_1_8888_war____.g0qk00\jsp\org\apache\jsp\search_jsp.java:178: cannot find symbol
[javac] symbol : method startsWith(java.lang.Object)
[javac] location: class java.lang.String
[javac] if(artist.startsWith(song.getProperty("Artist"))){
[javac] ^
[javac] 1 error
Thanks in advance!
The error is telling that it cannot find the given method
method startsWith(java.lang.Object)in theStringclass. Please note that the error saysjava.lang.Objectas method argument. This is indeed wrong. It has to bejava.lang.String, see also the javadoc.You have to change the return type of
song.getProperty()fromObjecttoString:Or to add a cast on
(String)if it is actually ofStringtype:Or to use a fullworthy Javabean:
with
Last but not least, this problem is unrelated to JSP. You would have exactly the same problem when doing so in a normal Java class. Writing Java code in a JSP file is not the best way to get the basic concepts right.