JSP+Servlet+Ajax验证用户

来源:互联网 发布:mysql实用教程 pdf 编辑:程序博客网 时间:2024/06/11 05:11

            废话不多说,贴代码,一般看到我这个帖子的人,说明至少他能理解下面的代码,是在不行的,给我留言。

                            JSP+JS代码:

            

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE html><html lang="en-us"><head><base href="<%=basePath%>"><title>YiGou OnlineShopping Register</title><!-- Bootstrap --><link rel="stylesheet" href="Style/bootstrap.css"><link rel="stylesheet" href="Style/bootstrap-theme.css"><!-- jQuery (necessary for Bootstrap's JavaScript plugins) --><script src="Style/jquery-2.0.2.js"></script><!-- Include all compiled plugins (below), or include individual files as needed --><script src="Style/bootstrap.js"></script><style type="text/css">.myfont {font-family: cursive; /*fantasy,Myriad Pro; */font-size: 100px;color: #0055ff;}</style><script type="text/javascript">function check() {var v_password = document.getElementById("inputPassword3");var v_confirmPassword = document.getElementById("inputPassword4");var password = v_password.value;var confirmPassword = v_confirmPassword.value;if (password == "") {    var show="<font color='red'>Password can't be empty!</font>";        document.getElementById("password").innerHTML=show;v_password.focus();return false;} else if (password != confirmPassword) {    var show="<font color='red'>The password is not consistent!</font>";        document.getElementById("password").innerHTML=show;v_password.focus();return false;}return true;}var req;function validate(){    var userEmail=document.getElementById("inputEmail3");    var url="ajaxRegisterServlet?id="+escape(userEmail.value);        if(window.XMLHttpRequest)    {        req=new XMLHttpRequest();    }else if(window.ActiveXObject)    {         req=new ActiveXObject("Microsoft.XMLHttp");    }        req.open("GET",url,true);    req.onreadystatechange=callback;    req.send(null);}function callback(){    if(req.readyState==4&&req.status==200){        var check=req.responseText;        show(check);    }}function show(str){    if(str=="OK"){        var show="<font color='green'>The email is valid!</font>";        document.getElementById("info").innerHTML=show;    }    else if(str=="NO")    {        var show="<font color='red'>The email has been registered!</font>";        document.getElementById("info").innerHTML=show;    }}</script></head><body style="background:url(Images/Website/img11.jpg) repeat left top"><div class="myfont" style="margin-top:60px;margin-left:355px;"><span>YiGou Register</span></div><div style="margin-top:30px;margin-left:470px;width:650px"><form class="form-horizontal" name="form" action="userServlet?method=userRegister" method="post" onsubmit="return check()"><div class="form-group"><label for="inputEmail3" class="col-sm-2 control-label">Email</label><div class="col-sm-10"><input type="email" class="form-control" id="inputEmail3" name="email"style="width:300px;float:left" placeholder="Email" onblur="validate()"><span id="info"></span></div></div></form>${Msg}</div><div style="margin-top:35px;margin-left:470px"><span style="font-size:20px;font-family:Georgia;color:black">©Copyright 2014 By QinJiangbo  All rights reserved </span></div></body></html>

      Servlet代码:

      

package com.yigou.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.yigou.service.IUserService;import com.yigou.service.IUserServiceImpl;public class AjaxRegisterServlet extends HttpServlet {/** *  */private static final long serialVersionUID = 1L;private IUserService service=new IUserServiceImpl();/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try{response.setContentType("text/html");response.setHeader("Cache-Control", "no-store");response.setHeader("Pragma", "no-store");response.setDateHeader("Expires", 0);String userEmail=request.getParameter("id");if(service.checkUser(userEmail)){out.write("OK");}else{out.write("NO");}}finally{out.close();}}}

0 0