1. The servlet code that generates the barcode:
Import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.servletoutputstream;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.google.zxing.barcodeformat;import Com.google.zxing.writerexception;import Com.google.zxing.client.j2se.matrixtoimagewriter;import Com.google.zxing.common.bitmatrix;import com.google.zxing.oned.code128writer;/** * @Description: Generate barcode (CODE128 format) */public class Barcode1dservlet extends HttpServlet {/** * @Fields serialversionuid:default serialversionuid */pri Vate static final Long Serialversionuid = 1l;private static final String KEY = "KeyCode";p rivate static final String WIDTH = "Mwidth";p rivate static final String HEIGHT = "Mheight";p rivate static final String IMAGETYPE = "JPEG"; @Overrideprotect Ed void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {String keycode = req . GetParameter (KEY); if (keycode! = null &&! "). Equals (KeyCode)) {Servletoutputstream stream = null;try {code128writer writer = new Code128writer (); int Width=180;int Hei ght=60; String mwidth = Req.getparameter (WIDTH); if (mwidth! = null &&! "). Equals (Mwidth.trim ())) {try{width=integer.valueof (mwidth);} catch (NumberFormatException e) { &nBsp; //todo output to log}} String Mheight = Req.getparameter (HEIGHT); if (mheight! = NULL &&! "". Equals (Mheight.trim ())) {try{height = integer.valueof (mheight);} catch (NumberFormatException e) {//todo output to log}} stream = Resp.getoutputstream (); Bitmatrix m = Writer.encode (KeyCode, barcodeformat.code_128, width, height); Matrixtoimagewriter.writetostream (M, IMAGETYPE, stream);} catch (Writerexception e) {e.printstacktrace ();} finally {if (stream! = null) {Stream.flush (); Stream.Close ();}}} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { This.doget (req, resp);}}
2, sample servlet for generating qrcode QR code
Import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.servletoutputstream;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.google.zxing.barcodeformat;import Com.google.zxing.writerexception;import Com.google.zxing.client.j2se.matrixtoimagewriter;import Com.google.zxing.common.bitmatrix;import com.google.zxing.qrcode.qrcodewriter;/** * @Description: Generated QR code Author Lwei */public class Barcode2dservlet extends HttpServlet {/** * @Fields serialversionuid:serialversionuid * * Private static final Long Serialversionuid = 1l;private static final String KEY = "KeyCode";p rivate static final string SI ZE = "Msize";p rivate static final String IMAGETYPE = "JPEG"; @Overrideprotected void Doget (HttpServletRequest req, Httpserv Letresponse resp) throws Servletexception, IOException {String keycode = Req.getparameter (KEY); if (keycode! = null &&A MP;! "". Equals (keycode) {Servletoutputstream stream = null;try {int size=129; String msize = Req.getparameter (SIZE); if (msize! = null &&! "). Equals (Msize.trim ())) {try{size=integer.valueof (msize);} catch (NumberFormatException e) {//todo output to Log}}stream = Resp.getoutputstream (); Qrcodewriter writer = new Qrcodewriter (); Bitmatrix m = Writer.encode (KeyCode, Barcodeformat.qr_code, size, size); Matrixtoimagewriter.writetostream (M, IMAGETYPE, stream);} catch (Writerexception e) {e.printstacktrace ();} finally {if (stream! = null) {Stream.flush (); Stream.Close ();}}} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { This.doget (req, resp);}}
3. Configuration in Web. XML
<servlet> <servlet-name>barCode</servlet-name> <servlet-class>com.xxx. xxx.common.servlet.barcode1dservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>barCode</servlet-name> <url-pattern>/barCode</url-pattern> </servlet-mapping> <servlet> <servlet-name>qrCode</servlet-name> < Servlet-class>com.xxx. xxx.common.servlet.barcode2dservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>qrCode</servlet-name> <url-pattern>/qrCode</url-pattern> </servlet-mapping>
4. Page reference
</img> </img>
Enough detail ....
Example of using zxing to generate a barcode QR code and output to a Web page in a servlet