使用jQuery ajax + Struts 一个登陆的例子.

下面是一个Jsp页面的代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<script type="text/javascript" src="/jQuery/jQuery/jquery-1.2.6.pack.js"></script>
<%
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>
    <script type="text/javascript">
   
    /*** 
    jQuery(function(){
		$("#user_name").blur(function(){
			var name= $.trim($("#user_name").val());
			$.ajax({
				url:"/jQuery/myServlet",
				data:{username:name},
				cache:false,
				success:function(data){
					if(data=="true"){
						alert("姓名可以使用!");
					}else{
						alert("姓名不能使用!");
					}	
				}
			})
		})
	});	
***/
	
	//提交;
	function checkSubmit(){
		var bol = true;
		var username = $.trim($("#user_name").val());
		var password = $.trim($("#password").val());
		if(username==null || username==""){
			$("#userDivId").show();
			$("#userDivId").html("用户名不能为空!");
			$("#msgDIVID").hide();
			$("#user_name").focus();
			bol= false;
		}else{
			$("#userDivId").hide();
		}
		
		if(password==null || password==""){
			$("#passDivId").show();
			$("#passDivId").html("密码不能为空!");
			$("#msgDIVID").hide();
			$("#password").focus();
			bol=false;
			
		}else{
			$("#passDivId").hide();
		}
		if(bol!=false){
			$.ajax({
				url:"/jQuery/login.do?p=loginCheck",
				data:{uname:username,upass:password},
				success:function(data){
					if(data=="1"){
						$("#msgDIVID").show();
						$("#msgDIVID").html("恭喜您,登录成功!");
					}else if(data=="2"){
						$("#msgDIVID").show();	
						$("#msgDIVID").html("抱歉,由于你的账号或密码登录失败!");
					}
				}
			});
		}
	}
    </script>
  </head>
  
  <body >
  		<form name="myform">
  		<table>
  		<tr>
  			<td>UserName:</td>
  			<td><input type="text" id="user_name"></td>
  			<td><div id="userDivId" style="border: 0px solid red;width:200px;height:10px;color: red"></div></td>
  		</tr>
  		
  		<tr>
  			<td>PassWord:</td>
  			<td><input type="password" id="password" size="22" name="password" /></td>
  			<td><div id="passDivId" style="border: 0px solid red;width:200px;height:10px;color: red;"></div></td>
  		</tr>
    	
    	<tr>
    		<Td><input type="reset"/ value="重置"></Td>
    		<Td><button name="btnSubmit" onclick="checkSubmit()">提交</button></Td>
    	</tr>
    	
    	</table>
    	
    	<Br>
    	<!-- 显示登陆信息的div层. -->
    	<div id="msgDIVID" style="border:0px solid blue;color: blue;width:400px;height:30;font-weight: bold;font-size: 20px"></div>
    	</form>
  </body>
</html>

 

 

 

下面一个是Struts Action里面的代码:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package pack.java.demo.struts.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/** 
 * MyEclipse Struts
 * Creation date: 11-04-2010
 * 
 * XDoclet definition:
 * @struts.action parameter="p" validate="true"
 */
public class LoginAction extends DispatchAction {
	/**
	 * 分发Action;验证登陆方法;
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward loginCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		//根据用户名请求;
		String username=request.getParameter("uname");
		//根据密码请求;
		String password=request.getParameter("upass");
		//因为没有连接数据库,就直接用用户名和密码;
		if(username.equals("zhouhaitao") && password.equals("123456")){
			try {
				response.getWriter().print("1");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				response.getWriter().print("2");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}

 

你可能感兴趣的:(apache,jquery,Ajax,MyEclipse,struts)