主頁 > 知識庫 > jsp網(wǎng)頁登陸驗證

jsp網(wǎng)頁登陸驗證

熱門標(biāo)簽:電子圍欄 銀行業(yè)務(wù) 科大訊飛語音識別系統(tǒng) Mysql連接數(shù)設(shè)置 團(tuán)購網(wǎng)站 阿里云 服務(wù)器配置 Linux服務(wù)器

jsp登陸驗證,網(wǎng)頁登陸驗證帶驗證碼校驗,登錄功能之添加驗證碼

part_1:專門用于生成一個驗證碼圖片的類:VerificationCode.java

package cn.mike.javase.test; 
 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Random; 
 
import javax.imageio.ImageIO; 
 
import org.junit.Test; 
 
/** 
 * @author : Administrator 
 * @function : 這是用來測試隨機(jī)生成驗證碼圖片的類; 
 */ 
public class VerificationCode { 
 
  /** 
   * 單元測試,試一下能不能自動生成驗證碼圖片 
   */ 
  // 這個函數(shù)是單元測試時使用的,這里private一下外面就調(diào)用不到了; 
  /* @Test */ 
  /* public */private void test_fun() { 
    VerificationCode vc = new VerificationCode(); 
    BufferedImage image = vc.getImage(); 
    try { 
      // 生成驗證碼圖片,并保存到指定的路徑 
      VerificationCode.output(image, new FileOutputStream(new File( 
          ".\\image\\vcode_2.jpg"))); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } 
 
    // 將隨機(jī)生成的文本內(nèi)容輸出到控制臺,用于校驗 
    System.out.println(vc.getText()); 
  } 
 
  private int w = 70;// 寬 
  private int h = 35;// 高 
  private String text;// 文本內(nèi)容(驗證碼字符串) 
  private Random r = new Random(); 
  private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" }; 
  // 隨機(jī)字符集合中不包括0和o,O,1和l,因為這些不易區(qū)分 
  private String codes = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYXZ"; 
  // 驗證碼圖片的背景色:白色 
  private Color bgColor = new Color(255, 255, 255); 
 
  /** 
   * 返回一個驗證碼圖片buffer對象:BufferedImage 
   */ 
  public BufferedImage getImage() { 
    BufferedImage image = createImage(); 
    // 獲取繪圖環(huán)境(畫筆工具) 
    Graphics2D g2 = (Graphics2D) image.getGraphics(); 
    // sb : 用來保存驗證碼字符串文本內(nèi)容 
    StringBuilder sb = new StringBuilder(); 
 
    for (int i = 0; i  4; ++i) {// 隨機(jī)生成4個字符 
      String s = randomChar() + ""; 
      sb.append(s); 
      float x = i * 1.0F * w / 4; 
      g2.setFont(randomFont()); 
      g2.setColor(randomColor()); 
      g2.drawString(s, x, h - 5); 
    } 
 
    this.text = sb.toString();// 記錄驗證碼文本內(nèi)容 
    drawLine(image);// 畫干擾線 
    return image; 
 
  } 
 
  /** 
   * @return 獲取驗證碼文本內(nèi)容 
   */ 
  public String getText() { 
    return text; 
  } 
 
  /** 
   * @param image 
   * @param out 
   *      將文本寫到指定的輸出流。比如本測試中FileOutputStream指定的保存路徑 
   */ 
  public static void output(BufferedImage image, OutputStream out) { 
    try { 
      ImageIO.write(image, "jpeg", out); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
 
  private void drawLine(BufferedImage image) { 
    Graphics2D g2 = (Graphics2D) image.getGraphics(); 
    for (int i = 0; i  3; ++i) {// 畫3條干擾線 
      int x1 = r.nextInt(w); 
      int y1 = r.nextInt(h); 
      int x2 = r.nextInt(w); 
      int y2 = r.nextInt(h); 
      g2.setColor(Color.BLUE); 
      g2.drawLine(x1, y1, x2, y2); 
    } 
  } 
 
  private Color randomColor() { 
    int red = r.nextInt(150); 
    int green = r.nextInt(150); 
    int blue = r.nextInt(150); 
    return new Color(red, green, blue); 
  } 
 
  private Font randomFont() { 
    int index = r.nextInt(fontNames.length); 
    String fontName = fontNames[index]; 
    int style = r.nextInt(4); 
    int size = r.nextInt(5) + 24; 
    return new Font(fontName, style, size); 
  } 
 
  private char randomChar() { 
    int index = r.nextInt(codes.length()); 
    return codes.charAt(index); 
  } 
 
  private BufferedImage createImage() { 
    BufferedImage image = new BufferedImage(w, h, 
        BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2 = (Graphics2D) image.getGraphics(); 
    g2.setColor(this.bgColor); 
    g2.fillRect(0, 0, w, h); 
 
    return image; 
  } 
 
} 

part_2:登錄界面:Login.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 'Login.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 _change_verity_code() { 
    var imgElem = document.getElementById("img_src"); 
 
    //添加一個請求參數(shù)a是因為,通常瀏覽器都有緩存,點擊換一張的時候沒反應(yīng),所以加一個請求參數(shù),獲取當(dāng)前請求時間,可以精確到毫秒,所以每次請求的參數(shù)都不同,所以瀏覽器有緩存也不妨礙; 
    imgElem.src = "/ServletDemoProject/servlet/GetVerificationCodeServlet?a=" 
        + new Date().getTime(); 
  } 
/script> 
 
  /head> 
 
  % 
    String fdbkMsg = (String) request.getAttribute("fdbkMsg"); 
    if (null == fdbkMsg) { 
      fdbkMsg = ""; 
    } 
  %> 
 
  % 
    Boolean logedIn = (Boolean) session.getAttribute("logedIn"); 
    if (null == logedIn) { 
      logedIn = false; 
    } else if (logedIn) { 
      //如果在本次會話已經(jīng)登陸,直接重定向到success-page-1 
      response 
          .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); 
    } 
  %> 
 
