The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How to write jsp with xml syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "How to write jsp with xml syntax" Watch "How to write jsp with xml syntax" New topic
Author

How to write jsp with xml syntax

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
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

Joined: Jun 13, 2000
Posts: 3340
Actually I think this explaination is fairly clear
http://www.javaranch.com/newsletter/Feb2002/newsletterfeb2002.jsp#feature


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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

Joined: Jan 08, 2001
Posts: 2164
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.
 
 
subject: How to write jsp with xml syntax
 
Threads others viewed
Problem with XML syntax
JSP document (XML syntax)
Converting JSP to XML
XML VIEW OF JSP PAGE:
jsp:text question
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com