Monday, July 21, 2008

Reading a txt file Content from URL by JavaScript

The html file having this script should be in the same domain with txt file for accessing from mozilla firefox because of security reason. From internet explorer txt file from any url can be read.

var xmlhttp;
function getFile(pURL) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",pURL,true);
xmlhttp.onreadystatechange = showFileContent;
xmlhttp.send(null);
}

function showFileContent() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
var out=xmlhttp.responseText;
document.getElementById('theExample').innerHTML=out;
}
}
}

See my codes, 1 having this script and reads contents from 2.
  1. http://debasisroy144.googlepages.com/helloworld.html
  2. http://debasisroy144.googlepages.com/helloworld.txt

Friday, July 18, 2008

Sending mail with attachment by JAVA

For this class mail-1.4.jar file will be needed as a library which can be found in this following site
http://www.java2s.com/Code/Jar/wsit/Downloadmail14jar.htm

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

public SendEmail() {
}
public static void sendEmail(String host, String from, String to, String subject, String infoMessage) throws Exception {
sendEmailWithAttachment(host, from, to, subject, infoMessage, "n/a");
}
public static void sendEmailWithAttachment(String host, String from, String to, String subject, String infoMessage, String fileAttachment) throws Exception {

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getInstance(props, null);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);

// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();

//fill message
messageBodyPart.setText(infoMessage);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
if(!fileAttachment.equals("n/a")){
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("MarbilReport.pdf");
multipart.addBodyPart(messageBodyPart);
}

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send( message );
}
}

Thursday, July 17, 2008

Configuring Tomcat Server for JAX-WS Support

Step 1 – Install JAX-WS
Go to https://jax-ws.dev.java.net/2.1.3/ and click on the link to download the binary. Double click on the downloaded binary and this will extract the contents into a sub-folder jaxws-ri. If you look in jaxws-ri\lib you will see all the jars that we need for deploying to Tomcat

Step 2 – Copy JAX-WS jars to Tomcat
Copy all the jars from jaxws-ri\lib to Tomcat lib directory. Tomcat will now support WebServices.

Step 3 – Include the JAX-WS Jars in the Project Path to compile Web Services and Web Service clients
Just add the jar file jaxws-tools.jar can be found in the extracted folder jaxws-ri\lib.


For more details please visit this site, http://life-on-a-laptop.blogspot.com/

Dynamic Invoking of Web Service Methods

When creating a JAX-WS client a Class like the following is created,

public class ServiceName extends Service {
private final static URL SERVICENAME_WSDL_LOCATION;
static {
URL url = null;
try {
url = new URL("http://IP:PORT/CONTEXTPATH/ServiceName?WSDL");
} catch (MalformedURLException e) {
e.printStackTrace();
}
SERVICENAME_WSDL_LOCATION = url;
}

public ServiceName(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}

public ServiceName() {
super(SERVICENAME_WSDL_LOCATION, new QName("http://PACKAGENAME/", "SERVICENAME"));
}

Anyone can see this class in netbeans from file section going through build->generated->wsimport->client and then under the given package name. Naturally, when we create an instance of this service using IDE generated code, what happens is IDE puts the second constructor for it,

ServiceName service = new ServiceName();

which takes the static wsdl url. And now look at the first constructor, if we create service instance by using first constructor in our code then, wsdlLocation and serviceName parameters can be drawn from a configuration xml file. So, if we have done this in our project then if sevice wsdl location changed then we don't have to recompile the project by creating new JAX-WS client with providing the new wsdl location. We just have to change the wsdl url location in the configuration file. And getting the port from service then we can access the methods under that web service.