最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Servlet+JDBC實(shí)現(xiàn)登陸功能的小例子(帶驗(yàn)證碼)

 更新時(shí)間:2020年06月24日 09:58:48   作者:大象大象你的鼻子怎么那么長(zhǎng)  
這篇文章主要介紹了Servlet+JDBC實(shí)現(xiàn)登陸功能的小例子(帶驗(yàn)證碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

案例需求:訪問帶有驗(yàn)證碼的登錄頁(yè)面login.jsp用戶輸入用戶名,密碼以及驗(yàn)證碼。如果用戶名和密碼輸入有誤,跳轉(zhuǎn)登錄頁(yè)面,提示:用戶名或密碼錯(cuò)誤如果驗(yàn)證碼輸入有誤,跳轉(zhuǎn)登錄頁(yè)面,提示:驗(yàn)證碼錯(cuò)誤如果全部輸入正確,則跳轉(zhuǎn)到主頁(yè)success.jsp,顯示:用戶名,歡迎您分析

步驟

文件樹展示


1.配置文件和jar包在上個(gè)案例均有配置過,需要改的有:User類新增驗(yàn)證碼成員變量,數(shù)據(jù)庫(kù)增加了一個(gè)驗(yàn)證碼字段(無用,只是為了UserDao包把查找到的數(shù)據(jù)值導(dǎo)入到User類不出錯(cuò))。

2.登陸界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>login</title>
  <script>
    window.onload = function(){
      document.getElementById("img").onclick = function(){
        this.src="/CaS/checkCodepic?time="+new Date().getTime();
      }
    }
  </script>
  <style>
    div{
      color: red;
    }
  </style>
</head>
<body>

  <form action="/CaS/loginServlet" method="post">
    <table>
      <tr>
        <td>用戶名</td>
        <td><input type="text" name="username"></td>
      </tr>
      <tr>
        <td>密碼</td>
        <td><input type="password" name="password"></td>
      </tr>
      <tr>
        <td>驗(yàn)證碼</td>
        <td><input type="text" name="checkCode"></td>
      </tr>
      <tr>
        <td colspan="2"><img id="img" src="/CaS/checkCodepic"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="登錄"></td>
      </tr>
    </table>


  </form>


  <div><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error")%></div>
  <div><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></div>

</body>
</html>

3.驗(yàn)證碼,畫了個(gè)驗(yàn)證碼,每次都把隨機(jī)數(shù)加入session中以便進(jìn)行對(duì)比

package Test;

import javax.imageio.ImageIO;
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.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/checkCodepic")
public class CheckCodepic extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int width=100;
    int height=50;
    //創(chuàng)建圖片對(duì)象
    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

    //美化圖片
    //創(chuàng)建畫筆
    Graphics g = image.getGraphics();
    //畫筆顏色
    g.setColor(Color.pink);
    //畫個(gè)矩形,填充為粉紅色
    g.fillRect(0,0,width,height);
    //給矩形加邊框
    g.setColor(Color.blue);
    g.drawRect(0,0,width-1,height-1);
    //寫字母或數(shù)字
    g.setColor(Color.green);
    String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random rd=new Random();
    StringBuilder sb=new StringBuilder();
    for(int i=1;i<=4;i++){
      int index = rd.nextInt(str.length());
      char c = str.charAt(index);
      sb.append(c);
      g.drawString(c+"",width/5*i,height/2);
    }
    String checkCode_session = sb.toString();
    //將驗(yàn)證碼存入session
    req.getSession().setAttribute("checkCode_session",checkCode_session);
    //加干擾線
    g.setColor(Color.blue);
    for(int i=1;i<=10;i++){
      int x1 = rd.nextInt(width);
      int x2 = rd.nextInt(width);
      int y1 = rd.nextInt(height);
      int y2 = rd.nextInt(height);
      g.drawLine(x1,y1,x2,y2);
    }

    //輸出展示
    ImageIO.write(image,"jpg",resp.getOutputStream());
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req,resp);
  }
}

4.loginServlet類,用來判斷驗(yàn)證碼和用戶名密碼是否正確,注意先判斷驗(yàn)證碼;注意重定向和請(qǐng)求轉(zhuǎn)發(fā)的不同,還有session的應(yīng)用。

package Test;

import Test.dao.UserDao;
import Test.userclass.User;
import org.apache.commons.beanutils.BeanUtils;

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;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //設(shè)置request編碼
    request.setCharacterEncoding("utf-8");
    //獲取參數(shù)
