jsp第一天练习

轻鸟评职场技能 2024-04-10 01:19:23

index.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>英文、数字和中文混合的彩色验证码</title> <link rel="stylesheet" href="css/style.css"> <script language="javascript"> function checkform(myform){ for(let i=0;i<myform.length;i++){ if(myform.elements[i].value==""){ alert(myform.elements[i].title+"不能为空!"); myform.elements[i].focus(); return false; } } } function myReload(){ document.getElementById("createCheckCode").src=document.getElementById("createCheckCode").src+"?nocache="+new Date().getTime(); } </script></head><body><table width="613" height="434" border="0" align="center" cellpadding="0" cellspacing="0" background="images/loginBg.jpg"> <tr> <td width="83" height="124" valign="top"> </td> <td width="418" valign="top"> </td> <td width="112" valign="top"> </td> </tr> <form name="form1" method="post" action="check.jsp" onSubmit="return checkform(form1)"> <tr> <td height="161" valign="top"> </td> <td align="center" valign="top"> <table width="90%" height="141" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="23%" align="center">用户名:</td> <td colspan="2"> <input name="username" type="text" id="username" size="30" title="用户名"></td> </tr> <tr> <td align="center">密 码:</td> <td colspan="2"><input name="pwd" type="password" id="pwd" size="30" title="密码"></td> </tr> <tr> <td align="center">验证码:</td> <td width="18%"><input name="checkCode" type="text" id="checkCode" title="验证码区分大小写" size="8" maxlength="4"></td> <td width="59%" style="padding-top:4px"> <img src="PictureCheckCode" id="createCheckCode"><a href="#" onClick="myReload()"> 看不清?换一个</a> </td> </tr> <tr> <td> </td> <td colspan="2"> <input name="Submit" type="submit">check.jsp

<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/6/13 Time: 20:07 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><% request.setCharacterEncoding("UTF-8"); String checkCode = request.getParameter("checkCode"); if("".equals(request.getParameter("username")) || "".equals(request.getParameter("pwd"))){ out.println("<script>alert('请输入用户名或密码!');window.location.href='index.jsp';</script>"); } else{ if(!("mr".equals(request.getParameter("username")) && "mrsoft".equals(request.getParameter("pwd")))){ out.println("<script>alert('您输入的用户名或密码不正确!');window.location.href='index.jsp';</script>"); } } if ("".equals(checkCode) || checkCode == null) { out.println("<script>alert('请输入验证码!');window.location.href='index.jsp';</script>"); } else{ if (!checkCode.equals(session.getAttribute("randCheckCode"))) { out.println("<script>alert('您输入的验证码不正确!');window.location.href='index.jsp';</script>"); } }%><html><head> <title>验证</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <link rel="stylesheet" href="css/style.css"></head><body><div style="height:70px"></div><table width="799" height="479" border="0" align="center" cellpadding="0" cellspacing="0" background="images/success.gif"> <tr> <td> </td> </tr> <tr> <td align="center">欢迎访问本网站!<br> <a href="index.jsp">返回</a> </td> </tr> <tr> <td> </td> </tr></table></body></html>

style.css

