| |
Servlet Source Example
IDAutomation's Servlet is compatible with all browsers and is easy to embed in HTML as
an image with the <IMG>
tag. See
example or view
the Servlet demo.
Purchase or Download
the Java Barcode Packages now.
Creating secure servlets:
Other methods of servlet
operation can be accomplished by programmers that modify the servlet source code
provided. This may be necessary to prevent the end-user from manipulating the
servlet. A working example is provided in the IDAutomationSecureServlet.java
file which is in the source code ZIP file of the linear package.
The following code is an example of the java class library being used as a
custom secure servlet:
//*****************************************************************
//
// JAVA Source for com.idautomation.linear; 3.1
//
// Copyright, IDAutomation.com, Inc. 2000-2004.
// All rights reserved.
//
// http://www.IDAutomation.com/java/
//
// NOTICE:
// You may incorporate our Source Code in your application
// only if you own a valid Java Developer License
// from IDAutomation.com, Inc. and the copyright notices
// are not removed from the source code.
//
//*****************************************************************
package com.idautomation.linear;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.Graphics2D.*;
import java.awt.*;
public class IDAutomationSecureServlet extends HttpServlet
{
/**
* Handle the HTTP POST method by sending an e-mail
*
*
*/
private boolean debug=false;
public void init() throws ServletException {
}
// MODIFY THIS METHOD TO RETRIEVE DATA TO ENCODE, CREATE THE BARCODE AND SET THE
PARAMETERS
private BarCode getChart (HttpServletRequest request) {
BarCode cb=new BarCode();
cb.code="SecureServlet";
return cb;
}
// Handle the request
// 1. create barcode
// 2. draw barcode in a Buffered Image
// 3. encode image as GIF or JPEG and send it to the browser
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
ServletOutputStream outb;
//default encoding type
String encode="jpeg";
if (request!=null) {
if (request.getParameter("FORMAT")!=null) encode=request.getParameter("FORMAT").toLowerCase();
if (encode.compareTo("gif")!=0) encode="jpeg";
}
response.setContentType("image/"+encode);
outb=response.getOutputStream();
// avoid caching in browser
response.setHeader ("Pragma", "no-cache");
response.setHeader ("Cache-Control", "no-cache");
response.setDateHeader ("Expires",0);
try {
// find sizes
int w=10;
int h=10;
// get the BarCode
BarCode cb=getChart(request);
if ((request!=null) && (request.getParameter("WIDTH")!=null) && (request.getParameter("HEIGHT")!=null))
{
w=new Integer(request.getParameter("WIDTH")).intValue();
h=new Integer(request.getParameter("HEIGHT")).intValue();
}
else {
//a temp image must be created to find the preferred size
cb.autoSize=true;
cb.setSize(170,90);
java.awt.image.BufferedImage imageTemp = new java.awt.image.BufferedImage(
cb.getSize().width,cb.getSize().height,java.awt.image.BufferedImage.TYPE_BYTE_INDEXED
);
java.awt.Graphics imgTempGraphics = imageTemp.createGraphics();
cb.paint(imgTempGraphics);
cb.invalidate();
w=cb.getSize().width;
h=cb.getSize().height;
imgTempGraphics.dispose();
}
java.awt.image.BufferedImage BarImage=new
java.awt.image.BufferedImage(w,h,java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics2D BarGraphics=BarImage.createGraphics();
if (debug) System.out.println("Size: "+w+" "+h);
cb.setSize(w,h);
cb.paint(BarGraphics);
if (encode.compareToIgnoreCase("gif")==0) {
// encode buffered image to a gif
// NOTICE - REMARKED NEXT 2 LINES OUT UNTIL UNISYS PATENT EXPIRES
cb.setSize(w,h);
//com.idautomation.linear.encoder.GifEncoder encoder = new
com.idautomation.linear.encoder.GifEncoder(BarImage ,outb);
//encoder.encode();
}
else
{
// create JPEG image
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(outb );
//increase the JPEG quality to 100%
com.sun.image.codec.jpeg.JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam( BarImage);
param.setQuality(1.0F,true);
encoder.setJPEGEncodeParam(param);
encoder.encode( BarImage,param );
}
} catch (Exception e) { e.printStackTrace();}
}
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException
{
try {
doGet(request,response);
} catch (Exception e) { e.printStackTrace();}
}
}
|