域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要介紹了使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
我們來(lái)分一下步驟吧:
1.HTML代碼,頁(yè)面先寫出來(lái);
2.正則表達(dá)式驗(yàn)證輸入的用戶名密碼是否正確,失去焦點(diǎn)驗(yàn)證
3.Ajax異步提交
4.servlet這是后臺(tái)處理代碼獲取數(shù)據(jù)并對(duì)比響應(yīng),然后跳轉(zhuǎn)成功頁(yè)面
效果圖:
結(jié)構(gòu):
代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
servlet代碼:
package com.chaz.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;
public class AJAXServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name = "ZhangSan";
String pwd = "Zhang123456";
String ajaxName = request.getParameter("name");
String ajaxPwd = request.getParameter("pwd");
System.out.println(ajaxName+":"+ajaxPwd);
if(name.equals(ajaxName)&&pwd.equals(ajaxPwd)){
out.print("ok");
}else{
out.print("Error");
}
out.flush();
out.close();
}
}
web.xml:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
This is the description of my J2EE component
This is the display name of my J2EE component
AJAXServlet
com.chaz.servlet.AJAXServlet
AJAXServlet
/AJAXServlet
跳轉(zhuǎn)成功頁(yè)面就這個(gè):
登錄成功!
總結(jié)
以上所述是小編給大家介紹的使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
來(lái)源:腳本之家
鏈接:https://www.jb51.net/article/172816.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!