Problems with assembling data into XML in JSP

Source: Internet
Author: User

1. Use the background JSP to obtain the List of requests in the Servlet and assemble 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; videprivate onewsservice vs = new VideoNewsServiceImpl (); protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {doPost (request, response);} protected void doPost (HttpServletRequest request, response) throws ServletException, IOException {List <VideoNews> news =. 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 response (); protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {doPost (request, response);} protected void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {List <VideoNews> news =. 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> 2. the following error occurs in Firefox: XML parsing error: XML or text declaration does not start with an object. chrome reports an error: XML declaration allowed only at the start of the document. According to the error message, you can see the XML declaration. <? Xml version = "1.0" encoding = "UTF-8"?> It must start with the document. 3. Solve the problem by placing 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.