I\'m using the following code as part of the GWT server-side class (servlet) for GWT-RPC.
private void getImage() {
HttpServletResponse res = this.ge
Servlets respond to varios HTTP methods: GET, POST, PUT, HEAD. Since you use GWT's new Image(url)
, and it uses GET, you need to have a servlet that handles GET method.
In order for servlet to handle GET method it must override a doGet(..)
method of HttpServlet.
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
//your image servlet code here
resp.setContentType("image/jpeg");
// Set content size
File file = new File("path/to/image.jpg");
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}
}
Then you must configure path to your servlet in your web.xml file:
<servlet>
<servlet-name>MyImageServlet</servlet-name>
<servlet-class>com.yourpackage.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyImageServlet</servlet-name>
<url-pattern>/images</url-pattern>
</servlet-mapping>
Then call it in GWT: new Image("http:yourhost.com/images")