Demo Java[tm] Print Service API Servlet README
The following README may contain actual software programs in source
code form. This source code is made available for developers to use as needed,
pursuant to the terms and conditions of this license.
JPS Servlet adds dynamic behavior to Print Servers.
This README file discusses the Demo Servlet source code and describes the
instructions to install and execute the Demo Servlet.
The Servlet Code:
In the demo JPS servlet, a user selects several print attributes from
the choices presented in the input
form. The JPS Demo Servlet implements the doPost() method to handle
form submission.
<form ACTION="servlet/JPSDemo" ENCTYPE="multipart/form-data" METHOD=POST>
In the doPost() method, we construct a
MultipartRequest
object passing in the request, a directory to save files to, and the maximum
POST size we should attempt to handle. The servlet passes its request object
to the MultiPartRequest constructor, along with a directory relative to
the server root (here Tomcat's ROOT) where the print files
are to be saved, and a maximum POST size of 5 MB. The servlet uses
its MultiPartRequest to iterate over the files that were sent. In this
Demo Servlet, we send only one file and the servlet gets the file's
name and content type. It also gets a File reference which is used to initailize
the FileInputStream of the Print object.
We construct a Print object, get the Document properties from the request
object passed, and perform the print operations as detailed in the JPS
Demo.
Here is the code for the doPost() method:
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//Set the output content type to HTML
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MultipartRequest multi = new MultipartRequest(request, ".",
5 * 1024 * 1024);
Print p = new Print(); //Create a Print object
out.println("<PRE>");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()) {
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println("<UL>" + "<LI><B>"+name+"</B>: "
+ value + "</UL>");
}
out.println("</PRE>");
out.println("<H3> File Details:</H3>");
out.println("<PRE>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
if (name != null && name.length() != 0) {
p.filename = name;
}
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
if (f != null ) {
if (f.exists() ) {
p.fin = new FileInputStream(f);
int count = p.fin.available();
//out.println("name: "+name);
out.println("File name:<B> "+filename +"</B>");
out.println("File type: "+type);
out.println("Bytes read: "+count);
out.println("</PRE>");
} else {
out.println("Choose a file to Print !");
String site="http://localhost/JPS/FileNotFound.html";
response.setStatus(response.SC_NOT_FOUND);
response.setHeader("Location",site);
response.sendRedirect(site);
}
}
}
// Get the Document properties from the http request object.
p.fidelity = request.getParameter("fidelity");
p.filename = request.getParameter("filename");
p.chromaticity = request.getParameter("chromaticity");
p.copies = request.getParameter("copies");
p.destination = request.getParameter("destination");
p.jobname = request.getParameter("jobname");
p.jobsheets = request.getParameter("jobsheets");
p.media = request.getParameter("media");
p.filetypestr = request.getParameter("filetype");
p.orientation = request.getParameter("orient");
p.sides = request.getParameter("sides");
p.mediasize = request.getParameter("mediasize");
//All Attributes are in place
p.setRequest();
if(!(p.isSupportedFileType(filetypestr))) {
out.println("File Type is not supported");
System.exit(1);
}
//Calling this method sets the appropriate DocFlavor
p.setDocFlavor();
//Returns a list of Print Services
PrintService[] ps = p.getPrintServices();
p.addPrintAListener(ps);
//List of All print Services available
PrintService[] plist = p.listPrinters();
for(int i=0; i < plist.length; i++ ) {
out.println(plist[i]);
out.println("</PRE>");
}
//Default Print Service
PrintService defp = p.getDefaultPrintService();
//compromiseAttribute() method is called to
//remove one attribute at a time from the user
//specified attribute set, until
//we find a appropriate print service
String compromised[] = p.getCompromised();
if (compromised != null) {
for (int ck = 0; ck < compromised.length; ck++ ) {
if (compromised[ck] != null ) {
out.println(compromised[ck]);
out.println("</PRE>");
}
}
}
//List the Print Services found
PrintService[] services = p.getServices();
//List the supported attributes for each Print Service
if (services.length > 0) {
for(int j = 0; j < services.length; j++ ) {
Class [] psattribute =
services[j].getSupportedAttributeCategories();
for(int i = 0; i < psattribute.length; i++ ) {
Object value = services[j].getSupportedAttributeValues(psattribute[i],null,null);
if(value == null) {
break;
}
if (value.getClass().isArray()) {
Object[] v = (Object[])value;
for (int k=0; k<v.length; k++) {
if (k > 0) {
out.print("<B>"+v[k]+"</B>");
}
}
}
else {
out.print("<B>" + value + "</B>");
}
out.print("
");
out.println("<BR>");
}
out.println("<BR>");
}
}
PrintJListener pjl = new PrintJListener();
DocPrintJob pj = p.createDocPrintJob(ps[0]);
p.addPrintJListener(pj);
Doc d = p.createFinalDoc();
p.printJob(pj,d);
int setype = pjl.getSetype();
PrintJobEvent spje = pjl.getSpje();
if (setype != 0 ) {
out.println("<B>Data xfer completed by: "
+ setype +"</B>");
}
if ( spje != null ) {
int i = spje.getPrintEventType();
out.println("<B>" + spje +"</B>");
out.println( "<B>" +"Print Job Completed
Successfully" + "</B>");
}
out.println("<B>Print Request Completed!</B>");
}
Installing the Servlet
The following components are required to compile and execute the Demo JPS
Servlet:
Download instrcutions:
- Download and install J2SDK 1.4 or higher from http://java.sun.com/j2se/1.4.
- Download and install the Servlet container, Tomcat version 4.0.3
Light or later from http://jakarta.apache.org.
- Place jpsservlet-input-form.html in CATALINA_HOME/webapps/JPS. This form
displays several attribute choices and accepts user inputs.
- Download the servlet source code and
compile it with JDK 1.4 or higher. Copy the compiled class files
in CATALINA_HOME/webapps/JPS/WEB-INF/classes directory.
- Download the Multipartrequest packages (cos-ddMMMyyyy.zip)
from
http://www.servlets.com/cos/index.html. JPS Servlet uses the file upload
package from O'Reilly's MultiPartRequest Servlet. Copy the MultiPartRequest
class files (com.oreilly.servlet.*) in the
CATALINA_HOME/webapps/JPS/WEB-INF/classes directory.
We have used cos-19Jun2001.zip for testing.
- Download the Java[tm] Servlet 2.3 classes from
http://java.sun.com/products/servlet/download.html.
- Set the following environment variables:
JAVA_HOME to where jdk 1.4 is installed,
CATALINA_HOME to where tomcat 4.0.3 is
extracted,
CLASSPATH to include the servlet classes,
PATH to include your jdk 1.4 location and
O'Reilly classes, cos/classes/com/oreilly/servlet.
- Create a 'temp' folder in your CATALINA_HOME location if one is not already there.
Tomcat stores the temporary files needed for Java application environment printing here.
- Compile the source in your classes directory.
Run the Demo JPS Servlet:
Start the Tomcat server and by default the HTTP Connector
runs at port 8080 as defined in your server.xml file. From the browser, go to
http://hostname.domainname:8080/JPS/jpsservlet.html
Choose the print attributes from the choices presented. A sample input
form page can be viewed here. Choose
the file for printing along with the print job attributes, and click the Submit
button. The sample output can be viewed here upon
Print Job Completion.
DOC ID #791
|