91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java web如何實現一個驗證碼權限登錄功能

發布時間:2020-11-12 16:26:43 來源:億速云 閱讀:158 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關Java web如何實現一個驗證碼權限登錄功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

所用到的開發工具為myeclipse10,MySQL數據庫。

首先,在myeclipse中新建一個Java web項目。

項目的結構:

Java web如何實現一個驗證碼權限登錄功能 

數據庫結構:

Java web如何實現一個驗證碼權限登錄功能Java web如何實現一個驗證碼權限登錄功能 

下面將各個包中的代碼粘貼出來。

com.ningmeng.dao包

package com.ningmeng.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.ningmeng.model.User;
public class UserDao {
 public User login(Connection con,User user) throws SQLException{
  User resultUser=null;
  String sql="select * from user where username=? and password=?";
  PreparedStatement ps=con.prepareStatement(sql);//
  ps.setString(1, user.getUsername());
  ps.setString(2, user.getPassword());
  ResultSet rs=ps.executeQuery();
  if(rs.next()){
   resultUser=new User();
   resultUser.setUsername(rs.getString("username"));
   resultUser.setPassword(rs.getString("password"));
  }
  return resultUser;
 }
}

com.ningmeng.model包

package com.ningmeng.model;
public class User {
 private int id;
 private String username;
 private String password;
 public User() {
  super();
 }
 public User(String username, String password) {
  super();
  this.username = username;
  this.password = password;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
}

com.ningmeng.util包

package com.ningmeng.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 public class DbUtil {
 private String url="jdbc:mysql://localhost:3306/db-jsp";
 private String user="root";
 private String password="123";
 private String driver="com.mysql.jdbc.Driver";
 public Connection getCon() throws Exception{
     Class.forName(driver);
    Connection con=DriverManager.getConnection(url, user, password);
    return con;
 }
 public static void getClose(Connection con) throws SQLException{
  if(con!=null){
   con.close();
  }
 }
 /*public static void main(String[] args) {
  DbUtil db=new DbUtil();
  try {
   db.getCon();
   System.out.println("測試連接數據庫,連接成功");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("測試連接數據庫,連接失敗");
  }
 }*/
}

com.ningmeng.web包

package com.ningmeng.web;
import java.io.IOException;
import java.sql.Connection;
import java.io.*;
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 javax.servlet.http.*;
import com.ningmeng.dao.UserDao;
import com.ningmeng.model.User;
import com.ningmeng.util.DbUtil;
public class LoginServlet extends HttpServlet{
 DbUtil db=new DbUtil();
 UserDao userDao=new UserDao();
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request, response);
 }
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  String checkcode=request.getParameter("checkCode");
  HttpSession session=request.getSession();
  //session.getAttribute("randCheckCode");
  String s = (String)session.getAttribute("randCheckCode");
  if(!checkcode.equals(s))
  {
   response.sendRedirect("linmao.jsp");
  }else{
   if(request.getParameter("ra").equals("l")){
  Connection con=null;
  try {
   User user=new User(username,password);
   con=db.getCon();
   User currentUser=userDao.login(con, user);
   if(currentUser==null){
    //System.out.println("no");
    request.setAttribute("error", "用戶名或者密碼錯誤");
    request.setAttribute("username", username);
    request.setAttribute("password", password);
    request.getRequestDispatcher("login.jsp").forward(request, response);
   }else{
    //System.out.println("yes");
    HttpSession session1=request.getSession();
    session1.setAttribute("currentUser",currentUser);
    response.sendRedirect("main.jsp");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   }
  }
   else{
    response.sendRedirect("linmao.jsp");
   }
  }
 }
 }

com.servlet包

