解決JSP中拼裝數(shù)據(jù)為XML出現(xiàn)的問題_JSP教程
推薦:jsp如何解決Form表單亂碼問題網(wǎng)站程序中的Form表單,是重要的組成內(nèi)容之一,很多時(shí)候,F(xiàn)orm表單會(huì)出現(xiàn)亂碼的現(xiàn)象,會(huì)給網(wǎng)站造成一定的困擾。如何解決亂碼的問題? JSP和Servlet的六種中文亂碼處理方法 一、表單提交時(shí)出現(xiàn)亂碼: 在進(jìn)行表單提交的時(shí)候,經(jīng)常提交一些中文,自然就避免不了出現(xiàn)中文亂
一、應(yīng)用背景
JSP取得Servlet中放入request的List,將List中的數(shù)據(jù)拼裝成XML。以下代碼在Eclipse的內(nèi)置瀏覽器中顯示為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>
二、發(fā)現(xiàn)問題
火狐中報(bào)錯(cuò):XML解析錯(cuò)誤:XML 或文本聲明不在實(shí)體的開頭
chrome報(bào)錯(cuò):XML declaration allowed only at the start of the document
根據(jù)錯(cuò)誤信息,可以知道XML聲明<?xml version="1.0" encoding="UTF-8"?>必須在文檔的開頭。
三、解決問題
將page、taglib、xml同時(shí)放在第一行即可,一個(gè)接一個(gè)的后面。雖然不好看,但是解決問題哦
分享:jsp上傳圖片即時(shí)顯示效果代碼scriptfunction setImagePreview() { var docObj=document.getElementById(doc); var imgObjPreview=document.getElementById(preview);if(docObj.files docObj.files[0]){ //火狐下,直接設(shè)img屬性 imgObjPreview.style.display = 'block'; imgObjPreview.style.width
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁模板:關(guān)于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對(duì)路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對(duì)路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說明:
JSP教程-解決JSP中拼裝數(shù)據(jù)為XML出現(xiàn)的問題
。