初学ajax , 用 jsp 写一个用户登录的验证处理, ajax请求的页面返回的是xml格式的数据
为什么返回来 总报null
login.jsp
<%@ 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 'login.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">
-->
<script type="text/javascript">
//创建ajax引擎
function getAjax()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
return xmlHttp;
}
//获取我的html元素
function getMyHTML(serverPage, objID){
var ajax = getAjax();
//得到一个html元素,在下面给这个html元素赋值
var obj = document.getElementById(objID);
var data = document.getElementById("username").value;
//设置请求方法及目标,并且设置为异步提交
ajax.open("post",serverPage,true);
//
ajax.setRequestHeader("content-type","application/x-www-form-urlencoded");//这里。。。。
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
if(ajax.status == 200){
//innerHTML是HTML元素的属性,如果您不理解属性那就理解为HTML元素的变量
//ajax.responseText是服务器的返回值,把值赋给id=passport1的元素的属性
//innerHTML这个属性或说这个变量表示一组开始标记和结束标记之间的内容
window.alert(ajax.responseXML);
//返回来的是一个xml对象,我们要获取这个对象中的<mes>节点的信息
window.alert(ajax.responseXML.getElementsByTagName("mes"));
window.alert(ajax.responseXML.getElementsByTagName("mes")[0].childNodes[0].nodeValue);
}
}
}
//发送请求
ajax.send("username="+data);
}
function checkName(){
getMyHTML("checkname.jsp","result");
}
</script>
</head>
<body>
<form name="name_form" method=post>
<input id="username" type="text" name="username" onBlur="checkName()">username<br /><sapn id="result"></sapn>
<input id="passowrd" tyep="passwprd" name="password">password<br />
<br /> <input id="email" type="text" name="emial">email<br />
<br /> <input type="submit" name="submit" id="submit" value="submit">
</a>
</form>
</body>
</html>
接受页面 checkname.jsp
<%@ page contentType="text/xml; charset=utf-8"%>
<meta http-equiv="cache-control" content="no-cache">
<%
String action = "";
String username = "";
//检查用户名
//用作数据库联接,可以根据你的情况修改,如果为测试可以不用*作记号的语句
try {
action = request.getParameter("action");
username = request.getParameter("username");
if ("".equals(username)) {
out.println("<res><mes>用户名不能为空!</mes></res>");
} else if (username.length() < 4 || username.length() > 20) {
out.println("<res><mes>不合法!(长度为4到20位,且不能使用?#=等特殊字符)</mes></res>");
} else if ("zhangsan".equals(username)) {
out.println("<res><mes>用户名已被占用,请重新输入!</mes></res>");
} else {
out.println("<res><mes>用户可以用了</mes></res>");
}
} catch (Exception e) {
System.out.println(request.getServletPath() + " error : "
+ e.getMessage());
}
%>
------解决思路----------------------
jsp文件转xml时失败,建议把.jsp换成 servlet
Page directive: illegal to have multiple occurrences
of contentType with different values (old: text/html; charset=UTF-8, new: text/xml; charset=utf-8)
------解决思路----------------------
package com.apobates.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckNameServlet
*/
@WebServlet("/checkName")
public class CheckNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckNameServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/xml");
response.setCharacterEncoding("utf-8");
StringBuffer outStr=new StringBuffer();
//检查用户名
//用作数据库联接,可以根据你的情况修改,如果为测试可以不用*作记号的语句
try {
String action = request.getParameter("action");
String username = request.getParameter("username");
if ("".equals(username)) {
outStr.append("<res><mes>用户名不能为空!</mes></res>");
} else if (username.length() < 4
------解决思路----------------------
username.length() > 20) {
outStr.append("<res><mes>不合法!(长度为4到20位,且不能使用?#=等特殊字符)</mes></res>");
} else if ("zhangsan".equals(username)) {
outStr.append("<res><mes>用户名已被占用,请重新输入!</mes></res>");
} else {
outStr.append("<res><mes>用户可以用了</mes></res>");
}
PrintWriter pw=response.getWriter();
pw.print(outStr.toString());
pw.flush();
pw.close();
} catch (Exception e) {
System.out.println(request.getServletPath() + " error : "
+ e.getMessage());
}
}
}
你的html写的有几处错误:
<body>
<form name="name_form" method=post>
<input id="username" type="text" name="username" onBlur="checkName()">username<br /><span id="result"></span>
<input id="passowrd" type="password" name="password">password<br />
<br /> <input id="email" type="text" name="emial">email<br />
<br /> <input type="submit" name="submit" id="submit" value="submit">
</form>
</body>
把你的jsp换成servlet的地址