So I have a servlet which prints content of various files. But when I want to print .xml file my servlet page doesn't print anything, because page uses this xml tags as html and is parsing them istead of printing. And I want to print this tags. I am reading file line by line and lines are stored in variable line.
-
You must return the correct content type header. Which platform you use for your application? – noway Aug 20 '12 at 11:05
-
Windows, NetBeans, but it have to work on linux (it's jboss servlet). Reading file and writing it out is in java code. – user15683854875644328975643872 Aug 20 '12 at 11:09
2 Answers
If you are attempting to Display XML as content in an HTML document:
Browsers can't tell the difference better a <
that the author intends to mean "Start of tag" and one that the author intends to mean "Render this".
You need to represent it as <
if you want it to appear as data.
The answer to htmlentities equivalent in JSP? explains how to convert a string of text into a string of HTML.
If you are attempting to Output an XML document instead of an HTML document:
You need to specify an XML content type (such as application/xml
) instead of an HTML content-type.
See How to set the content type on the servlet for an explanation.
-
First case, it's working, thanks. But if someone know a method without writing new function I'd be thankful. Greets. – user15683854875644328975643872 Aug 20 '12 at 11:26
If you want to print xml content in your HTMl page, you can use StringEscapeUtils.escapeHtml() function from Apache commons lang library to write xml file contents to your HTML page
PrintWriter writer = response.getWriter();
writer.write("<html><head></head><body>");
writer.write(StringEscapeUtils.escapeHtml(xmlContent);
writer.write("</body></html>");

- 1,092
- 1
- 8
- 11