大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Servlet驗證碼的代碼示例

Servlet驗證碼的代碼示例

更新時間:2022-01-04 10:39:03 來源:動力節(jié)點 瀏覽1061次

//login.jsp<br><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> 
${msg }
<form action="/day11/login" method="post">
        <table border="1" width="70%">
            <tr>
                <td>Enter name</td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
            <tr>
                 <td>Enter password</td>
                <td>
                    <input type="password" name="password" />
                </td>
            </tr>
            <tr>
                 <td>Verification code</td>
                <td>
                    <input type="text" name="code" />
                    <img id="imgId" src="/day11/checkcode">
                     <a href="#" onclick="run()">Can’t see clearly, change one</a>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     <input type="submit" value="log in"/>
                </td>
            </tr>
        </table>
    </form>
</body>
    <script type="text/javascript">
         // Can't see clearly, change one, timestamp
        function run(){
             // Get pictures
            var image = document.getElementById("imgId");
            image.src = "/day11/checkcode?"+new Date().getTime();
        }     
    </script>
</html>
package CheckCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; 
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
public class check extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        final int width=140;
        final int height=30;
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        Graphics2D g=(Graphics2D)image.getGraphics();         
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width-1, height-1);         
        String words="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        Random random=new Random(); 
        g.setColor(Color.WHITE);
         g.setFont(new Font("kaishu",Font.PLAIN,18));         
        int x=20;
        int y=20;
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<5;i++){
            int jiaodu=random.nextInt(60)-30;
            double hudu=jiaodu*Math.PI/180;
            g.rotate(hudu,x,y);
            int index=random.nextInt(words.length());
            char ch=words.charAt(index);
             
            sb.append(ch);
            g.drawString(" "+ch, x, y);
            g.rotate(-hudu,x,y);
            x+=18;
        }
        request.getSession().setAttribute("code", sb.toString());
        g.setColor(Color.BLUE);
        int x1;
        int x2;
        int y1;
        int y2;
        for(int i=0;i<4;i++){
            x1=random.nextInt(width);
            y1=random.nextInt(height);
            x2=random.nextInt(width);
            y2=random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }         
        ImageIO.write(image, "jpg", response.getOutputStream());
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    } 
}
package CheckCode; 
import java.io.IOException; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
public class loginServlet extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String code1=(String)request.getSession().getAttribute("code");
        System.out.println(code1);
        String code2=request.getParameter("code");
        if(code2!=null&&code1.equalsIgnoreCase(code2)){
            response.setContentType("text/html;charset=UTF-8");
             response.getWriter().write("Success");
        }else{
             request.setAttribute("msg","Verification code error");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
        doGet(request, response); 
    } 
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日韩一区二区不卡 | 成人国内精品久久久久影 | 国内夫妇精品对白在线播放 | 日本a级精品一区二区三区 日本a毛片在线播放 | 精品国产一区二区二三区在线观看 | 亚洲精品天堂一区二区三区 | 二级毛片免费观看全程 | 香蕉爱爱视频 | 国产一级特黄aaaa大片野外 | 国产在线一区二区三区欧美 | 牛牛影视ac精品视频 | 天天看天天射 | 国产成人免费视频精品一区二区 | 久久久亚洲欧洲日产国码二区 | 国产偷国产偷在线高清 | 中文字幕97 | 国内精品手机在线观看视频 | 久久精品爱 | 九九精品激情在线视频 | 国产色视频一区二区三区 | sss欧美华人整片在线观看 | 第一区免费在线观看 | 日韩欧美视频在线 | 午夜免费体验区 | 亚洲在线精品视频 | 久久综合久久综合久久综合 | 国产精品2020观看久久 | 国产欧美日韩中文久久 | 亚洲人和日本人jzz护士 | 人人做人人性 | 亚洲香蕉久久一区二区 | 日本久久中文字幕 | 欧美性xxx久久 | 国产网红精品 | 色综合婷婷 | 日日日夜夜操 | 国产香蕉一区二区精品视频 | 中文字幕在线精品视频万部 | 久久久中文字幕 | 香港a毛片免费全部播放 | 国产二区在线播放 |