博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(四)XML基础(客户端和服务端发送与接收xml数据)
阅读量:6852 次
发布时间:2019-06-26

本文共 3644 字,大约阅读时间需要 12 分钟。

案例:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%>My JSP 'index.jsp' starting page
使用ajax来发送与接收XML的数据

XmlServlet.java

package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;public class XmlSevlet extends HttpServlet {    /**     * The doGet method of the servlet. 
* * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 获取客户端传过来的xml字符串,并解析为xml对象 String xml_str = request.getParameter("xmlStr"); try { Document document = DocumentHelper.parseText(xml_str); Element rootElement = document.getRootElement(); Element element1 = rootElement.element("driverClassName"); Element element2 = rootElement.element("url"); Element element3 = rootElement.element("username"); Element element4 = rootElement.element("password"); System.out.println(element1.getText()); System.out.println(element2.getText()); System.out.println(element3.getText()); System.out.println(element4.getText()); } catch (DocumentException e) { e.printStackTrace(); } /** * 要返回一个XML的字符串。客户端的jQuery接收到之后,会自动将XML的字符串转为XML的文档对象。 * 因为JQuery。post(,,,"xml") 会把返回的字符串对象转为xml对象 */ PrintWriter out = response.getWriter(); StringBuffer str_xml = new StringBuffer(); str_xml.append("
"); for (int i = 0; i < 5; i++) { str_xml.append("
"); str_xml.append("
用户"+i+"
"); str_xml.append("
12
"); str_xml.append("
"); } str_xml.append("
"); out.print(str_xml.toString()); System.out.println(str_xml.toString()); out.flush(); out.close(); }}

结果:

 

 



 

 本章所有代码都在: 

 

转载于:https://www.cnblogs.com/shyroke/p/6820521.html

你可能感兴趣的文章
简说设计模式——模板方法模式
查看>>
php 图片写字
查看>>
linux之权限
查看>>
Deep Learning(深度学习)学习笔记整理系列之(三)
查看>>
网页布局之Div vs Table (2)
查看>>
可变参数列表
查看>>
BouncyCastle.Crypto的RSA算法调用源码
查看>>
vs2017 + Python3.6 +Django1.11 连接mysql数据库
查看>>
一张思维导图带你梳理HashMap相关知识
查看>>
MVC 从Excel导入到DataTable
查看>>
Symbol
查看>>
Selenium WebDriver + IE11 问题汇总
查看>>
Oracle数据库设置Scott登录
查看>>
IOS开发之UIScrollVIew运用
查看>>
iOS 基础-----关于UIView 的 frame 与 bounds
查看>>
ISO GPS定位,坐标转换以及如何显示
查看>>
深入理解Java:类加载机制及反射
查看>>
Use a PowerShell Module to Easily Export Excel Data to CSV
查看>>
Redis清理
查看>>
读书笔记—CLR via C#章节8-10
查看>>