溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

網(wǎng)頁登陸注冊(jsp實(shí)現(xiàn))驗(yàn)證碼

發(fā)布時間:2020-07-18 21:37:26 來源:網(wǎng)絡(luò) 閱讀:2181 作者:l123j 欄目:開發(fā)技術(shù)

這是一個登陸頁面,有登陸驗(yàn)證和驗(yàn)證碼的功能
(1)生成驗(yàn)證碼的servlet:
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.File;
import java.io.IOException;

public class YzmServlet extends HttpServlet {
//設(shè)置驗(yàn)證碼圖片的寬度
private static final int WIDTH = 100;
//設(shè)置驗(yàn)證碼的高度
private static final int HEIGHT = 80;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    //產(chǎn)生一張圖片
        BufferedImage image=new BufferedImage(Width,Height,BufferedImage.TYPE_INT_RGB);
    response.setHeader("Pragram","no-cache");
    response.setHeader("Cache-Control","no-catch");
    response.setDateHeader("Exprires",0);
    Graphics g=image.getGraphics();
            //設(shè)置背景顏色
    g.setColor(Color.WHITE);
            //填充圖片
    g.fillRect(0,0,Width,Height);
            //設(shè)置驗(yàn)證碼顏色
    g.setColor(Color.RED);
            //設(shè)置驗(yàn)證碼字體及大小
    Font font=new Font("微軟雅黑",Font.BOLD,20);
            //獲取驗(yàn)證碼
    String str=getRandomString(4);
            //獲取session
   HttpSession session=request.getSession();
         //給str做標(biāo)記
   session.setAttribute("str",str);
         //在圖片中畫出驗(yàn)證碼
    g.drawString(str,50,50);
            //在圖片中隨機(jī)劃線
    for (int i=0;i<15;i++){
        int x1= RandomUtils.nextInt(0,Width);
        int x2=RandomUtils.nextInt(0,Width);
        int y1=RandomUtils.nextInt(0,Height);
        int y2=RandomUtils.nextInt(0,Height);
                            Color color=new Color(RandomUtils.nextInt(0,255),RandomUtils.nextInt(0,255),RandomUtils.nextInt(0,255));
        g.setColor(color);
        g.drawLine(x1,y1,x2,y2);
    }
            //輸出圖片
    ImageIO.write(image,"jpg",response.getOutputStream());
}

}

(2)登陸驗(yàn)證的servlet:
import org.apache.commons.lang3.RandomUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

public class LoginServlet extends HttpServlet {

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

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
            //獲取輸入的驗(yàn)證碼的值
    String yzm=request.getParameter("yzm");
            //獲取輸入的用戶名
    String username=request.getParameter("username");
            //獲取輸入的密碼
    String userpassword=request.getParameter("userpassword");
            //獲取session
    HttpSession session=request.getSession();
            //獲取session標(biāo)記的值,即服務(wù)端生成的驗(yàn)證碼的值
    String yzm2=(String) session.getAttribute("str");
   //比較驗(yàn)證碼
         if (yzm.equals(yzm2)){
       if(username.equals("zhangsan")&&userpassword.equals("123456")){
           response.getWriter().print("登陸成功");
       }else{
           response.getWriter().print("用戶名或密碼錯誤");
       }

    }else{
        response.getWriter().print("驗(yàn)證碼錯誤");
    }

}

}
(3)登陸界面(比較簡陋)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
</head>
<script type="text/javascript">
function refreshImg() {
document.getElementById("y").src="./Yzm";
}

</script>

<body>
<table width="760px" height="100%" align="center" style="background-color: azure">
<tr>
<td>
<div align="center"></div>
<table width="200" height="300">
<tr>
<td>
<form action="./Login" name="form1" method="post">
用戶名:<input type="text" name="username">
密   碼:<input type="password" name="userpassword">
<a>
<img id="y" name="y" src="./Yzm" height="100" onclick="refreshImg()">
</a>
<button type="submit" value="看不清?" style="background-color: red" onclick="refreshImg()">看不清?</button><br>
驗(yàn)證碼:<input type="text" name="yzm" id="yzm" style="width: 30px" onclick="refreshImg()"><br>

                     <button type="submit" onclick="check()">提交</button>
                     <button type="reset">重置</button>
                 </form>
             </td>
         </tr>
        </table>
        </div>
    </td>
</tr>

</table>

</body>
</html>
(4)配置web.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app 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"
version="3.0"
metadata-complete="true">

<display-name>Welcome to Tomcat</display-name>
<description>
    Welcome to Tomcat
</description>
<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>YzmServlet</servlet-name>
    <servlet-class>servlet.YzmServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>YzmServlet</servlet-name>
    <url-pattern>/Yzm</url-pattern>
</servlet-mapping>

</web-app>

界面還有一些問題,不能更換驗(yàn)證碼.

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI