日期:2014-05-19  浏览次数:20795 次

关于jsp的一个问题 求大家帮忙~~
这是一个关于验证码的小例子
下面是zhuce.jsp

<%@ page language="java" import="java.util.*,javax.servlet.http.*;" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'zhuce.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body style="backgrond:1.jpg no-repeat" >

 <form method="get" action="zhuceServlet">
 
用户名:<input type="text" name="username"/><br/>
 密码:<input type="password" name="password"/> <br/>
 
 你喜欢什么休闲方式:<br/>
 
 <input type="checkbox" name="aihao" value="读书"/> 读书
 &nbsp;<input type="checkbox" name="aihao" value="旅游">旅游<br/>
  <input type="checkbox" name="aihao" value="足球 "/>足球 
  <input type="checkbox" name="aihao" value="打游戏"/>打游戏<br/>
  自我介绍:<br/>
  <textarea rows="5" cols="20" name="jianjie"></textarea>
  <br>

  <img src="image.jsp" width="60" height="20"/><br/>
  请输入上面图片
  <%
  String str=(String)session.getAttribute("rgb");
  if(str.equals("0")){
  out.print("红");
  }
  if(str.equals("1")){
  out.print("绿");
  }
  if(str.equals("2")){
  out.print("蓝");
  }
  %>
  色的字:
  
  <input type="text" name="rand"/>
  <p>
  <input type="submit" value="提交">
  <input type="reset" value="重置">
  </form>
  </body>
</html>

下面是image.jsp


<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="utf-8"%>
<%@ page import="java.io.OutputStream" %>

// 获取随机的rgb
<%!Color getRandColor(int y,int h){
Random random=new Random();
if(h>255)h=255;
if(y>255)y=255;
int r=y+random.nextInt(h-y);
int g=y+random.nextInt(h-y);
int b=y+random.nextInt(h-y);
return new Color(r,g,b);
}
 %>
<%
response.setHeader("pragma","No-cache");
response.setHeader("cache","no-cache");
response.