Friday, August 21, 2009

How do you communicate in between Applets & Servlets ?

We can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection and "tunnel" to the web server. The server then passes this information to the servlet in the normal way. Basically, the applet pretends to be a web browser, and the servlet doesn't know the difference. As far as the servlet is concerned, the applet is just another HTTP client.

What is the order of method invocation in an Applet ?

public void init() : Initialization method called once by browser.
public void start() : Method called after init() and contains code to start processing. If the user leaves the page and returns without killing the current browser session, the start () method is called without being preceded by init ().
public void stop() : Stops all processing started by start (). Done if user moves off page.
public void destroy() : Called if current browser session is being terminated. Frees all resources used by applet.

Wednesday, August 12, 2009

What is jGuard

jGuard is a java security framework based on JAAS. this framework is written for web applications, to resolve simply, access control problems.

Features
  • relies only on java 1.4 and j2ee 1.3 or higher

  • can be adapted on any webapp, on any application server

  • permit to a user to have more than one role simultaneously

  • does not depend on a web framework, or an AOP framework

  • build on top of the standard and very secure and flexible JAAS

  • authentications and authorizations are handled by pluggable mechanisms

  • handle authentication data stored in a database, an XML file, a JNDI datasource, an LDAP directory, Kerberos...

  • changes take effects 'on the fly' (dynamic configuration)

  • permissions, roles,and their associations can be created, updated,deleted on the fly through a webapp (an API is provided too)

  • each webapp has its own authentications and authorizations configuration

  • a taglib is provided to protect jsp fragment

  • support security manager

How list all loaded JDBC drivers and gets information about each one?

This example lists all loaded JDBC drivers and gets information about each one.
List drivers = Collections.list(DriverManager.getDrivers());
for (int i=0; i < drivers.size(); i++) {
Driver driver = (Driver)drivers.get(i);

// Get name of driver
String name = driver.getClass().getName();

// Get version info
int majorVersion = driver.getMajorVersion();
int minorVersion = driver.getMinorVersion();
boolean isJdbcCompliant = driver.jdbcCompliant();
}

How to connect to a SQL Server database using JDBC?

This example connects to a SQLServer database using the NetDirect JDBC driver.
Connection connection = null;
try {
String driverName = "com.jnetdirect.jsql.JSQLDriver"; // NetDirect JDBC driver
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase; // a JDBC url
String username = "username";
String password = "password";

// Load the JDBC driver
Class.forName(driverName);

// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}

How to export a MySQL Table to a Flat File?

The default format of a file exported from MySQL is as follows: the fields are separated by tabs, the lines terminated by 'n', and backslashes(), newlines (n), and tabs (t) escaped by a backslash. NULL is exported as N.
This example exports the data in a table called mysql_table2 to a file named outfile.txt.

try {
// Create the statement
Statement stmt = connection.createStatement();

// Export the data
String filename = "c:tempoutfile.txt";
String tablename = "mysql_2_table";
stmt.executeUpdate("SELECT * INTO OUTFILE "" + filename + "" FROM " + tablename);
} catch (SQLException e) {
}

How to list all the files in a directory?

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i < children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);