body{ font-size: 9pt; margin-left: 0px; margin-top: 0px; color: #000000; scrollbar-face-color: #7BB307; scrollbar-highlight-color: #ffffff; scrollbar-shadow-color: #fcfcfc; scrollbar-3dlight-color: #ececec; scrollbar-arrow-color: #ffffff; scrollbar-track-color: #ececec; scrollbar-darkshadow-color: #999966; background-color: #7BB307;}td { font-size: 9pt; line-height:150%;}a:hover { font-size: 9pt; color: #FF0000;}a { font-size: 9pt; text-decoration: none; color: #3C404D;}.btn_bg { background-image:url(../images/btn_bg.jpg); border:#436E9B solid 1px; width:64px; height:23px; padding:4px; color:#FFFFFF;}input { font-family: "宋体"; font-size: 12px; color: #333333; border: 1px solid #999999; height:20px;}

PictureCheckCode.java

package com.demo;import javax.imageio.ImageIO;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.awt.*;import java.awt.geom.AffineTransform;import java.awt.geom.Line2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;public PictureCheckCode extends HttpServlet { public PictureCheckCode() { super(); } public void destroy() { super.destroy(); } public void init() throws ServletException { super.init(); } // 获取随机颜色 public Color getRandColor(int s, int e) { Random random = new Random(); if (s > 255) s = 255; if (e > 255) e = 255; //随机生成RGB颜色中的r值 int r = s + random.nextInt(e - s); //随机生成RGB颜色中的g值 int g = s + random.nextInt(e - s); //随机生成RGB颜色中的b值 int b = s + random.nextInt(e - s); return new Color(r, g, b); } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0); // 指定生成的响应是图片 response.setContentType("image/jpeg"); int width = 86; int height = 22; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Graphics2D g2d = (Graphics2D) g; Random random = new Random(); Font mFont = new Font("黑体", Font.BOLD, 17); g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); g.setFont(mFont); g.setColor(getRandColor(180, 200)); // 画随机的线条 for (int i = 0; i < 100; i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int x1 = random.nextInt(6) + 1; int y1 = random.nextInt(12) + 1; BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); Line2D line = new Line2D.Double(x, y, x + x1, y + y1); g2d.setStroke(bs); g2d.draw(line); } String sRand = ""; // 输出随机的验证文字 String ctmp = ""; int itmp = 0; for (int i = 0; i < 4; i++) { //random = new Random(new java.util.Date().getTime() + i); switch (random.nextInt(4)) { case 1: itmp = random.nextInt(26) + 65; // 生成A~Z的字母 ctmp = String.valueOf((char) itmp); break; case 2: // 生成汉字 String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; // 生成第1位的区码 int r1 = random.nextInt(3) + 11; //生成11到14之间的随机数 String str_r1 = rBase[r1]; // 生成第2位的区码 int r2; if (r1 == 13) { r2 = random.nextInt(7); //生成0到7之间的随机数 } else { r2 = random.nextInt(16); //生成0到16之间的随机数 } String str_r2 = rBase[r2]; // 生成第1位的位码 int r3 = random.nextInt(6) + 10; //生成10到16之间的随机数 String str_r3 = rBase[r3]; // 生成第2位的位码 int r4; if (r3 == 10) { r4 = random.nextInt(15) + 1; //生成1到16之间的随机数 } else if (r3 == 15) { r4 = random.nextInt(15); //生成0到15之间的随机数 } else { r4 = random.nextInt(16); //生成0到16之间的随机数 } String str_r4 = rBase[r4]; System.out.println(str_r1 + str_r2 + str_r3 + str_r4); // 将生成机内码转换为汉字 byte[] bytes = new byte[2]; //将生成的区码保存到字节数组的第1个元素中 String str_r12 = str_r1 + str_r2; int tempLow = Integer.parseInt(str_r12, 16); bytes[0] = (byte) tempLow; //将生成的位码保存到字节数组的第2个元素中 String str_r34 = str_r3 + str_r4; int tempHigh = Integer.parseInt(str_r34, 16); bytes[1] = (byte) tempHigh; //根据字节数组生成汉字 ctmp = new String(bytes); //System.out.println("生成汉字:" + ctmp); break; default: // 生成0~9的数字 itmp = random.nextInt(10) + 48; ctmp = String.valueOf((char) itmp); break; } sRand += ctmp; Color color = new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110)); g.setColor(color); /** **随机缩放文字并将文字旋转指定角度* */ // 将文字旋转指定角度 Graphics2D g2d_word = (Graphics2D) g; AffineTransform trans = new AffineTransform(); trans.rotate(random.nextInt(45) * 3.14 / 180, 15 * i + 8, 7); // 缩放文字 float scaleSize = random.nextFloat() +0.8f; if (scaleSize > 1f) scaleSize = 1f; trans.scale(scaleSize, scaleSize); g2d_word.setTransform(trans); g.drawString(ctmp, 15 * i + 18, 14); } // 将生成的验证码保存到Session中 HttpSession session = request.getSession(true); ((HttpSession) session).setAttribute("randCheckCode", sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); }}

配置web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>PictureCheckCode</servlet-name> <servlet-class>com.demo.PictureCheckCode</servlet-class> </servlet> <servlet-mapping> <servlet-name>PictureCheckCode</servlet-name> <url-pattern>/PictureCheckCode</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

需要servlet和jsp的jar包

工程目录:

0 阅读:0

轻鸟评职场技能

简介:感谢大家的关注