ajax一个登陆的例子

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" 


%>
<%
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>用户登陆</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" src="${pageContext.request.contextPath }/js/jquery-1.4.2.js"></script>
<script type="text/javascript" charset="utf-8">

function loginzp(){
var xmlHttp=false;
var username=document.zpform.username.value;
var password=document.zpform.password.value;

var url="/testajax/log/log1?username="+username+"&password="+password;
if(window.XMLHttpRequest){//Mozilla等浏览器
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){//IE浏览器
try{
xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
window.alert("该浏览器不支持Ajax");
}
}

}

xmlHttp.open("post",url,true);
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4&&xmlHttp.status == 200){ //xmlHttp.readyState==4请求是否完成,xmlHttp.status == 200请求是否成功
//resultDiv.innerHTML = xmlHttp.responseText; //得到响应值
var str = xmlHttp.responseText.replace(/^\s*/,"").replace(/\s*$/,"");
//alert("\""+xmlHttp.responseText+"\"");  
alert(str);//判断是false或是true
//alert(typeof('success'));
alert(str=="success");//输出响应信息
if(str=="success") //加上.responseText.replace(/^\s*/,"").replace(/\s*$/,"")或(replace(/^\s*|\s*$/g, ''))替换前后空格,否则也可用数字判断,而用中文或字母不行(在页面loginSuccess中设的值是success,longinFail中设置的值为0)
{
window.location.href="/testajax/log/log2";//又通过一次struts跳转到另外一个页面
}
//alert(str);
//alert(str==0);//输出响应信息
if(str==0) {
document.getElementById("s1").innerHTML = "登录名不存在!";//输出错误信息



}else{
document.getElementById("resultDiv").innerHTML = "正在登录,请稍后......";
}
}
//alert(url);//F12调试模式查看输出
xmlHttp.send();
}



</script> 


  </head>
  
  <body>欢迎进入学生管理系统<hr/>
      
    <form name="zpform">
    用户名:<input type="text" name="username"><span id="s1"></span><br>
    密码:<input type="password" name="password"><span id="s2"></span><br>
   
    <input type="button" value="登录" οnclick="loginzp()"/><!-- 此处按钮必须为为button,否则会造成表单提交,界面刷新,不是Ajax效果 -->
    </form>
     <div id="resultDiv"></div>
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值