JS综合使用:点击图片切换一张

JS使用功能:

用法1:

<a href="javascript:checkAnother();">
<img src = "${pageContext.request.contextPath}/pictureServlet" title = "另一张" id = "anotherPict">
</a>

<script>
	function checkAnother(){
		var anotherPict = document.getElementById("anotherPict");
		anotherPict.src = "${pageContext.request.contextPath}/pictureServlet";
}
</script>

用法2:

<img src = "${pageContext.request.contextPath}/pictureServlet" title = "另一张" id = "anotherPict">

<script>
	window.onload(){
	var anotherPict = document.getElementById("anotherPict");
	anotherPict.onClick = function(){
		anotherPict.src = "${pageContext.request.contextPath}/pictureServlet";
	} 
}
</script>

画出图画的servlet:

@WebServlet("/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		int width = 120;
		int height = 100;
		int x = 0;
		int y = 0;
		BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = im.getGraphics();
		graphics.setColor(Color.pink);
		graphics.fillRect(x, y, width, height);
		graphics.setColor(Color.green);
		graphics.drawRect(x, y, width-1,height-1);
		String string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		graphics.setColor(Color.BLACK);
		int w_padding = width/6;
		int h_padding = height/2;
		for (int i = 1; i < 6; i++) {
			Random random = new Random();
			int index = random.nextInt(string.length());
			String str = string.charAt(index)+"";
			graphics.drawString(str,w_padding*i,h_padding);
		}
		ServletOutputStream outputStream = response.getOutputStream();
		ImageIO.write(im, "jpg", outputStream);
	}
	
	

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值