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.