  % 
    String username = ""; 
    Cookie[] cookies = request.getCookies(); 
    if ((null != cookies)  (cookies.length > 0)) { 
      for (Cookie c : cookies) { 
        if ("admin".equals(c.getValue())) { 
          username = "admin"; 
          break; 
        } 
      } 
    }//end if-condition 
  %> 
 
  body> 
    br> 
    div align="center"> 
      請登錄: 
      br> 
      form action="/ServletDemoProject/servlet/LoginVerificationServlet" 
        method="post"> 
        div> 
          用戶名: 
          input type="text" name="username" value="%=username%>" /> 
          br> 
        /div> 
 
        div> 
          密  碼: 
          input type="password" name="password" /> 
          br> 
        /div> 
        div> 
          驗證碼: 
          input type="text" name="code_text" size="3" /> 
          img src="/ServletDemoProject/servlet/GetVerificationCodeServlet" 
            id="img_src" /> 
          a href="javascript:_change_verity_code()">換一張/a> 
          br> 
        /div> 
 
        div> 
          font color='red'>%=fdbkMsg%>/font> 
          br> 
        /div> 
 
        div> 
          input type="submit" value="提交" /> 
          br> 
        /div> 
      /form> 
    /div> 
  /body> 
/html> 

part_3:處理登錄校驗的servlet :LoginVerificationServlet.java

package cn.mike.servlet.test_1212; 
 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import cn.mike.javase.test.VerificationCode; 
 
public class GetVerificationCodeServlet extends HttpServlet { 
 
  private static final long serialVersionUID = -3520994675366100452L; 
 
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
 
    // 1.新建一個VerificationCode類; 
    VerificationCode vc = new VerificationCode(); 
 
    // 2.從VerificationCode類中獲取BufferedImage對象; 
    BufferedImage bufImage = vc.getImage(); 
 
    // 3.同時獲取驗證碼中的文本內(nèi)容,并放到session域中, 用于校驗; 
    String code_text = vc.getText(); 
    request.getSession().setAttribute("code_text", code_text); 
 
    // 4.將生成的圖片輸出到客戶端瀏覽器 
    VerificationCode.output(bufImage, response.getOutputStream()); 
 
  }// end method-doGet 
 
  public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
 
    // do same as GET-method : 
    doGet(request, response); 
 
  }// end method-doPost 
 
} 

part_4:成功登陸后的提示界面1:success-page-1.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 'success-page-1.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"> 
  --> 
 
  /head> 
 
  % 
    String username = (String) session.getAttribute("username"); 
    if (null == username) { 
      //如果username為空值,說明不是通過正常渠道來的,轉(zhuǎn)發(fā)到Login頁面; 
      request.setAttribute("fdbkMsg", "別想走后門進(jìn)來,趕緊登錄!"); 
      request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( 
          request, response); 
    } 
  %> 
 
  body> 
    br> 
    %=username%>已經(jīng)成功登陸。 
    br> 
    font>您可以選擇瀏覽:/font> 
    br> 
    a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒有精彩./a> 
    br> 
    a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒更精彩./a> 
    br /> 
    a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">你敢點這兒嗎./a> 
    br /> 
  /body> 
/html> 

part_5:成功登陸后的提示界面1:success-page-2.jsp

%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%> 
%@ page language="java" import="java.text.SimpleDateFormat"%> 
% 
  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 'success-page-2.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"> 
  --> 
 
  /head> 
 
  % 
    String username = (String) session.getAttribute("username"); 
    if (null == username) { 
      request.setAttribute("fdbkMsg", "呵呵嗒,這里是你來的地方嗎?快登陸!"); 
      //轉(zhuǎn)發(fā)到登錄界面: 
      request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( 
          request, response); 
    } 
 
    SimpleDateFormat sDateFormat = new SimpleDateFormat("a"); 
    Date today = new Date(); 
    String am_pm_str = sDateFormat.format(today); 
    String am_pm_str_in_chinese = ""; 
    if ("am".equalsIgnoreCase(am_pm_str)) { 
      am_pm_str_in_chinese = "上午"; 
    } else 
      am_pm_str_in_chinese = "下午"; 
 
    // set null; 
    sDateFormat = null; 
    today = null; 
    am_pm_str = null; 
  %> 
 
  body> 
    br /> 
    font>b>%=username%>%=am_pm_str_in_chinese%>好,能來到頁面2真不簡單./b> 
    /font> 
  /body> 
/html> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • asp+Ajax簡單客戶登陸驗證
  • JS簡單實現(xiàn)登陸驗證附效果圖
  • 詳解Angular開發(fā)中的登陸與身份驗證
  • python實現(xiàn)帶驗證碼網(wǎng)站的自動登陸實現(xiàn)代碼
  • 用PHP實現(xiàn)登陸驗證碼(類似條行碼狀)
  • App登陸java后臺處理和用戶權(quán)限驗證
  • PHP 驗證登陸類分享
  • java客戶端登陸服務(wù)器用戶名驗證
  • Java Web開發(fā)過程中登陸模塊的驗證碼的實現(xiàn)方式總結(jié)
  • php通過smtp郵件驗證登陸的方法

標(biāo)簽:大理 衡水 棗莊 衢州 蚌埠 廣元 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp網(wǎng)頁登陸驗證》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266