package?com.ljdy;import?java.io.IOException;import?java.io.PrintWriter;import?javax.servlet.ServletException;import?javax.servlet.http.HttpServlet;import?javax.servlet.http.HttpServletRequest;import?javax.servlet.http.HttpServletResponse;public?class?LoginServlet?extends?HttpServlet{?@Override?protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)???throws?ServletException,?IOException?{??response.setContentType("text/html;charset=utf8");??//request.getSession().setAttribute("name",?str);??//對name對象賦值??//request.getSession().getAttribute("name")??//取得name對象的值??String?piccode=(String)request.getSession().getAttribute("piccode");??//獲取用戶輸入的驗證碼??String?checkcode=request.getParameter("checkcode");??//不區(qū)分大小寫??checkcode=checkcode.toUpperCase();??//創(chuàng)建一個寫入對象??PrintWriter?out?=?response.getWriter();??if(checkcode.equals(piccode)){???out.println("驗證碼輸入正確");??}else{???out.println("驗證碼輸入有誤,請重新輸出");??}??out.flush();??out.close();?}??????}
<%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>
<%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"><html>??<head>????<base?href="<%=basePath%>">????????<title>My?JSP?'index.jsp'?starting?page</title>?<meta?http-equiv="pragma"?content="no-cache">?<meta?http-equiv="cache-control"?content="no-cache">?<meta?http-equiv="expires"?content="0">?????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">?<meta?http-equiv="description"?content="This?is?my?page">?<!--?<link?rel="stylesheet"?type="text/css"?href="styles.css">?-->??<script?type="text/javascript">????<%--??function?reloadCode(){??????document.getElementById("imagecode").src='<%=request.getContextPath()%>/servlet/ImageServlet?ts='+new?Date().getTime();?????}?--%>???????????function?reloadCode(){???????var?time=new?Date().getTime();?????//?ts是請求過去的參數(shù)?隨便定義??????document.getElementById("imagecode").src="<%=request.getContextPath()%>/servlet/ImageServlet?d="+time;?????};??</script>??</head>????<body>??<div?style="text-align:?center;">????<form?action="<%=request.getContextPath()?%>/servlet/LoginServlet"??method="get">???????驗證碼:<input?type="text"?name="checkcode"/>??????<img?alt="驗證碼"?id="imagecode"?src="<%=request.getContextPath()?%>/servlet/ImageServlet"/>??????<a?href="javascript:?reloadCode();">看不清楚</a><br>??????<input?type="submit"?value="提交">??????</form></div>??</body></html>
package?com.ljdy;import?java.awt.Color;import?java.awt.Graphics;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?ImageServlet?extends?HttpServlet{?@Override?protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)???throws?ServletException,?IOException?{?????//創(chuàng)建圖片映像?白紙????BufferedImage?bi=new?BufferedImage(68,?22,?BufferedImage.TYPE_INT_RGB);?????//獲得畫筆?????Graphics?g?=?bi.getGraphics();//????Color?color?=?new?Color(200,151,255);//顏色生成//?????g.setColor(color);?????g.setColor(Color.gray);?????//設(shè)驗證碼背景?????g.fillRect(0,?0,?68,?22);?????//toCharArray將字符串轉(zhuǎn)換為字符數(shù)組,獲得一個含有字母和數(shù)字的數(shù)組,后續(xù)隨機從中獲取字符?????char[]?ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray();?????Random?r=new?Random();????????int?len=ch.length,index;???//?????int?len?=ch.length;//?????int?index;?????StringBuffer?s=new?StringBuffer();??????for(int?i=0;i<4;i++){??????index=r.nextInt(len);?//?將產(chǎn)生的隨機字母和數(shù)字放入index中??????g.setColor(new?Color(r.nextInt(88),r.nextInt(120),r.nextInt(90)));??//給每個驗證碼創(chuàng)建隨機顏色??????g.drawString(ch[index]+"",?(i)*15+3,?18);????//將獲得的字符寫在圖片并設(shè)定位置??????s.append(ch[index]);???//將隨機獲取的字符放入緩沖區(qū)中?保存圖片?????}?????request.getSession().setAttribute("newcode",s.toString());??//存至session中,以便后續(xù)驗證?????ImageIO.write(bi,?"JPG",?response.getOutputStream());??????//通過Image輸出圖片,并傳入前臺?}?@Override?protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)???throws?ServletException,?IOException?{???doGet(request,?response);?}????}
2019-04-11
報的錯誤是啥?