Tuesday, February 23, 2010

How to check a Zip file is corrupted or not using java.util.zip

The method below will take the zip file name as argument.
if the zip file is corrupted then it will throw exception as below.
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.(Unknown Source)
at java.util.zip.ZipFile.(Unknown Source)

in the catch block of ZipException we are catching the exception and returning the -1 as return.

if the method return type is 0 or -1 then we can say the zip file is corrupted or it is having zero length.


public int getZipFileEntries(java.io.File fileName) throws java.io.FileNotFoundException
{
ZipFile zipFile=null;
int zipEntries = 0;
try {
zipFile = new ZipFile(fileName);
zipEntries = zipFile.size();
} catch (ZipException e) {
zipEntries=-1;
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return zipEntries;
}

Tuesday, February 16, 2010

Sending Mail from commandline using JavaMail API

1) write a java class with main method as below.

=============================================================

package com.client.utilities;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* @param args
*/
public static void main(String[] args) throws SendFailedException {
String fromAddress="frommyself@compnay.com";
String toAddress="toyourself@company.com";
String subject="Hello am Sending mail from commandline";
String body="This is a test mail from JavaMailAPI";
try{
//prepare the properties
Properties props = new Properties();
/* if your working in a company , you can ask your network team for the name of SMTP host server of your
comapnny OR instead you can ask IPaddress of the SMTP host serevr */
//Replase the value assigned to smtpHostName String with your smtphost name
//eg : String smtpHostName="121.34.56.78";
String smtpHostName="name of the smtp host server";
props.put("mail.smtp.host", smtpHostName);
Session session = Session.getInstance(props, null);
//Create a MImeMessage MimeMessage message = new MimeMessage(session);
MimeMessage message = new MimeMessage(session);
InternetAddress from = new InternetAddress(fromAddress);
//Set from address to message
message.setFrom(from);
InternetAddress to = new InternetAddress(toAddress);
//Set to address to message , if you want to send mail to multiple reciepents you have to use
//setRecipient method
message.addRecipient(Message.RecipientType.TO, to);
//set the subject
message.setSubject(subject);
//set the body
message.setText(body);
//calling send method of javax.mail.Transport class
Transport.send(message);
}catch (SendFailedException sendfailedException)
{
sendfailedException.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

========================================================
2) using eclipseIDE , you can export the above java class as JAR.please see the stpes


step 1 : right click on the java class , Export-> expnad Java->select JARfile ->next->


step 2: in the JAR file specification page ,keep all the options set to default checks ,don't change any thing


step 3: in the same page give JAR file location where do you want to export lets say for eg: C:\SendMail.jar


step4:clcik next->next->finish


3)step2 will create JAR file at C drive. extract the jar file as like zip file. you can see the folder META-INF and your compiled class file folder(com/client/utilities).


4)open META-INF folder , open the file MANIFEST.MF file.


5) chnage the contents of MANIFEST.MF file as below.


Manifest-Version: 1.0com.client.utilities


Name: com/client/utilities/


Main-Class: com.client.utilities.SendMail


Class-Path: /apache-tomcat-6.0.20/apache-tomcat-6.0.20/lib/mail.jar /apache-tomcat-6.0.20/apache-tomcat-6.0.20/lib/activation.jar


Sealed: true


6)in the above step , to work with sending mail from java application you need to have mail.jar and activation.ajr, if you don't have these 2 files ,you can downlaod this from SUN downloads.

after that you keep this in the tomcat lib folder as a best practice. it is not mandatory to keep in the tomcat lib folder, you can create folder in C drive as C:\JARS , then place 2 jars inside this folder.but you need to change the Class-Path attribute of MANIFEST.MF file as

Class-Path: /JARS/mail.jar /JARS/activation.jar.

save the file.

7)zip the META-INF folder , this will have the MANIFEST.MF file which you saved in the above step. +your class file folder (com/client/utilities/SendMail.class) , rename the zip as SendMail.jar.

8)Now your jar file is ready , C:\SendMail.jar

9)run this jar file from command prompt as below

java -jar SendMail.jar

Please Comment about this post. if you have any questions also you can ask me.

Monday, February 8, 2010

Standard actions available in jsp

What are the standard actions available in JSP?


The standard actions available in JSP are as follows:



: It includes a response from a servlet or a JSP page into the current page. It differs from an include directive in that it includes a resource at request processing time, whereas the include directive includes a resource at translation time.

: It forwards a response from a servlet or a JSP page to another page.

: It makes a JavaBean available to a page and instantiates the bean.

: It sets the properties for a JavaBean.

: It gets the value of a property from a JavaBean component and adds it to the response.

: It is used in conjunction with ;, <, or="" plugin="">; to add a parameter to a request. These parameters are provided using the name-value pairs.

: It is used to include a Java applet or a JavaBean in the current JSP page.

Difference between Webserver & Application Server

Difference between AppServer and a Web server


(1) Webserver serves pages for viewing in web browser, application server provides exposes businness logic for client applications through various protocols

(2) Webserver exclusively handles http requests.application server serves bussiness logic to application programs through any number of protocols.

(3) Webserver delegation model is fairly simple,when the request comes into the webserver,it simply passes the request to the program best able to handle it(Server side program). It may not support transactions and database connection pooling.

(4) Application server is more capable of dynamic behaviour than webserver. We can also configure application server to work as a webserver.Simply applic! ation server is a superset of webserver.

A Web Server understands and supports only HTTP protocol whereas an Application Server supports HTTP,TCP/IP and many more protocols. Also many more features such as Caches,Clusters,Load Balancing are there in Application Servers which are not available in Web Servers. We can also Configure Application Servers to work as Web Server. In short, Applicaion Server is a super set of which Web Server is a sub set.

Difference between collection and collections

collection is an interface which defines methods to represent group of objects as single entity.

collections is an utility class for defining utility methods like sorting and searching methods.

Error-Java Heap Scape Out Of Memory Error

When you are running webapplication in tomcat , if you get the error like
Error-Java Heap Scape Out Of Memory Error ,
Solution :
1) Set a System Environmental Varaiable
_JAVA_OPTIONS=-Xmx1G
this means we are increasing the Java heap memory by setting this environmental variable
2) Restart the tomcat. when server again started it will pick the _JAVA_OPTIONS variable so you will get out of the above error

Sunday, February 7, 2010

framework

Frame work is a special software which is designed based on a architecture having the capability to generate the some inetegration logic dynamically is called as architecture.