开发者

How to write trivial JSP that just returns static XML page

I'm trying to write a trivial JSP that returns th开发者_StackOverflowe contents of a static xml file. I need to run this in tomcat. Eventually, this will be more dynamic, but at first, I just want to return an xml file. Can anyone point me to a demo for such a trivial beast, I'm trying to learn what are the minimum chunks I need to create the web app and install in tomcat.

Mucho appreciato,

pawpaw17


Following this document is always a good start.

But you may have issues.

First, it's basically trivial to do something like:

http://example.com/app/mydynamicxml.jsp

that returns an XML blob. Just paste the XML in to that file.

But it won't have an XML content type. You can fix that by adding directives to the JSP:

<%@page contentType="application/xml" %>

However, that brings on MORE problems.

Specifically, an XML file CAN NOT start with white space. It MUST start with <?.

That directive will very likely insert a blank line in to you XML file.

So, what you really want is:

<%@page contentType="application/xml" %><?xml version...

Finally, there IS a JSPX version of JSP, which uses an XML syntax, and avoids all of those white space issues. There's also a directive to Tomcat that can eliminate the white space issue. But, out the gate, this is the fastest, "obvious" tact to take.


The main thing would be to specify the content type as <%@ page contentType="text/xml" %>

<%-- Set the content type 
--%><%@ page contentType="text/xml" %><%-- 
--%><?xml version="1.0" encoding="UTF-8"?>
<root><entry key="key1" value="value1" /><entry key="key2" /></root>

Check out the article on Sun site


Tried adding a trimDirectiveWhitespaces="true" page directive, but that wasn't supported on my server.

The solution was simply to remove any newlines after any page directives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