JSP error

[nobr]I am learning how to use JSPs, and I am running into some trouble. Here is my code for the JSP that I am trying to run using Apache Tomcat version 5.5:

<HTML>

<HEAD>

<!-- Example2 -->

<TITLE> JSP loop</TITLE>

</HEAD>

<BODY>

<font face=verdana color=darkblue>

JSP loop

<BR> <BR>

<%!

public String writeThis(int x)

{

String myText="";

for (int i = 1; i < x; i )

myText = myText"<font size=" i" color=darkred face=verdana>VisualBuilder JSP Tutorial</font><br>" ;

return myText;

}

%>

This is the error that I am recieving when I try to open it from http://localhost:8080/secondjsp.jsp .

Exception report

message

description The server encountered an internal error () that prevented it from fulfillingthis request.

exception

org.apache.jasper.JasperException: Unable to compileclassfor JSP:

An error occurred at line: 14 in the jsp file: /secondjsp.jsp

Syntax error, insert"AssignmentOperator ArrayInitializer" to complete ForUpdate

11:public String writeThis(int x)

12:{

13: String myText="";

14:for (int i = 1; i < x; i )

15:myText = myText"<font size=" i" color=darkred face=verdana>VisualBuilder JSP Tutorial</font><br>" ;

16:

17:return myText;

An error occurred at line: 15 in the jsp file: /secondjsp.jsp

Syntax error on tokens, delete these tokens

12:{

13: String myText="";

14:for (int i = 1; i < x; i )

15:myText = myText"<font size=" i" color=darkred face=verdana>VisualBuilder JSP Tutorial</font><br>" ;

16:

17:return myText;

18:}

Stacktrace:

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)

org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)

org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.

I looked around a little bit, and I think that maybe I messed up when setting the ClassPath. I couldn't find a clear answer though. I'm using a tutorial to learn this. Here is the link to that: http://www.visualbuilder.com/jsp/tutorial/pageorder/13/

I'd appreciate any help or advice you could give me.

Thanks,

RH[/nobr]

[4111 byte] By [rhaup0317a] at [2007-11-27 5:08:35]
# 1
Use + to concatenate strings.String s = "foo" + i + "bar";
BalusCa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks for the tip, but I am still getting the error that says that it is unable to compile the class for JSP.
rhaup0317a at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

i somehow think it is a basic thing...

have you added

<%@ page language="java" %>

and imported all the related classes ?

and btb what is that you are trying out in the below snippet

for (int i = 1; i < x; i )

this would be an infinite loop

is something like

for (int i = 1; i < x; i++)

and why dont you make use of StringBuffer instead of a String ?

RahulSharnaa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Like I said earlier, I am new to JSPs and Java so I am using a tutorial right now to become more comfortable with it. The code is copied and pasted from the tutorial to a jsp file, but this error keeps coming up. To be honest with you I don't know how to import related classes because I don't know what that means, I don't know why they are using an infinite loop, and I have no idea what the difference between a String and Stringbuffer is. If you could "dumb it down" for me, I would really appreciate it.

rhaup0317a at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

First start learning Java: http://java.sun.com/docs/books/tutorial/

Then go ahead with Java EE: http://java.sun.com/javaee/5/docs/tutorial/doc/

The difference between String and StringBuffer is that the later is reliable faster in loops. But start learning Java and Java EE first, then you will get used with the default API.

BalusCa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Okay, but what is this error:org.apache.jasper.JasperException: Unable to compile class for JSP:
rhaup0317a at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

This is the main exception. The JSP cannot be compiled. Roughly this just means that the actual coding is bad. This is not an functional (runtime) error.

How to code in Java is covered by the basic Java tutorial. Once you get used with Java (read: once you never got compilation errors, or once you can solve them yourselves), then proceed with Java EE ;)

BalusCa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

[nobr]Well first of all please don't by trying things before being comfortable on basics you'd alwys screw up for the sake of nothing.

There is no harm in spending a week more on core java and concepts of advance java like Jdbc,servlets,rmi & others before you step into JSP and implementations of JAVA EE.

Hope this advice doesn't hurt you. coming back to our issue.

try the below snippet and make sure you save the file with an extension of ".jsp"

<%@ page language="java" %>

<HTML>

<HEAD>

<!-- Example2 -->

<TITLE> JSP loop</TITLE>

</HEAD>

<BODY>

<font face=verdana color=darkblue>

JSP loop

<BR> <BR>

<%!

public String writeThis(int x) {

StringBuffer myText = new StringBuffer();

for (int i = 1; i < x; i )

myText.append("<font size=\""+i+"\" color="darkred\" face=\"verdana\">VisualBuilder JSP Tutorial</font><br>") ;

return myText.toString() ;

}

%>

Result : <%=writeThis(5)%>

I'm sure some of this might sound strange.however,it wud be great if you can go about following my advice know in and out of (basic level) core java and few concepts of Adv java and then try experimenting with JSP/EJB.....& wat so ever...because that wud help you as time passes by...

REGARDS,

RaHuL[/nobr]

RahulSharnaa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

myText = myText "<font size=" i " color=darkred face=verdana>VisualBuilder JSP Tutorial</font>

" ;

It ll be replaced by the following line.

myText = myText+ "<font size=" i " color=darkred face=verdana>VisualBuilder JSP Tutorial</font>

" ;

art84a at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Are you sure?And Rahul: you're forgetten the i++ in the loop ;)
BalusCa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

[nobr]:D thanks for reminding wat i point

and had one more too color="darkred\" :((

<%@ page language="java" %>

<HTML>

<HEAD>

<!-- Example2 -->

<TITLE> JSP loop</TITLE>

</HEAD>

<BODY>

<font face=verdana color=darkblue>

JSP loop

<BR> <BR>

<%!

public String writeThis(int x) {

StringBuffer myText = new StringBuffer();

for (int i = 1; i < x; i++ )

myText.append("<font size=\""+i+"\" color=\"darkred\" face=\"verdana\">VisualBuilder JSP Tutorial</font><br>") ;

return myText.toString() ;

}

%>

Result : <%=writeThis(5)%>

aaahhhKkkk such an embracing thing... :((

Thanks a lot bro....

god bless you

tc !!

REGARDS,

RaHuL[/nobr]

RahulSharnaa at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
thanks man! you rule!
rhaup0317a at 2007-7-12 10:28:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...