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

加载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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageHealth extends HttpServlet {

public final int WIDTH = 100;
public final int HEIGHT = 20;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletExceptionIOException {
BufferedImage img = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();

setSize(g);
setWidth(g);
drawRectangle(g);

response.setDateHeader("expires", -1);
response.setHeader("cache-control", "no-cache");
response.setContentType("image/jpeg");
ImageIO.write(img, "jpg", response.getOutputStream());

}

// 设置边框样式
private void setSize(Graphics g) {

g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);

}

// 设置边框线大小
private void setWidth(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(2, 2, WIDTH - 4, HEIGHT - 4);
}

// 画出方块标示健康值
private void drawRectangle(Graphics g) {
g.setColor(Color.RED);
int health = Common.dog.getHealth() / 10;
int xP = 0;
for (int i = 0; i < health; i++) {
g.fillRect(xP, 20, WIDTH, HEIGHT);
xP += 10;
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}



jsp img servlet两次触发

文章评论

加载index.jsp出现img标签两次请求servlet图片解决思路
如何区分一个程序员是“老手“还是“新手“?
加载index.jsp出现img标签两次请求servlet图片解决思路
中美印日四国程序员比较
加载index.jsp出现img标签两次请求servlet图片解决思路
代码女神横空出世
加载index.jsp出现img标签两次请求servlet图片解决思路
10个帮程序员减压放松的网站
加载index.jsp出现img标签两次请求servlet图片解决思路
为什么程序员都是夜猫子
加载index.jsp出现img标签两次请求servlet图片解决思路
10个调试和排错的小建议
加载index.jsp出现img标签两次请求servlet图片解决思路
如何成为一名黑客
加载index.jsp出现img标签两次请求servlet图片解决思路
60个开发者不容错过的免费资源库
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员的一天:一寸光阴一寸金
加载index.jsp出现img标签两次请求servlet图片解决思路
我是如何打败拖延症的
加载index.jsp出现img标签两次请求servlet图片解决思路
初级 vs 高级开发者 哪个性价比更高?
加载index.jsp出现img标签两次请求servlet图片解决思路
不懂技术不要对懂技术的人说这很容易实现
加载index.jsp出现img标签两次请求servlet图片解决思路
5款最佳正则表达式编辑调试器
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员都该阅读的书
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员必看的十大电影
加载index.jsp出现img标签两次请求servlet图片解决思路
Web开发人员为什么越来越懒了?
加载index.jsp出现img标签两次请求servlet图片解决思路
什么才是优秀的用户界面设计
加载index.jsp出现img标签两次请求servlet图片解决思路
每天工作4小时的程序员
加载index.jsp出现img标签两次请求servlet图片解决思路
“肮脏的”IT工作排行榜
加载index.jsp出现img标签两次请求servlet图片解决思路
聊聊HTTPS和SSL/TLS协议
加载index.jsp出现img标签两次请求servlet图片解决思路
编程语言是女人
加载index.jsp出现img标签两次请求servlet图片解决思路
程序猿的崛起——Growth Hacker
加载index.jsp出现img标签两次请求servlet图片解决思路
我的丈夫是个程序员
加载index.jsp出现img标签两次请求servlet图片解决思路
写给自己也写给你 自己到底该何去何从
加载index.jsp出现img标签两次请求servlet图片解决思路
十大编程算法助程序员走上高手之路
加载index.jsp出现img标签两次请求servlet图片解决思路
Web开发者需具备的8个好习惯
加载index.jsp出现img标签两次请求servlet图片解决思路
鲜为人知的编程真相
加载index.jsp出现img标签两次请求servlet图片解决思路
漫画:程序员的工作
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员应该关注的一些事儿
加载index.jsp出现img标签两次请求servlet图片解决思路
旅行,写作,编程
加载index.jsp出现img标签两次请求servlet图片解决思路
老程序员的下场
加载index.jsp出现img标签两次请求servlet图片解决思路
总结2014中国互联网十大段子
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员的样子
加载index.jsp出现img标签两次请求servlet图片解决思路
团队中“技术大拿”并非越多越好
加载index.jsp出现img标签两次请求servlet图片解决思路
那些争议最大的编程观点
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员眼里IE浏览器是什么样的
加载index.jsp出现img标签两次请求servlet图片解决思路
亲爱的项目经理,我恨你
加载index.jsp出现img标签两次请求servlet图片解决思路
程序员的鄙视链
加载index.jsp出现img标签两次请求servlet图片解决思路
要嫁就嫁程序猿—钱多话少死的早
加载index.jsp出现img标签两次请求servlet图片解决思路
当下全球最炙手可热的八位少年创业者
加载index.jsp出现img标签两次请求servlet图片解决思路
“懒”出效率是程序员的美德
加载index.jsp出现img标签两次请求servlet图片解决思路
看13位CEO、创始人和高管如何提高工作效率
加载index.jsp出现img标签两次请求servlet图片解决思路
老美怎么看待阿里赴美上市
加载index.jsp出现img标签两次请求servlet图片解决思路
科技史上最臭名昭著的13大罪犯
加载index.jsp出现img标签两次请求servlet图片解决思路
Java程序员必看电影