jsp验证码使用

jsp验证码使用

yanzhe,jsp 

 <%@ page autoFlush="false" import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%@ page import="org.apache.commons.lang.RandomStringUtils"%>
<%
RandomStringUtils rs=new RandomStringUtils();
String random=rs.randomAlphanumeric(4);
session.setAttribute("random",random);
%>
<%

out.clear();
response.setContentType("image/jpeg");
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
int width=38, height=12;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//以下填充背景颜色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//设置字体颜色
g.setColor(Color.RED);
g.drawString(random,3,10);
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
%>

使用Login.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
 <META HTTP-EQUIV="Expires" CONTENT="0">
<title>网站管理系统--宁波中小在线</title>
<style type="text/css">
<!--
body {
 background-color: #FFFFFF;
 margin-left: 0px;
 margin-top: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
}
body,td,th {
 font-size: 12px;
 color: #000000;
}
-->
</style>
<link href="img/main.css" rel="stylesheet" type="text/css">
</head>
<script language="javascript">
 function check(){
    if(myform.username.value==""){
    alert("请输入用户名!");
    return false;
 }
 if(myform.password.value==""){
   alert("请输入密码!");
   return false;
 }
 return true;
 }
</script>
<body>
<table  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="750" height="461" valign="top" background="img/admin.jpg"><table width="719" height="366" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="515" height="145">&nbsp;</td>
        <td width="158">&nbsp;</td>
        <td width="46">&nbsp;</td>
      </tr>
      <tr>
        <td height="67">&nbsp;</td>
        <td><img src="img/1.jpg" width="158" height="89"></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height="132">&nbsp;</td>
        <td>
  <form action="admin_do.jsp" method="post" name="myform">
  <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="32%" height="25">用户名:</td>
            <td width="68%"><span class="pos_service">
              <INPUT class=biaoge3 id=username  maxLength=24 name=username>
</span></td>
            </tr>
          <tr>
            <td height="25">密&nbsp;&nbsp;码:</td>
            <td><span class="pos_service">
              <INPUT class=biaoge3 id=password maxLength=24 type="password" name=password>
</span></td>
          </tr>
          <tr>
            <td height="25">验证码:</td>
            <td><span class="pos_service">
              <INPUT class=biaoge3 maxLength=24 name=rand>
</span></td>
            </tr>
          <tr>
            <td height="20">&nbsp;</td>
            <td><table width="50%"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td align="center"  class="red"><img src="yanzh.jsp"></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="30">&nbsp;</td>
            <td>
   <input type="image" src="img/main_login_go.gif" border="0" onClick="return check()">
   </td>
          </tr>
        </table>
  </form>
  </td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
验证页面admin_do.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,cn.cmpy.company.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<jsp:useBean id="user" scope="page" class="cn.cmpy.company.user"></jsp:useBean>
<jsp:useBean id="userCtl" scope="page" class="cn.cmpy.company.userCtl"></jsp:useBean>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
 <META HTTP-EQUIV="Expires" CONTENT="0">
<title>登陆验证</title>
</head>
<body>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
String rand=request.getParameter("rand");
String random=(String)session.getAttribute("random");
if(rand!=null && random!=null && !rand.equals(random)){
 out.print("<script language='javascript'>window.alert('验证码不正确,请重新输入验证码');history.go(-1);</script>");
}
else{
   int id=0;
   id=userCtl.loginUser(username,password);
  if(id!=0){
    session.setAttribute("userid",String.valueOf(id));
 response.sendRedirect("main.jsp");
  }
  else {
   out.print("<script language='javascript'>window.alert('用户名或者密码不正确,请重新登陆');history.go(-1);</script>");
  }
%>
<%}%>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值