• jsp4


    <%@ 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>
            <style type="text/css">
                body{
                    background-color: azure;
                }
                form{
                    position: fixed;
                    top: 250px;
                    left: 650px;
                    background-color: #ADD8E6;
                    border-radius: 5px;
                }
                div{
                    font-size: 22px;
                    margin-top: 20px;
                    padding-left: 40px;
                }
                input{
                     200px;
                    height: 20px;
                    border-radius: 5px;
                }
                #tj{
                     80px;
                    height: 30px;
            
                    margin-left: 140px;
                        border-radius: 5px;
                        font-size: 16px;
                        margin-bottom: 20px;
                }
            </style>
            
    </head>
    <body>
        <form action="/school/panduanServlet" method="post">
                    <div>账 号:    <input type="text" name="username"></div>
                    <div>密 码:    <input type="text" name="password"></div>
                    <div>验 证 码:<input type="text" name = "yanzhengma"> <img   src="getcode" alt="看不清,换一张" onclick="change(this)"/></div>
                    <div><input type="submit" id="tj" value="登录" ></div>
                </form>
    
    </body>
    <script type="text/javascript">
        function  change(img) {
            img.src = "getcode?"+new Date().getTime();
            
        }
    </script>
    </html>
    复制代码
    复制代码
    <%@ 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>
    <%
    String username = (String)request.getAttribute("username");
    %>
    <%=username %>欢迎您
    </body>
    </ht
    复制代码
    复制代码
    package servlet;
    
    import java.io.IOException;
    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 javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class pandunServlet
     */
    @WebServlet("/panduanServlet")
    public class panduanServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public panduanServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
             HttpSession session = request.getSession();
             String yanzhengma = session.getAttribute("code").toString();
             System.out.println(yanzhengma);
             String username = request.getParameter("username");
             String password = request.getParameter("password");
             String yanzhengma1 = request.getParameter("yanzhengma");
             if(yanzhengma.equals(yanzhengma1)) {
                 if("zs".equals(username) && "123".equals(password)) {
                     request.setAttribute("username", username);
                     request.getRequestDispatcher("/success.jsp").forward(request, response);
                 }else {
                     request.getRequestDispatcher("/loginyanzheng.jsp").forward(request, response);
                 }
             }else {
                 request.getRequestDispatcher("/loginyanzheng.jsp").forward(request, response);
             }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    复制代码
    复制代码
    package servlet;
    
    import java.awt.image.RenderedImage;
    import java.io.IOException;
    import java.util.Map;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import utils.CodeUtil;
    
    /**
     * Servlet implementation class CodeServlet
     */
    @WebServlet("/getcode")
    public class CodeServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public CodeServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 调用工具类生成的验证码和验证码图片
            Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();
    
            // 将四位数字的验证码保存到Session中。
            HttpSession session = request.getSession();
            session.setAttribute("code", codeMap.get("code").toString());
            System.out.println( codeMap.get("code").toString());
            // 禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", -1);
    
            response.setContentType("image/jpeg");
    
            // 将图像输出到Servlet输出流中。
            ServletOutputStream sos;
            try {
                sos = response.getOutputStream();
                ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
                sos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    复制代码
    复制代码
    package utils;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    
    public class CodeUtil {
        private static int width = 90;// 定义图片的width
        private static int height = 20;// 定义图片的height
        private static int codeCount = 4;// 定义图片上显示验证码的个数
        private static int xx = 15;
        private static int fontHeight = 18;
        private static  int codeY = 16;
        private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
        /**
         * 生成一个map集合
         * code为生成的验证码
         * codePic为生成的验证码BufferedImage对象
         * @return
         */
        public static Map<String,Object> generateCodeAndPic() {
            // 定义图像buffer
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // Graphics2D gd = buffImg.createGraphics();
            // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
            Graphics gd = buffImg.getGraphics();
            // 创建一个随机数生成器类
            Random random = new Random();
            // 将图像填充为白色
            gd.setColor(Color.WHITE);
            gd.fillRect(0, 0, width, height);
    
            // 创建字体,字体的大小应该根据图片的高度来定。
            Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
            // 设置字体。
            gd.setFont(font);
    
            // 画边框。
            gd.setColor(Color.BLACK);
            gd.drawRect(0, 0, width - 1, height - 1);
    
            // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
            gd.setColor(Color.BLACK);
            for (int i = 0; i < 30; i++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                gd.drawLine(x, y, x + xl, y + yl);
            }
    
            // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
            StringBuffer randomCode = new StringBuffer();
            int red = 0, green = 0, blue = 0;
    
            // 随机产生codeCount数字的验证码。
            for (int i = 0; i < codeCount; i++) {
                // 得到随机产生的验证码数字。
                String code = String.valueOf(codeSequence[random.nextInt(36)]);
                // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
                red = random.nextInt(255);
                green = random.nextInt(255);
                blue = random.nextInt(255);
    
                // 用随机产生的颜色将验证码绘制到图像中。
                gd.setColor(new Color(red, green, blue));
                gd.drawString(code, (i + 1) * xx, codeY);
    
                // 将产生的四个随机数组合在一起。
                randomCode.append(code);
            }
            Map<String,Object> map  =new HashMap<String,Object>();
            //存放验证码
            map.put("code", randomCode);
            //存放生成的验证码BufferedImage对象
            map.put("codePic", buffImg);
            return map;
        }
    }
  • 相关阅读:
    dom4j解析xml字符串实例
    使用Dom4j生成xml文件(utf-8编码)
    viewer.js插件简单使用说明
    html标签title属性效果优化
    XML文件读写编码不是UTF-8的问题
    webservice wsdl文件标签讲解
    定销房概念扫盲
    Sql Server系列:视图
    Centos7安装mysql5.6
    java实现链式队列
  • 原文地址:https://www.cnblogs.com/wishings/p/14590148.html
Copyright © 2020-2023  润新知