日期:2014-05-17  浏览次数:20733 次

加载index.jsp出现img标签两次请求servlet图片

<!-- index.jsp -->
<%@ page language="java" import="java.util.*" 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 'index.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">
-->
<link rel="stylesheet" type="text/css" href="common.css">

<script type="text/javascript">
function refresh(img) {
//alert("aaaaaaa");
img.src = img.src + "?" + new Date().getTime();
}
</script>

</head>

<body class="center">
<h3>欢迎来到宠物小屋</h3>

<form id="form1" name="form" action="/javaWork/servlet/Pet">
<table class="left">
<tr>
<td><b>宠物种类:</b></td>
<td><select name="type" onchange="typeChanged()">
<option value="dog">狗狗</option>
<option value="penguin">企鹅</option>
</select>
</td>
</tr>
<tr>
<td><b>宠物昵称:</b></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><b>健康值:</b></td>
<td><input type="text" name="health"><span class="red">(0~100)</span>
</td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="checkCode" /><img
src="/javaWork/servlet/CheckImage?i=1" title="点击刷新"
onclick="refresh(this)" style="cursor:pointer" />
</td>
</tr>
<tr>
<td class="center"><input type="submit" value="提交" /></td>
<td class="center"><input type="reset" value="清空" /></td>
</tr>
</table>


</form>
</body>
</html>



//Servlet: CheckImage.java

package cn.bdqb.t36;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;