package com.servlet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
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 javax.imageio.ImageIO;
public class PictureCheckCode extends HttpServlet {
 private static final long serialVersionUID = 1L;
 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;
  int r,g,b;
  r=s+random.nextInt(e-s); //隨機生成RGB顏色中的r值
  g=s+random.nextInt(e-s); //隨機生成RGB顏色中的g值
  b=s+random.nextInt(e-s); //隨機生成RGB顏色中的b值
  return new Color(r,g,b);
 }
 @Override
 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,height=33;  //指定生成驗證碼的寬度和高度
  BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //創建BufferedImage對象,其作用相當于一圖片
  Graphics g=image.getGraphics();  //創建Graphics對象,其作用相當于畫筆
  Graphics2D g2d=(Graphics2D)g;  //創建Grapchics2D對象
  Random random=new Random();
  Font mfont=new Font("楷體",Font.BOLD,20); //定義字體樣式
  g.setColor(getRandColor(200,250));
  g.fillRect(0, 0, width, height); //繪制背景
  g.setFont(mfont);     //設置字體
  g.setColor(getRandColor(180,200));
  //繪制100條顏色和位置全部為隨機產生的線條,該線條為2f
  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++){
   switch(random.nextInt(3)){
    case 1:  //生成A-Z的字母
      itmp=random.nextInt(26)+65;
      ctmp=String.valueOf((char)itmp);
      break;
    default:
      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),random.nextInt(110));
   g.setColor(color);
   //將生成的隨機數進行隨機縮放并旋轉制定角度 PS.建議不要對文字進行縮放與旋轉,因為這樣圖片可能不正常顯示
   /*將文字旋轉制定角度*/
   Graphics2D g2d_word=(Graphics2D)g;
   AffineTransform trans=new AffineTransform();
   trans.rotate((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);
  }
  HttpSession session=request.getSession(true);
  session.setAttribute("randCheckCode", sRand);
  g.dispose(); //釋放g所占用的系統資源
  ImageIO.write(image,"JPEG",response.getOutputStream()); //輸出圖片
 }
}

配置web.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>web04</display-name>
 <welcome-file-list>
 <welcome-file>login.jsp</welcome-file>
  <welcome-file>main.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>com.ningmeng.web.LoginServlet</servlet-class>
 </servlet>
 <servlet>
 <description>輸出驗證碼</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>PictureCheckCode</servlet-name>
 <servlet-class>com.servlet.PictureCheckCode</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/login</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>PictureCheckCode</servlet-name>
 <url-pattern>/PictureCheckCode</url-pattern>
 </servlet-mapping>
</web-app>

login.jsp:

<%@ 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>
<script language="javascript">
function myReload() {
 document.getElementById("CreateCheckCode").src = document
   .getElementById("CreateCheckCode").src
   + "&#63;nocache=" + new Date().getTime();
}
</script>
</head>
<body>
<form action="login" method="post">
 <table>
  <tr>
   <th colspan="2">登錄界面</th>
  </tr>
  <tr>
   <td>賬號:</td>
   <td><input type="text" id="username" name="username" value="${username}"></td>
  </tr>
  <tr>
   <td>密碼:</td>
   <td><input type="password" id="password" name="password" value="${password}"></td>
  </tr>
  <tr>
   <td>驗證碼:</td>
   <td><input name="checkCode" type="text" id="checkCode" title="驗證碼區分大小寫"
    size="8" ,maxlength="4" />
   <img src="PictureCheckCode" id="CreateCheckCode" align="middle">
   <a href="" onclick=" rel="external nofollow" myReload()"> 看不清,換一個</a>
  </tr>
  <tr>
   <td>類型:</td>
   <td>
   <input type="radio" name="ra" value="l" checked="checked"/>一般研究人員
   <input type="radio" name="ra" value="m" />管理員研究人員
   </td>
  </tr>
  <tr>
   <td>
   <input type="submit" value="提交">
   <font color="red">${error}</font>
   </td>
  </tr>
 </table>
</form>
</body>
</html>

main.jsp:

<%@ 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>
<p>登錄成功</p>
當前用戶:${currentUser.username}<br/>
當前密碼:${currentUser.password}<br/>
</body>
</html>

linmao.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>" rel="external nofollow" >
 <title>My JSP 'linmao.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" rel="external nofollow" >
 -->
 </head>
 <body>
 This is my JSP page. <br>
 </body>
</html>

項目現在做到驗證碼和權限這一塊,找回密碼功能還沒有做完,結構中Check.jsp還用不到。還有連接數據庫的操作,就不詳細敘述了。

Java web如何實現一個驗證碼權限登錄功能 

看完上述內容,你們對Java web如何實現一個驗證碼權限登錄功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

榆林市| 高淳县| 北安市| 资阳市| 陇西县| 四川省| 孟州市| 泸定县| 汝城县| 七台河市| 上杭县| 巴彦淖尔市| 昌乐县| 耒阳市| 盈江县| 朝阳市| 密云县| 清涧县| 张北县| 蒲江县| 江油市| 浮梁县| 和龙市| 儋州市| 永年县| 来凤县| 南通市| 塘沽区| 且末县| 合川市| 玛曲县| 武义县| 齐齐哈尔市| 永嘉县| 渝中区| 镇坪县| 汨罗市| 嘉义县| 乌恰县| 安图县| 桃园市|