jsp验证码使用_JSP教程
推荐: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"> </td>
<td width="158"> </td>
<td width="46"> </td>
</tr>
<tr>
<td height="67"> </td>
<td><img src="img/1.jpg" width="158" height="89"></td>
<td> </td>
</tr>
<tr>
<td height="132"> </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">密 码:</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"> </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"> </td>
<td>
<input type="image" src="img/main_login_go.gif" border="0" onClick="return check()">
</td>
</tr>
</table>
</form>
</td>
<td> </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 response.sendRedirect不跳转的原因分析及解决
- JSP指令元素(page指令/include指令/taglib指令)复习整理
- JSP脚本元素和注释复习总结示例
- JSP FusionCharts Free显示图表 具体实现
- 网页模板:关于jsp页面使用jstl的异常分析
- JSP页面中文传递参数使用escape编码
- 基于jsp:included的使用与jsp:param乱码的解决方法
- Java Web项目中连接Access数据库的配置方法
- JDBC连接Access数据库的几种方式介绍
- 网站图片路径的问题:绝对路径/虚拟路径
- (jsp/html)网页上嵌入播放器(常用播放器代码整理)
- jsp下显示中文文件名及绝对路径下的图片解决方法
- 相关链接:
- 教程说明:
JSP教程-jsp验证码使用。