东软学习,AJAX的一个简单验证用户名实例

前端之家收集整理的这篇文章主要介绍了东软学习,AJAX的一个简单验证用户名实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

(1)以继承的方式配制ajax.png


(2)下面是一个简单的项目源码:

1.registeruser.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 'registeruser.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" src="/strutsDemo/js/jquery-1.7.2.js"></script>
	
	<script>
	
	
	$(document).ready(function(){
		checkname();
	});
	
		function checkname(){
			$("#loginname").blur(function(){
				var loginname=$("#loginname").val();
				$.ajax({
				   type: "POST",url: "/strutsDemo/checkuname/check",data: "loginname="+loginname,success: function(msg){
				    if(msg.result=="y"){
				    	alert("用户名已存在");
				    }
				   }
				});
			})
		}
	</script>
	
  </head>
  
  <body>
     <form action="" method="post">
  	用户名:<input id="loginname" type="text" name="loginname"/><br/>
  	密码:<input type="password" name="upwd"/><br/>
  	<input type="submit" value="登录"/>
  </form>
  </body>
</html>

2.struts-user1.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="checkuname" namespace="/" extends="struts-default,json-default">
		<action name="check" class="com.neusoft.action.CheckUname">
			<result name="success" type="json">
				
			</result>
		</action>
	</package>
   

</struts>


3.checkuname.java
package com.neusoft.action;

import java.sql.sqlException;

import com.neusoft.service.UserService;

public class CheckUname {
	
	private String loginname;
	private String result;
	
	private UserService us;
	
	public String execute(){
		us=new UserService();
		
		try {
			result=us.checkUname(loginname);
		} catch (sqlException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success";
	}
	
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
}	

(3)所需要的库文件


原文链接:https://www.f2er.com/ajax/164324.html

猜你在找的Ajax相关文章