目录

缺省servlet说明

缺省servlet说明

润乾提供的这个servlet是将打印配置以文件的形式保存在服务器上的用户主目录(user.home)中,文件名叫clientPrintSetup.properties。其源程序如下:

package com.runqian.report4.usermodel; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;import java.util.*;import com.runqian.base4.util.*;public class PrintSetupServlet extends HttpServlet { public void service( HttpServletRequest request, HttpServletResponse response ) { PrintWriter pw = null; try { request.setCharacterEncoding( "GBK" ); response.setContentType( "text/html; charset=GBK" ); pw = response.getWriter(); String userHome = System.getProperty( "user.home" ); String fileName = userHome + File.separator + "clientPrintSetup.properties";String action = request.getParameter( "action" ); String key = request.getParameter( "key" ); //存取打印设置的关键字 Properties p = new Properties();if( "write".equalsIgnoreCase( action ) ) { //保存 FileInputStream fis = null;try {fis = new FileInputStream( fileName );p.load( fis ); }catch( Exception e ) {} finally {try{ if( fis != null ) fis.close(); }catch( Exception ex ) {} } String width = request.getParameter( "width" ); //纸张宽 String height = request.getParameter( "height" ); //纸张高 String x = request.getParameter( "x" ); //左边距 String y = request.getParameter( "y" ); //上边距 String w = request.getParameter( "w" ); //内容区宽 String h = request.getParameter( "h" ); //内容区高 String orientation = request.getParameter( "orientation" ); //纸张方向 String printerName = request.getParameter( "printerName" ); //打印机名 String setup = width + "|" + height + "|" + x + "|" + y + "|" + w + "|" + h + "|" + orientation + "|" + printerName; p.setProperty( key, setup ); FileOutputStream fos = null; try { fos = new FileOutputStream( fileName );p.store( fos, "Client user's print setup of report" );}catch( Exception e ) {} finally { try{ if( fos != null ) fos.close();}catch( Exception ex ) {}} pw.println( "ok" );} else {FileInputStream fis = null; try { fis = new FileInputStream( fileName ); p.load( fis );}catch( Exception e ) {}finally { try{ if( fis != null )fis.close(); }catch( Exception ex ) {}} String setup = p.getProperty( key ); if( setup != null ) { ArgumentTokenizer at = new ArgumentTokenizer( setup, '|' ); //以下是向客户端传送打印设置,每行一个属性,属性名含义与前面保存时的相同pw.println( "width=" + at.nextToken() ); pw.println( "height=" + at.nextToken() ); pw.println( "x=" + at.nextToken() );pw.println( "y=" + at.nextToken() );pw.println( "w=" + at.nextToken() );pw.println( "h=" + at.nextToken() ); pw.println( "orientation=" + at.nextToken() );pw.println( "printerName=" + at.nextToken() ); pw.println( "setup=yes" ); //输出这一行,说明此报表保存有打印配置} elsepw.println( "setup=no" );//输出这一行,说明此报表没有保存打印配置 } pw.flush(); } catch ( Exception e ) { if( pw != null ) pw.println( "setup=no" ); '}