解决JSP中拼装数据为XML出现的问题_JSP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:jsp如何解决Form表单乱码问题
网站程序中的Form表单,是重要的组成内容之一,很多时候,Form表单会出现乱码的现象,会给网站造成一定的困扰。如何解决乱码的问题? JSP和Servlet的六种中文乱码处理方法 一、表单提交时出现乱码: 在进行表单提交的时候,经常提交一些中文,自然就避免不了出现中文乱

 一、应用背景

JSP取得Servlet中放入request的List,将List中的数据拼装成XML。以下代码在Eclipse的内置浏览器中显示为xml,没有问题。

[java]
/**
* 新闻Servlet
* @author 徐越
*
*/
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);
}
}

/**
* 新闻Servlet
* @author 徐越
*
*/
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>

 

二、发现问题

火狐中报错:XML解析错误:XML 或文本声明不在实体的开头

chrome报错:XML declaration allowed only at the start of the document

根据错误信息,可以知道XML声明<?xml version="1.0" encoding="UTF-8"?>必须在文档的开头。


三、解决问题

将page、taglib、xml同时放在第一行即可,一个接一个的后面。虽然不好看,但是解决问题哦

分享:jsp上传图片即时显示效果代码
scriptfunction setImagePreview() { var docObj=document.getElementById(doc); var imgObjPreview=document.getElementById(preview);if(docObj.files docObj.files[0]){ //火狐下,直接设img属性 imgObjPreview.style.display = 'block'; imgObjPreview.style.width

来源:模板无忧//所属分类:JSP教程/更新时间:2013-04-17
相关JSP教程