originalityAjax.html代码如下:
和servlet交互的,OriginalityAjaxAction.java代码如下:
- package web.action;
- 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;
- public class OriginalityAjaxAction extends HttpServlet {
-
- private static final long serialVersionUID = 1978049925174268801L;
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- this.doPost(request, response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
-
- String username = request.getParameter("username");
- response.setCharacterEncoding("UTF-8");
- PrintWriter out = response.getWriter();
-
- if(username.equals("admin")){
- out.print("用户:[admin]已经被使用了");
- }else{
- out.print("用户:[" + username + "]可以使用");
- }
- }
-
- }
web.xml的servlet配置如下:
- <servlet>
- <servlet-name>OriginalityAjaxAction</servlet-name>
- <servlet-class>web.action.OriginalityAjaxAction</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>OriginalityAjaxAction</servlet-name>
- <url-pattern>/OriginalityAjaxAction</url-pattern>
- </servlet-mapping>
以上简单的页面和servlet交互,返回一个字符串结果。
下面进行扩展,解析servlet返回的xml数据,为了保证代码的完整性,下面列出完整代码:
originalityAjaxXml.html
后台的catalog.xml数据代码:
- <?xml version="1.0" encoding="utf-8"?>
- <catalog>
-
- <product productId="1">
- <name>Nokia 6010中文的哈</name>
- <description>Easy to use without sacrificing style, the Nokia 6010 phone offers functional voice communication supported by text messaging, multimedia messaging, mobile internet, games and more</description>
- <price>99.99</price>
- <image>Nokia_6010.gif</image>
- <series>6000</series>
- <triband>false</triband>
- <camera>false</camera>
- <video>false</video>
- <highlight1>MMS</highlight1>
- <highlight2>Large color display</highlight2>
- <qtyInStock>2</qtyInStock>
- </product>
-
- <product productId="2">
- <name>Nokia 3100 Blue</name>
- <description>Light up the night with a glow-in-the-dark cover - when it‘s charged with light you can easily find your phone in the dark. When you get a call, the Nokia 3100 phone flashes in tune with your ringing tone. And when you snap on a Nokia Xpress-onâ„¢ gaming cover*, you‘ll get luminescent light effects in time to the gaming action.</description>
- <price>139</price>
- <image>Nokia_3100_blue.gif</image>
- <series>3000</series>
- <triband>true</triband>
- <camera>false</camera>
- <video>false</video>
- <highlight1>Glow-in-the-dark</highlight1>
- <highlight2>Flashing lights</highlight2>
- <qtyInStock>1</qtyInStock>
- </product>
-
- <product productId="3">
- <name>Nokia 3100 Pink</name>
- <description>Light up the night with a glow-in-the-dark cover - when it‘s charged with light you can easily find your phone in the dark. When you get a call, the Nokia 3100 phone flashes in tune with your ringing tone. And when you snap on a Nokia Xpress-onâ„¢ gaming cover*, you‘ll get luminescent light effects in time to the gaming action.</description>
- <price>139</price>
- <image>Nokia_3100_pink.gif</image>
- <series>3000</series>
- <triband>true</triband>
- <camera>false</camera>
- <video>false</video>
- <highlight1>Glow-in-the-dark</highlight1>
- <highlight2>Flashing lights</highlight2>
- <qtyInStock>7</qtyInStock>
- </product>
-
- <product productId="4">
- <name>Nokia 3120</name>
- <description>Designed for both business and pleasure, the elegant Nokia 3120 phone offers a pleasing mix of features. Enclosed within its chic, compact body, you will discover the benefits of tri-band compatibility, a color screen, MMS, XHTML browsing, cheerful screensavers, and much more.</description>
- <price>159.99</price>
- <image>Nokia_3120.gif</image>
- <series>3000</series>
- <triband>true</triband>
- <camera>false</camera>
- <video>false</video>
- <highlight1>Multimedia messaging</highlight1>
- <highlight2>Animated screensavers</highlight2>
- <qtyInStock>15</qtyInStock>
- </product>
-
- <product productId="5">
- <name>Nokia 3220</name>
- <description>The Nokia 3220 phone is a fresh new cut on some familiar ideas - animate your MMS messages with cute characters, see the music with lights that flash in time with your ringing tone, download wallpapers and screensavers with matching color schemes for the interface.</description>
- <price>159.99</price>
- <image>Nokia_3220.gif</image>
- <series>3000</series>
- <triband>false</triband>
- <camera>true</camera>
- <video>false</video>
- <highlight1>MIDI tones</highlight1>
- <highlight2>Cut-out covers</highlight2>
- <qtyInStock>5</qtyInStock>
- </product>
-
- </catalog>
OriginalityAjaxXmlAction.java代码如下:
- package web.action;
- import java.io.File;
- 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.io.SAXReader;
- public class OriginalityAjaxXmlAction extends HttpServlet {
-
- private static final long serialVersionUID = 1978049925174268801L;
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- this.doPost(request, response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/xml;charset=utf-8");
- response.setHeader("Cache-Control", "no-cache");
- String path = request.getSession().getServletContext().getRealPath("/") + "catalog.xml";
- PrintWriter out = response.getWriter();
-
- SAXReader saxReader = new SAXReader();
- Document document = null;
- try {
- document = saxReader.read(new File(path));
- } catch (DocumentException e) {
- e.printStackTrace();
- }
-
- String xml = document.asXML();
- System.out.println(xml);
- out.print(xml);
- }
-
- }
比较完整的原始ajax写法
原文:http://www.cnblogs.com/bb3q/p/4546231.html