Solve the Problem of assembling data as XML in JSP

Source: Internet
Author: User

I. application background

JSP obtains the List of requests placed in the Servlet and assembles the data in the List into XML. The following code is displayed as xml in Eclipse's built-in browser.

[Java]
/**
* News Servlet
* @ Author Xu Yue
*
*/
Public class ListServlet extends HttpServlet
{
Private static final long serialVersionUID = 1L;
Private VideoNewsService vs = new VideoNewsServiceImpl ();

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
DoPost (request, response );
}

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List <VideoNews> news = vs. readNews ();
Request. setAttribute ("lstnews", news );
Request. getRequestDispatcher ("/WEB-INF/pages/news. jsp"). forward (request, response );
}
}

/**
* News Servlet
* @ Author Xu Yue
*
*/
Public class ListServlet extends HttpServlet
{
Private static final long serialVersionUID = 1L;
Private VideoNewsService vs = new VideoNewsServiceImpl ();

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
DoPost (request, response );
}

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List <VideoNews> news = vs. readNews ();
Request. setAttribute ("lstnews", news );
Request. getRequestDispatcher ("/WEB-INF/pages/news. jsp"). forward (request, response );
}
}
[Html]
<% @ Page language = "java" contentType = "text/xml; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<? Xml version = "1.0" encoding = "UTF-8"?>
<VideoNews>
<C: forEach items = "$ {lstnews}" var = "n">
<News id = "$ {n. id}">
<Title >$ {n. title} </title>
<Length >$ {n. timeLength} </length>
</News>
</C: forEach>
</VideoNews>

<% @ Page language = "java" contentType = "text/xml; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<? Xml version = "1.0" encoding = "UTF-8"?>
<VideoNews>
<C: forEach items = "$ {lstnews}" var = "n">
<News id = "$ {n. id}">
<Title >$ {n. title} </title>
<Length >$ {n. timeLength} </length>
</News>
</C: forEach>
</VideoNews>

 

Ii. Discover Problems

Firefox reports XML parsing error: XML or text declaration is not at the beginning of the object

Chrome error: XML declaration allowed only at the start of the document

According to the error message, you can know the XML declaration <? Xml version = "1.0" encoding = "UTF-8"?> It must start with the document.


Iii. Problem Solving

Place page, taglib, and xml at the same time in the first line, followed by one. It's hard to see, but it solves the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.