HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
When sending XML data via HTTP, it is necessary to use JSP to handle incoming and outgoing XML documents, such as RSS document. As an XML document, it is just a bunch of text only, create an XML document using JSP is not difficult than creating an HTML document.
Send XML content using JSP and send it as HTML content. The only difference is that you need to put the page context attribute is set to text / xml. To set the context attribute, use <% @ page%> command, like this:
<%@ page contentType="text/xml" %>
The next example XML content sent to the browser:
<%@ page contentType="text/xml" %> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> </books>
Use a different browser to access this example, look at the examples presented in the document tree.
Before using the JSP process XML, you need to XPath and XML-related files in the two libraries <Tomcat Installation Directory> \ lib directory:
books.xml file:
<books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> <book> <name>Great Mistry</name> <author>NUHA</author> <price>2000</price> </book> </books>
main file:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="https://www.oracle.com/java/technologies/" %> <%@ taglib prefix="x" uri="https://www.oracle.com/java/technologies/" %> <html> <head> <title>JSTL x:parse Tags</title> </head> <body> <h3>Books Info:</h3> <c:import var="bookInfo" url="http://localhost:8080/books.xml"/> <x:parse xml="${bookInfo}" var="output"/> <b>The title of the first book is</b>: <x:out select="$output/books/book[1]/name" /> <br> <b>The price of the second book</b>: <x:out select="$output/books/book[2]/price" /> </body> </html>
Visit http: // localhost: 8080 / main, results are as follows:
BOOKS INFO: The title of the first book is:Padam History The price of the second book: 2000
This is the XSLT stylesheet style.xsl file:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl= "#" version="1.0"> <xsl:output method="html" indent="yes"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="books"> <table border="1" width="100%"> <xsl:for-each select="book"> <tr> <td> <i><xsl:value-of select="name"/></i> </td> <td> <xsl:value-of select="author"/> </td> <td> <xsl:value-of select="price"/> </td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
This is main file:
<%@ taglib prefix="c" uri="https://www.oracle.com/java/technologies/" %> <%@ taglib prefix="x" uri="https://www.oracle.com/java/technologies/" %> <html> <head> <title>JSTL x:transform Tags</title> </head> <body> <h3>Books Info:</h3> <c:set var="xmltext"> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> <book> <name>Great Mistry</name> <author>NUHA</author> <price>2000</price> </book> </books> </c:set> <c:import url="http://localhost:8080/style.xsl" var="xslt"/> <x:transform xml="${xmltext}" xslt="${xslt}"/> </body> </html>
Results are as follows:
More on Using JSTL to handle XML content, please consult JSP Standard Tag Library .