SHARE
Follow this article on Twitter Facebook LinkedIn Bookmark and Share
Home >> No Category

Using dynamic images in a JSP

Using dynamic images in a JSP

By:  Robert Cooney  On: 02 Nov 2000 For: Channelworld India 
 

Over the course of the summer, I had to go through a severe learning curve to write a Web catalogue application for a publishing company in Toronto. The focus of this column is the one area on which I got stuck for a long time: dynamic images.

My own search for information on the subject turned up scarce and ambiguous answers, and demonstrated that there are a lot of other people out there who wanted to do the very same thing as I.

These days, a Web page is nothing if it isn't dynamic. The demand for Internet applications that communicate with a database has grown exponentially since the e-commerce boom, and technologies such as Active Server Pages, Sun's Java Server Pages and Allaire's ColdFusion have become standard fare. JSP is a popular choice for the same reason the Java language itself is so successful: code portability. Your Java server pages and servlets will theoretically run on any platform, provided you have installed a Java application server. There are many excellent tutorials available for dealing with JSP, but they are generally lacking in their description of image handling. This is unfortunate, as most on-line catalogues require the ability to display a graphical representation of the product.

When an image is stored in a database in binary form, it does not have a URL associated with it. Therefore, it cannot be accessed using standard HTML tags and we must write Java code that will query the database, return and display the image. The trap many developers fall into is trying to write this code inside the JSP itself. This leads to many hours of frustration, because the content type is set to text/html and even if the image is successfully fetched it will only display as a long stream of character data. The solution is to write a separate servlet for image retrieval.

In order to compile servlets you may need to install the JavaServer Web Development Kit (JSWDK). It is available for download from Sun at http://java.sun.com/products/servlet/.

Writing the Servlet

The following code example assumes prior knowledge of Java and basic SQL. It will create a servlet that receives a product number as a parameter and outputs the corresponding image. Depending on your needs, database connection information and SQL statements can also be passed as parameters, but for simplicity's sake they are hard-coded here for connecting to an Oracle database.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class ImageGetter extends HttpServlet {

//Method to get the parameter (product number) into a String and //pass it as an argument to the getImage method

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


Sign up for our Newsletters

 












Print |  Views: 4347   |   Rating:ononononoff  (3 votes)
Rate this article on a scale of
1 to 5 stars,5 being the best.




Robert Cooney Robert Cooney is a contributor to the International Data Group (IDG) News Service, which publishes global technology stories from bureaus around the world to more than 300 publications in more than 60 countries.

Recent Canadian IT Jobs




blog comments powered by Disqus