Servlet 登录时数据校验
生活随笔
收集整理的這篇文章主要介紹了
Servlet 登录时数据校验
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
包含JavaScript登陸時(shí)校驗(yàn)和服務(wù)器端的校驗(yàn)
JSP校驗(yàn)意義
客戶端驗(yàn)證:使用JavaScript驗(yàn)證
好處:降低服務(wù)器端負(fù)擔(dān) 不足: 不安全,可以跳過只能進(jìn)行格式驗(yàn)證,無法進(jìn)行業(yè)務(wù)驗(yàn)證(用戶名是否存在)
服務(wù)器端驗(yàn)證:使用JSP驗(yàn)證
好處: 安全 可以進(jìn)行格式驗(yàn)證和業(yè)務(wù)驗(yàn)證
不足:增加了服務(wù)器端的負(fù)擔(dān) 建議:同時(shí)進(jìn)行服務(wù)器端和客戶端驗(yàn)證
導(dǎo)包(Jquery包)
Login.jsp(登錄界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>登錄界面</title><script type="text/javascript" src="js/jquery-1.12.3.min.js"></script><script type="text/javascript">$(function () {$("#uname").blur(function () {//進(jìn)行用戶名的非空校驗(yàn)var val =$("#uname").val();if(val==null||val==""){$("#uname_span").text("用戶名不能為空").css("color","red");}else {$("#uname_span").text("用戶名合法").css("color","green");}})$("#pwd").blur(function () {//進(jìn)行密碼的非空校驗(yàn)var val =$("#pwd").val();if(val==null||val==""){$("#pwd_span").text("密碼不能為空").css("color","red");}else {$("#pwd_span").text("密碼合法").css("color","green");}})})</script></head> <body><h3>用戶登錄</h3><form action="DoLoginServlet" method="post"><p>賬號(hào):<input type="text" name="uname" id="uname"/><span id="uname_span"><%Object msg= request.getAttribute("msg");if(msg!=null){out.print(msg);}%></span></p><p>密碼:<input type="password" name="pwd" id="pwd"><span id="pwd_span"></span></p><p><input type="submit" value="提交"/></p></form></body> </html>success.jsp(登錄成功界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>成功登錄</title> </head> <body><h1>成功登錄</h1></body> </html>fa.jsp(非法登錄攔截)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>警告</title> </head> <body><h1>滾</h1><h4>好好登┗|`O′|┛ 嗷~~</h4><h4>想啥呢</h4><h4>滾去正常登陸去</h4> </body> </html>DoLoginServlet.java(servlet流程控制)
package com.java.servlet; import com.sun.net.httpserver.HttpServer; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(urlPatterns = "/DoLoginServlet") public class DoLoginServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//接收數(shù)據(jù)String uname = req.getParameter("uname");String pwd = req.getParameter("pwd");//服務(wù)端校驗(yàn)if(uname==null||"".equals(uname)){req.getRequestDispatcher("fa.jsp").forward(req,resp);return;}//處理數(shù)據(jù)boolean flage= false;if ("sxt".equals(uname)&&"123".equals(pwd)){flage= true;}//做出響應(yīng)if (flage){req.getRequestDispatcher("success.jsp").forward(req,resp);}else{req.setAttribute("msg","用戶名和密碼不匹配");req.getRequestDispatcher("Login.jsp").forward(req,resp);}} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Servlet 登录时数据校验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servelt 中文乱码
- 下一篇: 路径使用场景