Resolving problems with XML assembled data in JSP

Source: Internet
Author: User
Tags foreach final parse error first row

First, the application background

The JSP gets the list of the request in the servlet and the data in the list is assembled into XML. The following code appears as XML in the eclipse's built-in browser, no problem.

[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} ">
<t Itle>${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. problems identified

Bug in Firefox: XML parse Error: XML or text declaration not at the beginning of an entity

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

Depending on the error message, you know that the XML declaration <?xml version= "1.0" encoding= "UTF-8"?> must be at the beginning of the document.


Iii. Problem-solving

Place page, taglib, and XML at the same time in the first row, one after the other. It's not good, but it solves the problem.

Related Article

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.