jsp的含有验证、md5的登录例子

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<form action="shop.do?p=login" method="post">
  用户:<input type=text name=username> <br>
  密码:<input type=password name=password> <br>
  验证码:<input type=text name=code><img id="img" src="shop.do?p=code" title="看不清换一张" οnclick="go()"> <br>
      
  <input type=submit value="登陆">
</form>


<script>
  function go()
  {

   //这里要记得加上后面的一个随机数,主要是为了防止缓存的影响
    document.getElementById('img').src='shop.do?p=code&'+Math.random();
  }
</script>

 

 

public void doCode(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
     response.setContentType("image/jpg");
     //图片缓冲区,第一个参数是要创建的图像的宽度,第二个是高度,第三个是图像的类型
     BufferedImage  bufferedImage = new BufferedImage(80,20,1);
     //得到画笔
     Graphics      g    = bufferedImage.getGraphics();
     //填充背景
     g.setColor(Color.YELLOW);
     //这是画一个矩形,第一个参数要填充矩形的 x 坐标。第二个参数y - 要填充矩形的 y 坐标。第三个参数要填充矩形的宽度。第四个参数要填充矩形的高度。
     g.fillRect(0, 0, 80, 20);
     //线
     g.setColor(Color.BLACK);
//第一个参数第一个点的x坐标,第二参数是第一点的y坐标,第三、四个参数是第二个点的x、y坐标
     g.drawLine(new Random().nextInt(80), new Random().nextInt(20), new

Random().nextInt(80), new Random().nextInt(20));
     g.drawLine(new Random().nextInt(80), new Random().nextInt(20), new

Random().nextInt(80), new Random().nextInt(20));
     //字符串生成图片
     String code = MyRandom.getRandom();
     request.getSession().setAttribute("code", code);
     g.setFont(new Font("宋体",Font.BOLD,20));
     g.setColor(Color.RED);
 //第一个参数是要写的字符,第二个参数是x坐标,第三个参数是y坐标
     g.drawString(code.substring(0, 1), 10, 12);
     g.drawString(code.substring(1, 2), 30, 16);
     g.drawString(code.substring(2, 3), 50, 13);
     g.drawString(code.substring(3, 4), 70, 17);
     //发送到客户端
     ImageIO.write(bufferedImage, "jpeg", response.getOutputStream());
 }

 

import java.util.Random;

/*
 * 产生n位随机数(字母和数字混和)
 */
public class MyRandom {
 public static void main(String[] args) {
  String code = getRandom(6);
  System.out.println(code);
 }
   public static String getRandom(int n){
    String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    Random  random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i=0;i<n;i++){
     int rr = random.nextInt(62);//产生[0-62)间随机数
     sb.append(str.charAt(rr));
    }
   
    return sb.toString();
   }
  
   public static String getRandom(){
    return getRandom(4);
   
  
   }
}

//加上下面的三句话可以避免缓存的影响

response.addHeader("pragma", "no-cache");
  response.addHeader("cache-control", "no-cache");
  response.addHeader("expires", "0");

//这是MD5加密,超简单,知识直接调用,不过要考一些MD5的文件

public boolean checkLogin(String username, String password) {
  String sql = "select * from users where username=? and password=?";
  ResultSet rs = db.getRs(sql, username, MD5.md5(password));
  try {
   if (rs.next())
    return true;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return false;
 }

//这是jsp的第八节课内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值