Barcode for Java's Java Servlets may be used to add server-side barcode
generation capability to a dedicated web server. Barcode for Java's Servlet is compatible with
all browsers and is easy to embed
in HTML as an image with the <IMG>
tag.
Servlet Implementation:
Example Using
JSDK:
- Place the barcode JAR or class files in the CLASSPATH. In Windows®,
this is accomplished by modifying the CLASSPATH environment variable to include
the full path to the JAR file. As an example, the following will add
LinearBarCode.jar to the classpath:
SET CLASSPATH=.;c:\java\classroot\;c:\jars\linear.jar
-
Install JDK 1.2 or above or verify that JDK 1.2
or above is installed on the server. The encoder requires Java 2 or JDK
1.2 or above to create the JPEG files used by the servlet.
Special requirements for Linux and Unix servers:
- Be sure Java.awt GUI functions are available in the environment. If the
Java environment has the java.awt library stripped out, then
the encoder will be unable to create image files.
- If the UNIX or Linux server does not have an X-Window environment,
then
either (1) use Headless Java or (2) connect to a remote X
Server or (3) install the X Windows environment and run an X Windows
session or (4) install an emulator, PJA library or virtual frame buffer.
- Install the appropriate
servlet server code on the server and make sure it is specified in the CLASSPATH. For example:
SET CLASSPATH=.;c:\java\classroot\;c:\jars\linear.jar;c:\jsdk2.1\server.jar;c:\jsdk2.1\servlet.jar
- Per the JSDK instructions, edit c:\jsdk2.1\examples\WEB-INF\servlets.properties
to include the following statement:
LinearServlet.code=com.businessrefinery.barcode.linear.LinearServlet
- Next, start the servlet server by executing C:\jsdk2.1\startserver.bat.
- After the servlet server is started, type the following code
into the browser and specify applet parameters to create the barcode.
This URL creates the a barcode encoding "123456789" with a
height if .8cm:
http://localhost:8080/examples/servlet/LinearServlet?BARCODE=123456789&BAR_HEIGHT=.8
Servlet Implementation Using
ServletExec
ISAPI on Windows® Server:
- In the servlet administration area choose Configure.
- Choose Add Servlet and enter the servlet name, class and
code base.
- Choose Submit and choose to Reload the new servlet.
- The servlet should now be viewable online by
entering a URL such as
http://yourserver.com/servlet/LinearServlet?BARCODE=123456789
Inserting barcodes into HTML - after the servlet is working:
- Barcodes may be integrated within web pages and HTML forms by using
an IMG tag, for example:
<img src="http://barcodeforjava.ws/servlet/LinearServlet?HEIGHT=80&WIDTH=170&BARCODE=123456780&BAR_HEIGHT=.8">
Notice in the above code that the HEIGHT and BAR_HEIGHT
are different. The HEIGHT and WIDTH parameter should be set to the
maximum allowed barcode size within a form.
-
The image that is created defaults to the screen
resolution of 96 DPI and may be compressed if necessary to obtain
smaller X dimensions or precision. Because of the screen’s resolution, the X
dimension can only be adjusted within increments of .03cm. For
example, if an X dimension of .015 is needed, the image on the web
page must be compressed by 50%. The following HTML tag reduces the
image by 50%; it will not look correct on the screen but it will print
correctly.
<IMG height=40 alt="Barcode Image" src="http://barcodeforjava.com/servlet/lin?HEIGHT=80&WIDTH=160&BARCODE=123456789"
width=80>
Creating secure servlets:
Other methods of servlet operation can be accomplished by developers that modify the servlet source code provided.
This may be necessary to make sure the end-user cannot manipulate
the servlet.
Auto-sizing and performance considerations:
When using the Servlet, auto sizing of the image is disabled if the
HEIGHT and WIDTH are manually specified. Because a temp image is created
to determine the size, performance is improved by specifying the size.