Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write jsp with xml syntax

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell what we have to do in order to write a jsp
with xml syntax
I tried to run a jsp with xml syntaxt but it is not giving the required output
for example
<%="hello"%>
<%
int i=0;
%>
<% int i=0;%>
<% for(i=0;i<8;i++)
{%>
out.println(<%=i%> ;
<%
}
%>
<%="hello2"%>
gives the output
hello int i=0; out.println(0); out.println(1); out.println(2); out.println(3); out.println(4); out.println(5); out.println(6); out.println(7); hello2
but if i try with xml syntax
<jsp:expression>
"hello"
</jsp:expression>
<jsp:scriptlet>
int i=0;
</jsp:scriptlet>
<jsp:scriptlet> for(i=0;i<8;i++)
{</jsp:scriptlet>
out.println(<jsp:expression>i</jsp:expression> ;
<jsp:scriptlet>
}
</jsp:scriptlet>
it gives weird output
"hello" int i=0; for(i=0;i<8;i++) { out.println(i); }
can any one pls clarify how to do it with xml syntax
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Made the code work:

First of all: Give yourself a favour and look for some debug strategy with jsp.
I allways have the generated java-file in the tomcat\work\localhost\theApp\what_ever_path_and_filename opened in textpad. Posted that yesterday, too. Trust me guys: Its a good idea.

Things discovered:
1. Tomcat only interprets jsp-files as xml-syntax, if you enclose all the tags in a jsp:root - root tag.
Because you omited that element, Tomcat thought it wouldn't be xml-syntax.
2. There were some more errors: For example out.println was outside an jsp:scriptlet-tag. And
out.println(<jsp:expression>i</jsp:expression>);
results in
out.println(out.print(i));
which causes a compiler error.
I am not very experienced in writing xml, but I think even for an experienced xml-guy the normal syntax is much easier to write than xml-syntax.
I think I have read somewhere that xml-syntax is targeted for jsp-generating tools, not for humans like us. This makes strong sense to me.
3. An interesting point is that you have to write for(i=0;i<8;i++){
as
for(i=0;i&lt;8;i++){
(btw. to post the &lt; in ubb without being converted into < is another story!)
You have to mask the characters which have a syntactical meaning for the xml-parser, like < (start tag). I believe the xml parser converts the &lt; in < before the jsp-engine starts generating the servlet.
A more clever alternative should be to enclose all java-code in cdata sections. Like:

(have not tested that syntax. Should work)
[ April 11, 2002: Message edited by: Axel Janssen ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think this explaination is fairly clear
http://www.javaranch.com/newsletter/Feb2002/newsletterfeb2002.jsp#feature
 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Axel
which server r u using?
I tried the same way as u told on Tomcat 3.3
but it also gives the same output
as i told in my first post
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gaurav Chikara:
Hi Axel
which server r u using?
I tried the same way as u told on Tomcat 3.3
but it also gives the same output
as i told in my first post

Hi Gaurav,
you see from Carls short, concise and informative article that xml-syntax is a new feature of jsp1.2.
Tomcat3 only supports jsp1.1. We need jsp1.2 for the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic