Monday, November 29, 2004

Use iText to create pdf file

package uhn;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import java.io.*;
import java.awt.Color;

/**
*
* @author zlu
*/
public class Test {

/** Creates a new instance of Test */
public Test() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("\\temp\\Chap0101.pdf"));
document.addTitle("This is a test for iText.");
document.addAuthor("Zhibin Lu");
document.open();
document.add(new Paragraph("Hello World"));
Chunk chunk = new Chunk("Hello world", FontFactory.getFont(FontFactory.TIMES_ROMAN, 20, Font.NORMAL, new Color(255, 0, 0)));
chunk.setBackground(new Color(0xFF, 0xFF, 0x00));
document.add(chunk);
chunk = new Chunk("This is a test for no format text.");
document.add(chunk);
document.close();
}

}

Thursday, November 25, 2004

Create PDF file with Java

iText is a library that allows you to generate PDF files on the fly.

I tried this package a little bit today, and found it was pretty cool. I think it is very useful to create some read-only, generic format files like receipts.

Monday, November 22, 2004

IDEs for servlet

NetBeans:
built-in webapps support and Tomcat server.

Eclipse:
need to install Webtools plugin and Aston plugin.

Servlet Template

//Code created by NetBeans 4.0 beta 2

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {

/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);

}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");

out.println("</body>");
out.println("</html>");

out.close();
}

/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}

}


Autoincrement primary key for Oracle

create sequence test_seq

start with 1
increment by 1
nomaxvalue;

create trigger test_trigger
before insert on test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;

insert into test values(test_seq.nextval, 'voila!');

select sequence_name from user_sequences;
select trigger_name from user_triggers;
drop sequence test_seq;
drop trigger test_trigger;
alter trigger test_trigger disable;
alter trigger test_trigger enable;

Note:
a sequence is used to generate a primary key/unique value. Nothing more, nothing
less. any assumptions about "gap free" or "lost numbers" is in vain.

things in the shared pool (where sequences are cached) are aged out using an LRU
(least recently used). Hence, if you select s.nextval and then don't touch S for
a while and we need space in the shared pool -- out goes S and out goes any
cached values for S.

Wednesday, November 17, 2004

cvs

CVS is a version control system.
Project Home: https://www.cvshome.org
cvs GUI: http://www.wincvs.org/

Set cvs repository directory with CVSROOT
use 'cvs init' to initialize the repository
use 'cvs import' to upload the project
use 'cvs checkout' to download files
use 'cvs commit' to upload files
use 'cvs update' to synchronize

Monday, November 15, 2004

Create Jpeg picture with Java

/*Note: Under unix or Mac, Use Java 1.4, which supports headless Java (no more X server required). Run your application with -Djava.awt.headless=true given as parameter to the virtual machine.
When running Tomcat, use export JAVA_OPTS=-Djava.awt.headless=true*/

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class Hello {

public static void main(String[] args) throws Exception {
BufferedImage img = new BufferedImage(600, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.red);
g2d.fillRect(0, 0, 150, 300);
g2d.fillRect(450, 0, 150, 300);
g2d.setColor(Color.white);
g2d.fillRect(149, 0, 300, 300);
File imgfile = new File("/Users/zhibin/tmp/test.jpg");
javax.imageio.ImageIO.write(img, "jpg", imgfile);
}

}///:~