加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

当鼠标经过显示图片信息

发布时间:2020-12-16 03:25:40 所属栏目:百科 来源:网络整理
导读:使用Ajax写当鼠标经过显示图片信息 ImageBean package domain;public class Image {private int id;private String name;private String auther;public Image(int id,String name,String auther) {super();this.id = id;this.name = name;this.auther = auth

使用Ajax写当鼠标经过显示图片信息

ImageBean

package domain;

public class Image {
	private int id;
	private String name;
	private String auther;
	public Image(int id,String name,String auther) {
		super();
		this.id = id;
		this.name = name;
		this.auther = auther;
	}
	public Image() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuther() {
		return auther;
	}
	public void setAuther(String auther) {
		this.auther = auther;
	}
	

}

ImageDao.java

package dao;

import domain.Image;

public class ImageDao {
	// 根据编号查询图片
	public Image findImageById(int id) {
		Image image = new Image();
		image.setId(id);
		if (id == 1) {
			image.setAuther("hang");
			image.setName("fen");

		} else if (id == 2) {
			image.setAuther("zhentou");
			image.setName("lv");

		}
		return image;

	}

}

ImageServlet.java

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import dao.ImageDao;
import domain.Image;

public class ImageServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException {
		System.out.println("imageservlet.doget");
		String id = request.getParameter("id");
		ImageDao imageDao = new ImageDao();
		Image image = imageDao.findImageById(Integer.parseInt(id));
		JSONArray jsonArray = JSONArray.fromObject(image);
		String jsonString = jsonArray.toString();
		System.out.println("jsonString=" + jsonString);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(jsonString);

	}

}

Image.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>image.jsp</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>
		This is my JSP page. <br>
		<img height="250px" src="images/_DSC2140.jpg" value="1" />
		<img height="250px" src="images/IMG_8339.jpg" value="2" />
		<script type="text/javascript">
			var imgElementArray = document.getElementsByTagName("img");
			var size = imgElementArray.length;
			for(var i = 0; i < size; i++) {
				imgElementArray[i].onmouSEOver = function() {
					//alert(this.value);
					var id = this.value;
					var xhr = createXHR()
					//alert(xhr!=null);
					xhr.onreadystatechange = function() {
						if(xhr.readyState == 4) {
							if(xhr.status == 200) {
								//alert("a");
								var jsonString = xhr.responseText;
								var jsonObject = eval("(" + jsonString + ")");
								alert(jsonObject[0].id + " : " + jsonObject[0].name + " : " + jsonObject[0].auther);
							}
						}
					}
					xhr.open("GET","servlet/ImageServlet?time=" + new Date().getTime() + "&id=" + 1);
					xhr.send(null);
				}
			}

			function createXHR() {
				var xhr = null;
				try {
					xhr = new ActiveXObject("microsoft.xmlhttp");

				} catch(e) {
					xhr = new XMLHttpRequest();
				}
				return xhr;
			}
		</script>
	</body>

</html>

注意:

对应一致


运行:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读