使用AJAX的简单案例

servle 实现逻辑操控 

package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//获取账号密码
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		String loginstate = "Fail";
		String tagetUrl = "/infp.jsp";
//当密码跟账号相等 存入SEESION 跳转到成功页面
		if(account.equals(password)){
			loginstate = "Success";
			tagetUrl = "/suceess.jsp";
			HttpSession session = req.getSession();
			session.setAttribute("account", account);
		}
//如果不相等则跳转失败页面
		
		req.setAttribute("loginstate", loginstate);
		ServletContext application = this.getServletContext();
		RequestDispatcher rd = application.getRequestDispatcher(tagetUrl);
		rd.forward(req, resp);
	}
		
	
	
}

注册页面使用AJAX技术

<%@ 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>
  
  <body onLoad="initAjax()">
  	<script language="javascript">
  		function login(){
  		//步骤1 获取当前的值
  		var account = document.loginForm.account.value;
  		var password = document.loginForm.password.value;
  		//实例化 异步对象
  		var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  		var url = "servlet/LoginServlet?account="+account+"&password="+password; 
  		xmlHttp.open("POST",url,true);
  		xmlHttp.onreadystatechange = function(){
  			if(xmlHttp.readyState==4){
  				resultDiv.innerHTML = xmlHttp.responseText;
  			}else{
  				resultDiv.innerHTML += "loging...";
  			}
  			
  		};
  		xmlHttp.send();
  		
  		}
  	
  	
  	
  	</script>
  	
  <script language="javascript">
  var xmlHttp = false;
  function initAjax(){
  	if(weindow.XMLHttpRequest){
  		xmlHttp = new XMLHttpRequest;
  	}else if(window.ActiveXObject){
  	try{
  	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  	}catch(e){
  		try{
  			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}catch(e){
  			window.alert("不支持该浏览器");
  		}
  	}
  	}
  
  
  }
  
  
  	</script>
  
  	欢迎登陆学生管理系统:<hr>
  	<div id = "resultDiv">
  		<form name = "loginForm">
  			账号:<input type = "text" name="account"><br>
  			密码:<input type = "password" name = "password"><br>
  			<input type = "button" value = "login" onclick="login()">
  		
  		</form>
  	
  	
  	</div>
  </body>
</html>

web.xml的配置 

   <servlet>
  	<servlet-name>a</servlet-name>
  	<servlet-class>servlet.LoginServlet</servlet-class> 
  </servlet>
  <servlet-mapping>
  <servlet-name>a</servlet-name>
  <url-pattern>/servlet/LoginServlet</url-pattern>
  </servlet-mapping> 

猜你喜欢

转载自blog.csdn.net/qq_37139458/article/details/81148050