我惨痛的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 <input type="text" id="username" />
<br/>
pwd  <input type="password" id="password" />
<br/>
<input type="checkbox" id="memery" value="memery" />记住用户名   
<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值