我有一個網頁(htm),它有一個文本框(將其更改爲下拉選擇)。使用img標記顯示在servlet中的圖像未找到
我寫有圖片的名稱(巴紐,.JPG,.GIF),並打一個提交按鈕
一個servlet調用,我想顯示具有用戶輸入了該名稱的圖像在網頁上。
的圖像是存在於同一目錄中的servlet的.class文件都存在, 但是當我嘗試使用<img>
標籤(通過寫入爲PrintWriter的流)從servlet來顯示它,我得到的資源不可用。
我手動也試圖找到使用localhost:8080/webAppName/imgName
幾個圖像,但仍然找不到資源。
當我嘗試使用File
類的exists()
方法時,我也得到了錯誤。
該servlet工作正常,我已經顯示了一些文本作爲對客戶端的響應,但它無法找到圖像。
Servlet被調用爲HTTP GET。
下面是servlet代碼:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Expt16_3PassImg2Servlet extends HttpServlet
{
String IPMsg;
String imgName="";
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
imgName=request.getParameter("imgNameEntry");
boolean imgExists=true;
pw.println("<html>");
pw.println("<body>");
//if((new File("./"+imgName)).exists())
pw.println("<img src=\""+imgName+"\">");
//else
//pw.println("<h1>Image doesnt exist</h1>");
pw.println("</body>");
pw.println("</html>");
pw.println("<br>Sample Text");
}
}
我知道我們可以通過設置內容類型爲圖像/ JPEG顯示圖像,但我想用img標籤來顯示它。
順便說一句,我將我的servlet擴展到HttpServlet。
請注意您的servlet不是線程安全的。 – BalusC 2012-01-03 13:48:47
嗯感謝that..but thats只是一個示例虛擬代碼.. 它不是真正的.. ..! – ritesht93 2012-01-04 02:17:59