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
@H_301_31@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>
注意:
对应一致