jsp验证码使用_JSP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:jsp自动生成静态文件(html)
jsp自动生成静态文件(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);
//设置字体颜色
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>
 

 

分享:用JSP来发送邮件
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信件标题:FONT color=#ff0000*/FONT/TD TDINPUT class=input id=topic name=topic /TD /TR TR T

来源:模板无忧//所属分类:JSP教程/更新时间:2010-04-18
相关JSP教程