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.