Pages

Saturday, July 28, 2012

Adapter Class

In this article you will learn about adapter classes their use and how to write adapter classes manually.
Before going for adapter classes you need to have an idea about Abstract classes and Interfaces.

as we know if we declare an interface all its methods must be abstract and need to be override in the sub class which implements that interface.
>> So whats the problem here ? 
Its very time consuming and difficult task to provide definitions for each and every method of interface in the sub-classes.
>> what is the need of adapter classes ?
If you are not interested to override all the methods of interface you can make use of adapter classes.

Adapter class: it is an abstract class implementing any interface and it contains definitions of some of the methods of the interface it is implementing.this Adapter class can be inherited now by any class without any bounds of  overriding  all the methods of the interface because the adapter class contains the definitions of that methods.
Note: if a method isn't overridden  in the adapter class then it is necessary to be overridden it in the sub class otherwise compiler will give an error.

here is an example showing adapter class :

import java.io.*;
import java.util.*;
interface iface  
{
public void input(int x,int y);
public void biggest();
public void display();
}

abstract class adapter implements iface
{
int d,e;
public void input(int x,int y)
{
d=x;
e=y;
}
}
class big extends adapter
{
int c;
public void biggest()
{
if(d>e)
{
c=d;
}
else
{
c=e;
}
}
public void display()
{
System.out.println("biggest number is :"+c);
}

}
public class main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
big a=new big();
int g=sc.nextInt();
int h=sc.nextInt();
a.input(g,h);
a.biggest();
a.display();
}
}

In the above example the input() method is optional to override in the subclass because it is already overridden in the adapter class.
This example is just my attempt to make the concept clear,you may not find adapter classes much useful here in this example but in real time where interfaces contains tens of methods the programmers never like to override all of them directly in their classes so they use adapter classes and its a very effective technique.
It reduces code and saves time.

Friday, July 27, 2012

Creating a SSL Certificate


>> First of all You need JDK installed on your system to proceed further.
>> Now using the keyTool utility available with JDK we can create SSL certificates like shown in the screenshot.
use the command "keytool -genkey -alias tomcat -keyalag RSA"
* here RSA is the algorithm used for encryption of data.

     
>> You will see all the steps as shown in the above screenshot if you are going right.
>> After creation of .keystore file(SSL certificate) it is stored in the user' home folder automatically.
>> Now you need to configure the .keystore file in the server.xml file which is located inside %TOMCAT_HOME%\conf

               add the following element to the server.xml file.
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" keystoreFile="C:/Users/hsd/.keystore" secure="true" keystorePass="hsdhaka" clientAuth="false" sslProtocol="TLS" />
>> Now start the tomcat server
>> Enter the URL https://localhost:8443/ in addressbar of any browser and you will see the following page


This page indicates that your certificate is not created by a trusted vendor and thus can't be trusted,but the SSL security is on for your applications by doing so.

If you will click on
you will see the requested page as you expect it to be
but you can easily notice address bar of your browser

this shows that the resource you have requested is not trust worthy.
If you don't want to see this kind of alerts before your website is displayed get,your certificate designed and signed by a trusted SSL vendor in the market like thawte.



Wednesday, July 4, 2012

how to deploy a servlet application in tomcat

Step to Deploy servlet application on tomcat web-server:

Step-1  Installing Tomcat :
If you have tomcat server already installed on your system go to step-2 directly.
Otherwise download the tomcat setup or zip file from http://tomcat.apache.org/ and extract the files wherever you prefer.Extracting is enough in case of zip file download,no special installation setup is required.

Step-2 Setting environment variables
 >> Set JAVA_HOME variable to your JDK installation directory i.e C:\Program Files\Java\jdk1.6.0 in my
      System.It may be different on your system so check for it.
>> Set classpath for servlet-api.jar file which contains all the servlet libraries which is essential for compiling
      any servlet application.This servlet-api.jar file is provided with your server product in a location similar to
      "D:\apache-tomcat-6.0.29\lib\servlet-api.jar" ,this path may vary according to the installation location of 
       tomcat server.
       you need to execute this command from command prompt to set classpath:
       set classpath=D:\apache-tomcat-6.0.29\lib\servlet-api.jar;
       and your classpath is set proceed to next step.

Step-3 Designing Basic components like a servlet,html page.

>> Create a simple html file which will be used to invoke the servlet class.

//index.html

<html>
<center>
<form action="./myServlet" method="get">
<input type="submit"/>
</form>
</center>
</form>


>> Now its time to create a servlet class.

//Myservlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Myservlet extends HttpServlet
{
     public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
     {
          PrintWriter pw=resp.getWriter();
          pw.println("<html><center>this is servlet response</center></html>");
      }

}

Step-4 Compile the servlet class.

Open the command prompt
Change current directory to the one which contains your Myservlet.java file
Now execute command:   "javac Myservlet.java" 
It will generate the class file for your servlet if there is no error.

Step-5 Create a deployment descriptor file i.e web.xml
deployment descriptor contains the servlet information like servlet class,servlet mapping and url pattern for which the container will invoke the servlet.

//web.xml

<web-app>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>Myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>


Step-6 Arranging all the above components in a standard directory structure

Create a root folder for your application i.e Sample here in my example
Inside root folder create a new folder WEB-INF.
Inside WEB-INF create sub-folder classes and arreange the all the files as shown in the directory structure.
Your final directory structure should look like the below one.

Step-7 Deploy the Application 

Deployment is nothing but placing root folder of your application inside the
"D:\apache-tomcat-6.0.29\webapps "    folder  that is inside the server installation directory.

Step-8 Make a request to your application:
>> start the tomcat server by clicking on startup.batch file located inside the bin folder of your tomcat installation directory.
>>open any browser and type the following url  "http://localhost:8080/Sample/index.html "
     it will show your html page with a submit button which will call  Myservlet on clicking.

You are done,Good luck