How to Zip a folder in Java

September 18, 2007 · Posted in java · 1 Comment 

Using the in-built zip function of java, attached is a sample code to copy a folder and all its sub-folders into a zip file.

Java Program to Zip a folder

This program uses Commons IO – IOUtils library to copy files.

How to check GMail RSS/Atom feed

September 17, 2007 · Posted in java · 1 Comment 

Google provides a feed url to check your new mail(s) in your inbox. The url is https://gmail.google.com/gmail/feed/atom.

You would need to authenticate first with your gmail username and password and it will list the unread email(s).

I was reading about Rome and thought to write a small program utilizing its features to read my gmail feed. Attached is the java program to read the gmail feed.

Let me know if you have any comments.

Validate e-mail address with regular expression

August 2, 2007 · Posted in java, javascript · Comment 

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

All regular expressions start and end with forward slashes to differentiate them from ordinary string expressions. Most regular expressions start matches at the first character ^ and end at the last $.

Now we try to match the mailbox name which can include periods and dashes \w+ states one or more alphanumeric must be at the start of the name. ([\.-]?\w+)* allows periods or dashes to be included in the mailbox name with the trailing \w+ ensuring that those characters can not finish the name. The @ is the mandatory separator.

The domain name can have several .xx or .xyz suffixes such as .com.uk. Once again \w+ ensures that domain starts with an alphanumeric and ([\.-]?\w+)* allows for the dashes and periods. Finally (\.\w{2,3})+ ensures that there is at least one suffix of between 2 and 3 characters preceded by a period.

Note: This is not a completely foolproof validation as it does not account for new domain names of 4 or more characters. Also not all two and three letter combinations are legitimate domains!

Displaying a wait page for a struts action

June 4, 2007 · Posted in java, struts · Comment 

I was searching google to find a way to show a wait page when a struts form is submitted and the response is loaded. Sometimes it is required when the page has to process a lot of information or get huge amount of data from the database.

I found a really clean way to do so at http://wiki.apache.org/struts/StrutsPleaseWait .

Variable name for getters

February 28, 2007 · Posted in java · Comment 

It’s NOT the name of the member variable that counts, it’s the name of the get method.

You use the part after “get” as the property name.

Normally you would convert the first letter after “get” to lowercase, but if the second letter is uppercase also, then you don’t.

getXYAxis – the variable name should be “XYAxis”

getName – the variable name should be “name”

Legal Identifiers in Java

December 13, 2006 · Posted in java · Comment 
  • Identifiers must start with a letter, a currency character($), or an underscore(_).
  • Identifiers cannot start with a number.
  • After the first character, identifiers can contain any combination of letters, currency characters, underscores, or numbers.
  • Identifiers are case-sensitive
  • “enum” cannot be used as identifier because it is added as a Java keyword in 5.0

Heap size and Eclipse

October 29, 2006 · Posted in java · Comment 

To increase heap size of eclipse editor, modify eclipse.ini file to the values you need
-vmargs
-Xms128M
-Xmx512M
-XX:PermSize=64M
-XX:MaxPermSize=128M

Read The Init-Params Defined In The web.xml

August 20, 2006 · Posted in java · Comment 

[eg. of web.xml

<web-app>
<servlet>
<servlet-name>ReadInitParams</servlet-name>
<servlet-class>com.company.MyServlet</servlet-class>
<display-name>Example Servlet</display-name>
<init-param>
<param-name>emailHost</param-name>
<param-value>192.168.1.1</param-value>
</init-param>
</servlet>
</webapp>

In Servlet Code:

public void init(ServletConfig config) throws ServletException {
String emailHost = config.getInitParameter("emailHost");
}

or

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String host = getServletConfig().getInitParameter("emailHost");
}

In JSP Code:

<html>
<head><title>Read Init params from a JSP</title></head>
<body>
<%
String emailHost = null;

public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter("emailHost");
}
%>
<table border="1">
<tr><td>email server</td><td><%=emailHost%></td></tr>
</table>

<% // It can also be read directly from the implicit object - config %>
<%=config.getInitParameter("sys")%><br><br>

</body>
</html>

Difference between request.getParameter() and request.getAttribute()

July 23, 2006 · Posted in java · Comment 

The difference between getAttribute() and getParameter() is that getParameter() will return the value of a parameter that was submitted by an HTML form or that was included in a query string.

getAttribute() returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.

Runtime.getRuntime().exec()

May 5, 2006 · Posted in java · Comment 

To copy a file, you can use the following command:

copy source.txt destination.txt

To run this in Java,

String[] cmd = {“copy”,”source.txt”,”destination.txt”}

Runtime.getRuntime().exec(cmd);

This may generate error=2. To fix this, use the complete path of copy.exe

eg. C:\Windows\System32\copy.exe

« Previous PageNext Page »

  • Calendar

    February 2012
    M T W T F S S
    « Jan    
     12345
    6789101112
    13141516171819
    20212223242526
    272829