在Servlet只能显示一个图片,并且用图层定位时写代码很是麻烦,并且如果是从流中读数据,就只能显示一个图片,同时,还不能解决图片和文字信息的同时输出和位置叠加 在JSP中,我们可以用以下办法来解决 首先,写一个Servlet负责从数据文件或数据库等数据来源中得到数据流并输出 然后在JSP文件中多次调用此Servlet来请求并取的图片数据流并显示 我的Servlet代码入下 InitializeMap.java /* *创建日期2005-5-30 * *TODO要更改此生成的文件的模板,请转至 *窗口-首选项-Java-代码样式-代码模板 */ packagezy.ser; importjava.io.File; importjava.io.FileInputStream; importjava.io.OutputStream; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /** * *TODO要更改此生成的类型注释的模板,请转至窗口-首选项-Java-代码样式-代码模板 */ publicclassInitializeMapextendsHttpServlet{ StringimgName; publicInitializeMap(){ super(); } /** *Destructionoftheservlet. */ publicvoiddestroy(){ super.destroy();//Justputs"destroy"stringinlog //Putyourcodehere } /** *ThedoDeletemethodoftheservlet. * *ThismethodiscalledwhenaHTTPdeleterequestisreceived. * *therequestsendbytheclienttotheserver *theresponsesendbytheservertotheclient *ifanerroroccurred *ifanerroroccurred */ publicvoiddoDelete(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException,IOException{ //Putyourcodehere } /** *ThedoGetmethodoftheservlet. * *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget. * *therequestsendbytheclienttotheserver *theresponsesendbytheservertotheclient *ifanerroroccurred *ifanerroroccurred */ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ this.initURI(request); Filefile=newFile(this.getServletContext().getRealPath("img\\map1\\"+this.imgName+".jpg")); System.out.println(file); FileInputStreamfis=newFileInputStream(file); System.out.println(fis); intlength=fis.available(); System.out.println(length); byte[]data=newbyte[length]; //readthestreamdataintothestream fis.read(data); fis.close(); OutputStreamsos=response.getOutputStream(); //readthestreamdataintotheoutputstream response.setContentType("image/*"); sos.write(data); sos.flush(); sos.close(); } /** *ThedoPostmethodoftheservlet. * *Thismethodiscalledwhenaformhasitstagvaluemethodequalsto *post. * *therequestsendbytheclienttotheserver *theresponsesendbytheservertotheclient *ifanerroroccurred *ifanerroroccurred */ publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ response.setContentType("text/html"); PrintWriterout=response.getWriter(); out.println(""); out.println(""); out.println(""); out.println(""); out.print("Thisis"); out.print(this.getClass()); out.println(",usingthePOSTmethod"); out.println(""); out.println(""); out.flush(); out.close(); } /** *ThedoPutmethodoftheservlet. * *ThismethodiscalledwhenaHTTPputrequestisreceived. * *therequestsendbytheclienttotheserver *theresponsesendbytheservertotheclient *ifanerroroccurred *ifanerroroccurred */ publicvoiddoPut(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //Putyourcodehere } /** *Returnsinformationabouttheservlet,suchasauthor,version,and *copyright. * */ publicStringgetServletInfo(){ return"ThisismydefaultservletcreatedbyEclipse"; } /** *Initializationoftheservlet. * *ifanerroroccure */ publicvoidinit()throwsServletException{ //Putyourcodehere } publicvoidinitURI(HttpServletRequestrequest){ this.imgName=request.getParameter("imgName"); } } |