//    String username = request.getParameter("username");
//    String password = request.getParameter("password");
//    String checkCode = request.getParameter("checkCode");
//    User user=new User();
//    user.setUsername(username);
//    user.setPassword(password);
//    user.setCheckCode(checkCode);
    Map<String, String[]> parameterMap = request.getParameterMap();
    User user=new User();
    try {
      BeanUtils.populate(user,parameterMap);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
    UserDao userDao=new UserDao();
    //先判斷驗(yàn)證碼是否正確
    String checkCode_session = (String)request.getSession().getAttribute("checkCode_session");
    request.getSession().removeAttribute("checkCode_session");
    if(checkCode_session!=null && checkCode_session.equalsIgnoreCase(user.getCheckCode())){//忽略大小寫
      //如果正確,判斷用戶名密碼是否正確
      User login = userDao.login(user);
      if(login!=null){
        //登陸成功,存儲(chǔ)用戶信息
        request.getSession().setAttribute("username",login.getUsername());
        //重定向到success.jsp
        response.sendRedirect(request.getContextPath()+"/success.jsp");

      }else{//登陸失敗,轉(zhuǎn)發(fā)到登陸界面
        request.setAttribute("login_error","用戶名或密碼不正確");
        request.getRequestDispatcher("/login.jsp").forward(request,response);

      }
    }else{ //如果不正確,轉(zhuǎn)發(fā)到登陸界面
      request.setAttribute("cc_error","驗(yàn)證碼不正確");
      request.getRequestDispatcher("/login.jsp").forward(request,response);

    }
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
  }
}

5.成功登陸界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>

  <h1><%=request.getSession().getAttribute("username")%>,歡迎您</h1>

</body>
</html>

結(jié)果

登陸界面


驗(yàn)證碼錯(cuò)誤情況


用戶名或密碼不正確情況


成功登陸

到此這篇關(guān)于Servlet+JDBC實(shí)現(xiàn)登陸功能的小例子(帶驗(yàn)證碼)的文章就介紹到這了,更多相關(guān)Servlet+JDBC 登陸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java應(yīng)用CPU使用率過高排查方式

    Java應(yīng)用CPU使用率過高排查方式

    這篇文章主要介紹了Java應(yīng)用CPU使用率過高排查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Springboot 集成 SocketIO的示例代碼

    Springboot 集成 SocketIO的示例代碼

    Socket.IO是實(shí)現(xiàn)瀏覽器與服務(wù)器之間實(shí)時(shí)、雙向和基于事件的通信的工具庫(kù),本文主要介紹了Springboot 集成 SocketIO的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • 詳解Spring Boot 部署與服務(wù)配置

    詳解Spring Boot 部署與服務(wù)配置

    本篇文章主要介紹了詳解Spring Boot 部署與服務(wù)配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • 詳解在Spring Boot中使用Mysql和JPA

    詳解在Spring Boot中使用Mysql和JPA

    本文向你展示如何在Spring Boot的Web應(yīng)用中使用Mysq數(shù)據(jù)庫(kù),也充分展示Spring Boot的優(yōu)勢(shì)
    2017-04-04
  • 詳解JAVA中的for-each循環(huán)與迭代

    詳解JAVA中的for-each循環(huán)與迭代

    本文詳解了JAVA中的for-each循環(huán)與迭代,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),也是java中的難點(diǎn)知識(shí),需要的朋友可以參考下 。
    2016-10-10
  • 如何使用lamda表達(dá)式對(duì)list進(jìn)行求和

    如何使用lamda表達(dá)式對(duì)list進(jìn)行求和

    這篇文章主要介紹了如何使用lamda表達(dá)式對(duì)list進(jìn)行求和問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 基于RecyclerChart的KLine繪制Volume實(shí)現(xiàn)詳解

    基于RecyclerChart的KLine繪制Volume實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了基于RecyclerChart的KLine繪制Volume實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • java  hibernate使用注解來定義聯(lián)合主鍵

    java hibernate使用注解來定義聯(lián)合主鍵

    這篇文章主要介紹了java hibernate使用注解來定義聯(lián)合主鍵的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Java報(bào)錯(cuò):FileNotFoundException的解決方案

    Java報(bào)錯(cuò):FileNotFoundException的解決方案

    在Java編程中,FileNotFoundException 是一種常見的受檢異常,通常發(fā)生在試圖打開一個(gè)不存在的文件或文件路徑錯(cuò)誤時(shí),本文將詳細(xì)探討FileNotFoundException的成因、解決方案以及預(yù)防措施,幫助開發(fā)者理解和避免此類問題,需要的朋友可以參考下
    2024-06-06
  • Java實(shí)現(xiàn)正則匹配 “1234567” 這個(gè)字符串出現(xiàn)四次或四次以上

    Java實(shí)現(xiàn)正則匹配 “1234567” 這個(gè)字符串出現(xiàn)四次或四次以上

    文章介紹了如何在Java中使用正則表達(dá)式匹配一個(gè)字符串四次或四次以上的出現(xiàn),首先創(chuàng)建正則表達(dá)式,然后使用Pattern和Matcher類進(jìn)行匹配和計(jì)數(shù),通過示例代碼展示了如何實(shí)現(xiàn)這一功能,并解釋了匹配的整體次數(shù)和精確出現(xiàn)次數(shù)的邏輯,感興趣的朋友一起看看吧
    2025-02-02

最新評(píng)論

徐水县| 嘉禾县| 南溪县| 江源县| 唐海县| 嘉定区| 剑河县| 中卫市| 原阳县| 天峻县| 九江市| 德阳市| 佛坪县| 贡觉县| 共和县| 黎城县| 武宁县| 偃师市| 东山县| 淄博市| 鹤山市| 黔南| 巴彦县| 铅山县| 临朐县| 临西县| 临邑县| 合水县| 武定县| 元阳县| 鸡西市| 彝良县| 阳新县| 凌海市| 如东县| 绥化市| 崇州市| 龙岩市| 安陆市| 邵东县| 万宁市|