簡體   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