jsp驗證碼使用_JSP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:jsp自動生成靜態(tài)文件(html)
      jsp自動生成靜態(tài)文件(html): html.jsp %@ page contentType=text/html;charset=utf-8% %@page import=cn.nbaia.newPage.newsPage.*,cn.nbaia.newPage.newsImg.*,java.util.*,cn.nbaia.newPage.columnPage.*,cn.nbaia.company.*,cn.nbaia.link.* % %@page

      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);
      //設(shè)置字體顏色
      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>網(wǎng)站管理系統(tǒng)--寧波中小在線</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>
       

       

      分享:用JSP來發(fā)送郵件
      MESSAGE.JSP TABLE width=555 border=0 align=center cellPadding=0 cellSpacing=0 FORM action=message_do.jsp method=post TBODY TR TD width=80 height=30信件標(biāo)題:FONT color=#ff0000*/FONT/TD TDINPUT class=input id=topic name=topic /TD /TR TR T

      來源:模板無憂//所屬分類:JSP教程/更新時間:2010-04-18
      相關(guān)JSP教程