简单的AJAX应用

it2022-05-05  140

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script>

function showHint(str){

//if (str.length==0) // { // document.getElementById("txtHint").innerHTML=""; // return; // }

xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("您的浏览器不支持AJAX!"); return; }

var url="../servlet/Ajax?method=checkusername";url=url+"&str="+str;//url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.send(null);}

function showHint2(str2){

xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("您的浏览器不支持AJAX!"); return; }

var url="../servlet/Ajax?method=checkpassword";var str1=document.getElementById("psw1").value;url=url+"&str2="+str2+"&str1="+str1;xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.send(null);}

function GetXmlHttpObject(){ var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp;}

function stateChanged() { if (xmlHttp.readyState==4) { if(xmlHttp.responseText=="密码不相同"||xmlHttp.responseText==""){ document.getElementById("txtHint2").innerHTML=xmlHttp.responseText; }else{ document.getElementById("txtHint").innerHTML=xmlHttp.responseText; } }}

function add(){document.forms[0].action="../servlet/Ajax?method=add";}</script></head><body><div><p>注册信息</p><form action="" οnsubmit=add() method="post" name=form1><table><tr><td>用 户 名:</td><td><input type="text" name="text" id="tex1" onBlur="showHint(this.value)"></td><td><span id="txtHint"></span></td></tr><tr><td>密 码:</td><td><input type="password" name="psww" id="psw1" ></td><td></td></tr><tr><td>重输密码:</td><td><input type="password" name="psww2" id="psw2" onBlur="showHint2(this.value)"></td><td><span id="txtHint2"></span></td></tr><tr><td>姓名:</td><td><input type="text" name="nam" id="mail"></td><td></td></tr><tr><td>身份证号:</td><td><input type="text" name="idcard" id="mail"></td><td></td></tr><tr><td><input type="reset" name="res" value="重置" /><input type="submit" name="sub" value="提交" /></td></tr></table></form></div></body></html>

 

sevlet:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String method=request.getParameter("method"); if(method.equals("checkusername")) { checkUsername(request,response); } if(method.equals("add")) { add(request,response); } if(method.equals("checkpassword")) { checkPassword(request,response); }

} public void checkPassword(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String ps1 = request.getParameter("str1"); String ps2=request.getParameter("str2"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); if (ps1.equals(ps2)) { out.print(""); }else{ out.print("密码不相同");} } public void add(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String username=request.getParameter("text"); String password=request.getParameter("psww"); String name=request.getParameter("nam"); String idcard=request.getParameter("idcard"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); LoginerManage lm=new LoginerManage(); try { lm.add(username, password, name, idcard); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } out.print("注册成功!!"); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.sendRedirect("../WEB-AJAX/ajax.jsp"); } public void checkUsername(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String str = request.getParameter("str"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); LoginerManage lm=new LoginerManage(); if (str == null || str.equals("")) { out.print("请输入用户名");

} else { try { if (lm.query(str).size()==0) {

out.print("可以注册"); } else { out.print("账号存在,请重输"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

转载于:https://www.cnblogs.com/yaer/p/5282534.html


最新回复(0)