Monday, June 22, 2009

How to dynamic manage upload files name in web server

This topic is help to understand how to manage file name uses SQL Server and dynamic specific the client upload file name store in the server file system.
the purpose is save the file to the server file system without having the file name replicated problems. if client download the files from the server, the server can specify the file name depend which the real file name store in the server file system and a popup dialog on user browser  with a file name specify from SQL server but not the physical file name store in the server file system.
 
 
Over write MIME protocol
When Internet Explorer receives the header, it raises a File Download dialog box whose file name box is automatically populated with the file name that is specified in the header.
 
For ASP .NET
Response.AddHeader("content-disposition","attachment; filename=fname.ext");
 
For JSP
response.setHeader( "Content-Disposition", "attachment; filename=" + downloadFileName );
 
Note:
  1. You may have to specify the Content Type resp.setContentType( "application/x-download" );
  2. Another Content-Type can instate of the content-disposition but it bahaves is differently
    resp.setHeader( "Content-Type", "attachment; filename=" + downloadFileName );
References:
    For more information about content-disposition, see Request for Comments (RFC) 1806
    http://support.microsoft.com/kb/260519

JSP Example
<%@ page  language="java" contentType="text/html"%>
<%@ page import="java.io.*, java.util.*, javax.servlet.*"%>
<%
    if (request.getParameter("filename").equals("") == false)
    {
        String path = request.getParameter("path");
        String filename = request.getParameter("filename");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + request.getParameter("filename"));
        int iRead;
        FileInputStream stream = null;
        try
        {
            File f = new File(path + filename);
            stream = new FileInputStream(f);
            int ilength = 0;
            while ((iRead = stream.read()) != -1)
            {
                out.write(iRead);
            }
            out.flush();
        }
        finally
        {
            if (stream != null)
            {
                stream.close();
            }
        }
    }
%>

Tuesday, June 16, 2009

How to Load ResourceBundle

 
Load Resource from different location
// Load resource from specific file object
File f = new File("d:\\\\props\\res.jar");
URL url = f.toURI().toURL();
URL[] urls = new URL[]{url};
ResourceBundle lang = ResourceBundle.getBundle("tw.com.mycomp.res.properties", new Locale("en", "US"), new URLClassLoader(urls));
 
// Load resource from specific URL
URL[] urls = new URL[]{ new URL("file:///d:/props/errmsg.properties") };
ResourceBundle lang = ResourceBundle.getBundle("tw.com.mycomp.res.properties", new Locale("en","US"), new URLClassLoader(urls));
 
 

Wednesday, June 10, 2009

How to declare and initial array in java

Declares and initial an array in Java
 
int [] anArray;                         // can also be written as :  int anArray [];
 
int [] anArray = null;
int [] anArray = new int[10];    // can also be written as :  int anArray [] = new int[10];
 
String [] array = {"A", "B", "C"};
String [][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}};
 
Create an new Array object as a parameter in function
getNames( new String [] {"Smith", "Jones", "Oehler"} );
 
 

Monday, June 8, 2009

Query Statement for Test Connection

Oracle
SELECT 1 FROM DUAL
 
MSSQL
SELECT 1
 

Sunday, June 7, 2009

Create Schema in Oracle

Purpose
Create multiple tables and views and perform multiple grants in your own schema in a single transaction. If all statements execute successfully, then the database commits them as a single transaction. If any statement results in an error, then the database rolls back all of the statements.
 
conn / as sysdba

CREATE USER uwclass
IDENTIFIED BY uwclass
DEFAULT TABLESPACE uwdata
TEMPORARY TABLESPACE temp
QUOTA 10M ON uwdata;

GRANT create session TO uwclass;
GRANT create table TO uwclass;
GRANT create view TO uwclass;

conn uwclass/uwclass

-- first one that doesn't work (t3 does not exist)
CREATE SCHEMA AUTHORIZATION uwclass
CREATE TABLE t1
(tid NUMBER(10) PRIMARY KEY, last_name VARCHAR2(20))
CREATE TABLE t2
(tid NUMBER(10) PRIMARY KEY, last_name VARCHAR2(20))
CREATE VIEW t1t2_view AS
SELECT t1.tid, t2.last_name FROM t1, t3 WHERE t1.tid = t2.tid
GRANT select ON t1t2_view TO system;

-- then one that does
CREATE SCHEMA AUTHORIZATION uwclass
CREATE TABLE t1
(tid NUMBER(10) PRIMARY KEY, last_name VARCHAR2(20))
CREATE TABLE t2
(tid NUMBER(10) PRIMARY KEY, last_name VARCHAR2(20))
CREATE VIEW t1t2_view AS
SELECT t1.tid, t2.last_name FROM t1, t2 WHERE t1.tid = t2.tid
GRANT select ON t1t2_view TO system;
 

Thursday, June 4, 2009

The Properties of Tomcat Server

Tomcat System Properties
 
System.getProperty("catalina.base");
System.getProperty("catalina.home");
System.getProperty("catalina.config");
System.getProperty("user.dir");
 
ServletContext.getRealPath("/WEB-INF/");
 
 

How to compair two objects in java

 
// exact match for a class.
if (object.getClass() == MyClass.class)
{
    ...
}
 
// Determines if the specified Object is assignment-compatible 
// with the object represented by this Class. This method is the 
// dynamic equivalent of the Java language instanceof operator. 
// The method returns true if the specified Object argument is 
// non-null and can be cast to the reference type represented by 
// this Class object without raising a ClassCastException. 
// It returns false otherwise.
if (MyClass.isInstance(obj))
{
...
}