繁体   English   中英

通过jsp上的servlet分配图像

[英]Dispaly image through servlet on jsp

从Servlet中,我生成JPEG图像并写入该Servlet的输出流中。通过jsp,我调用此Servlet URL并将图像显示为类似于带有照片的用户个人资料。 这里的问题是,第一次登录时,它将动态生成图像并显示,但是下次,如果我先关闭浏览器登录,它将显示privies图片,然后将显示当前图片。

JSP:

     <div class="sortable">
<div class="box span5" style="margin-left: 50px;">
 <div class="box-header well">
 <h2><i class="icon-th"></i>Employee Attendance</h2>
<div class="box-icon">
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
</div>
</div>
<div class="box-content"  style="height:230px;" > 
<img border="0" src="admissionenquirylist.do?method=image" alt="Pulpit rock" width="370" height="240"/>
</div>
</div>
</div>

Servlet:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.treamis.admission.process;

import com.google.gson.Gson;
import com.treamis.entity.Academicyearmaster;
import com.treamis.entity.AdmissionenquiryStudentdetails;
import com.treamis.entity.EmployeeEntity;
import com.treamis.hr.employee.PaginationClass;
import com.treamis.hr.employee.SetPaginationRecords;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.ServletOutputStream;

/**
 *
 * @author ranjeeth.g
 */
public class AdmissionEnquiry extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * Provides the mapping from resource key to method name.
     *
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.admissionEnqiryList", "admissionEnqiryList");
        map.put("button.image", "image");
        map.put("button.delete", "delete");
        return map;
    }

    /**
     * Action called on Add button click
     */
    public void admissionEnqiryList(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        // TODO: implement add method
        try {
            String stdutendserch = request.getParameter("admissionenquirysearch");
            System.out.println("stdutendserch = " + stdutendserch);

            Admissionservices as = new Admissionservices();
            List<Enquirylistbean> stdserc = as.getStudentEnquirySerch(stdutendserch);
            if (stdserc != null) {
                response.setContentType("application/json");
                String json = new Gson().toJson(stdserc);
                System.out.println("json = " + json);
                response.getWriter().print(json);

            } else {
                response.setContentType("application/json");
                String json = new Gson().toJson(null);
                response.getWriter().print(json);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
//        return mapping.findForward(SUCCESS);
//        return null;
    }

    /**
     * Action called on Edit button click
     */
    public void image(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        try {
            System.out.println("Inside the image responce action");
            response.setContentType("image/jpeg");
            Academicyearmaster academicyearmaster = (Academicyearmaster) request.getSession().getAttribute("academicyear");
//            String ss = getServlet().getServletContext().getRealPath("\\");
//            String filePath = ss + "img\\paichart.png";
            ServletOutputStream out = response.getOutputStream();
//             System.out.println("out = " + out);
//            String filePath2 = ss + "img\\paichart1.png";
//            ExecutorService executor = Executors.newFixedThreadPool(2);
            com.treamis.hr.employee.Sendded sendded = new com.treamis.hr.employee.Sendded(out, academicyearmaster);
            sendded.image();
//            executor.execute(sendded);

        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO: implement edit method
//        return mapping.findForward(SUCCESS);
    }

    /**
     * Action called on Delete button click
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        // TODO:implement delete method
        return mapping.findForward(SUCCESS);
    }

    /* And your JSP would have the following format for submit buttons:

     <html:form action="/test">
     <html:submit property="method">
     <bean:message key="button.add"/>
     </html:submit>
     <html:submit property="method">
     <bean:message key="button.edit"/>
     </html:submit>
     <html:submit property="method">
     <bean:message key="button.delete"/>
     </html:submit>
     </html:form>
     */
}

Java代码生成图像:

   try{
 ChartUtilities.writeChartAsJPEG(out, chart, 600, 400, info);
//            System.out.println("file2 = " + file1);
        } catch (Exception e) {
            e.printStackTrace();
            return "success";
        } finally {
            out.close();            
        }

答案在于Life cycle of ServletLife cycle of Servlet 即使一个Servlet收到多个请求,也只会创建Servlet类的一个实例。

检查是否有全局资源并进行修复。

或发布完整的servlet类以获得更好的响应。

希望能帮助到你!

我认为,如果对每个保存在磁盘中的文件使用不同的文件名,则不会再出现此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM