0

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.

2
  • You must return the correct content type header. Which platform you use for your application? Commented Aug 20, 2012 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. Commented Aug 20, 2012 at 11:09

2 Answers 2

2

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>");
Sign up to request clarification or add additional context in comments.

Comments

2

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 &lt; 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.

1 Comment

First case, it's working, thanks. But if someone know a method without writing new function I'd be thankful. Greets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.