我惨痛的jquery+js+xml+servlet+jsp(html)经历

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
  <script type="text/javascript" src="js/ajaxXML.js"></script>
  <body>
    <form id="myform" name="myform">
  name&nbsp;<input type="text" id="username" />
  <br/>
  pwd&nbsp;&nbsp;<input type="password" id="password" />
  <br/>
  <input type="checkbox" id="memery" value="memery" />记住用户名&nbsp;&nbsp;&nbsp;
  <input type="checkbox" id="memery" value="memery" />SSL安全登录
  <br/>
  <input type="button" id="submit" value="测试"/>
  </form>
  <div id="result" ></div>
  </body>
</html>
///////////////////////////////////////////////////////////////////////////
$(function() {
$("#submit").click(function() {
var username = $("#username").val(),
pwd = $("#password").val();
var requestInfo = {'username' : username, 'password' : pwd};
var urlInfo = "servlet/XMLAjax";
var typeInfo = "POST";
var dataTypeInfo = "xml";
$.ajax({
url : urlInfo,
type : typeInfo,
dataType : dataTypeInfo,
data : requestInfo,
error : errorF,
success : successF
});
});
});

function errorF(XMLHttpRequest, textStatus, errorThrown) {
    // 通常 textStatus 和 errorThrown 之中
    // 只有一个会包含信息
alert(XMLHttpRequest + "===" + textStatus + "===" + errorThrown);
    this; // 调用本次AJAX请求时传递的options参数
}

function successF(dataInfo) {
    // dataInfo 可能是 xmlDoc, jsonObj, html, text, 等等...
$(dataInfo).find("record").each(function(i) {
var usernameVal = $(this).find("name").text(),
pwdVal = $(this).find("password").text();
alert(usernameVal + "===" + pwdVal);
if (usernameVal && pwdVal && typeof pwdVal !== 'undefined' && typeof usernameVal !== 'undefined') {
$("#result").append("<b>name=</b>" + usernameVal + "<br/><b>password=</b>" + pwdVal + "<br/>");
}
});
   $("#myform").css("display","none");
   this; // 调用本次AJAX请求时传递的options参数
}
//////////////////////////////////////////////////////////////////////////
package com.server;

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.DocumentHelper;
import org.dom4j.Element;

public class XMLAjax extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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.setContentType("text/xml");
PrintWriter out = response.getWriter();
String nameStr = request.getParameter("username").trim(),
pwdStr = request.getParameter("password").trim();
Document doc = DocumentHelper.createDocument();
Element recordsE  = doc.addElement("records");
for (int i = 0; i < 2; ++i) {
Element recordE  = recordsE.addElement("record");

Element nameE  = recordE.addElement("name");
nameE.addText(nameStr);

Element pwdE  = recordE.addElement("password");
pwdE.addText(pwdStr);
}
System.out.println(doc.asXML());
out.println(doc.asXML());
out.flush();
out.close();
}
}
/////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>XMLAjax</servlet-name>
    <servlet-class>com.server.XMLAjax</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>XMLAjax</servlet-name>
    <url-pattern>/servlet/XMLAjax</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

你可能感兴趣的:(html,jquery,jsp,xml,servlet)