<!-- 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 ServletException, IOException {
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两次触发