如何在註冊時使用驗證碼註冊(servlet原始碼)
【1】書寫CheckCodeServlet類
package com.hyxy.servlet.session;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
public class CheckCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //建立圖片物件 BufferedImage image = new BufferedImage(80, 30,BufferedImage.TYPE_INT_RGB); //獲取畫筆 Graphics g = image.getGraphics(); Random r = new Random(); //給畫筆上色 g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); //繪製方形 g.fillRect(0, 0, 80, 30); g.setColor(new Color(0,0,0)); g.setFont(new Font(null,Font.BOLD,30));//設定字型大小 //真實的驗證碼 String number = getNumber(4); System.out.println("checkcode:"+number); //獲取session物件 HttpSession session = request.getSession(); //儲存驗證碼 session.setAttribute("number", number); //將驗證碼繪製到圖片上 g.drawString(number, 1, 24); //繪製5條幹擾線 for(int i=0;i<5;i++){ g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.drawLine(r.nextInt(80),r.nextInt(30),r.nextInt(80),r.nextInt(30)); } //設定響應頭 response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); javax.imageio.ImageIO.write(image, "jpeg", os); } public static String getNumber(int n){ String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String number =""; Random ran = new Random(); for(int i = 0 ;i<n;i++){ number+=str.charAt(ran.nextInt(str.length())); } return number; }
}
記得配置web.xml檔案哦
我們直接在瀏覽器直接訪問CheckCodeServlet
我們可以看到結果:
【2】書寫regist.jsp頁面
<%@ 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 'regist.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="css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> 註冊 </h1> <form action="RegisterServlet" method="post"> <table cellpadding="0" cellspacing="0" border="0" class="form_table"> <tr> <td valign="middle" align="right"> 使用者名稱: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="username" /> </td> </tr> <tr> <td valign="middle" align="right"> 真實姓名: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="realName" /> </td> </tr> <tr> <td valign="middle" align="right"> 密碼: </td> <td valign="middle" align="left"> <input type="password" class="inputgri" name="password" /> </td> </tr> <tr> <td valign="middle" align="right"> 性別: </td> <td valign="middle" align="left"> 男 <input type="radio" class="inputgri" name="sex" value="m" checked="checked"/> 女 <input type="radio" class="inputgri" name="sex" value="f"/> </td> </tr> <tr> <td valign="middle" align="right"> 驗證碼: <img id="num" src="CheckCodeServlet" /> <a href="javascript:;" onclick="document.getElementById('num').src = 'image?'+(new Date()).getTime()">換一張</a> </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="number" /> </td> </tr> </table> <p> <input type="submit" class="button" value="Submit »" /> </p> </form> </div> </div> <div id="footer"> <div id="footer_bg"> [email protected] </div> </div> </div> </body> </html>
顯示結果:
【3】從後臺中獲取登入資料(書寫RegisterServlet類)
package com.hyxy.servlet.test;
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; import javax.servlet.http.HttpSession;
import com.hyxy.servlet.bean.User; import com.hyxy.servlet.interfaces.impl.UserDaoImpl;
public class RegisterServlet extends HttpServlet {
/** * */ private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String realName = request.getParameter("realName"); String sex = request.getParameter("sex"); //獲取瀏覽器端的驗證碼 String number = request.getParameter("number"); //獲取session物件 HttpSession session = request.getSession(); String checkCode = (String)session.getAttribute("number"); //System.out.println("校驗ma:"+checkCode); if(checkCode.equalsIgnoreCase(number)) { User user = new User( username, password, realName, sex); UserDaoImpl dao = new UserDaoImpl(); dao.addUser(user); request.getRequestDispatcher("login.jsp").forward(request, response); }else{ request.getRequestDispatcher("fail.jsp").forward(request, response); } }
}