Friday, October 26, 2012

Configure tomcat for SSL protocol

In this post i will show you how to configure tomcat 7.x for SSL protocol in windows

First we want to generate certificate file using java keytool

1. Open command prompt and go to bin folder of java installation

  • CD %JAVA_HOME%\bin

2. type following commands (i used my password as abcd@1234 (This is called keystore password).Please       use your own)
  • D:\Program Files\Java\jdk1.6.0\bin>keytool -delete -alias tomcat -keypass abcd@1234        

      Use alias as tomcat.There will be generate .keystore which is hold your key inside C:\key. Please make
      sure there will be a directory called C:\key before type following command ( you can use your own
      directory as well). You will be ask some questions.Please give as localhost when asking Your first 
      and Last Name( Here we give as localhost because we configure it in local machine.If you are           configure tomcat in a server, Then you have to give the server name.For an example: If application hosting @ www.myapp.com then you have to use myapp as the Your First and Last Name).Others can be answered as you want.Type same password you use as
      keystore password.Other wise there will be some error occurred when running tomcat.
  • D:\Program Files\Java\jdk1.6.0\bin>keytool -genkey -alias tomcat -keyalg RSA -keystore c:/key/.keystore

      Type following commands as it is.( Change passwords and locations  you use as keystore password
      and .keystore file generated
  • keytool -export -alias tomcat -keypass abcd@1234 -file server.crt -keystore c:/key/.keystore
  • keytool -import -file server.crt -keypass abcd@1234 -keystore ..\jre\lib\security\cacerts

Now you having .keystore file inside c:\key (if you use the same location as tutorial) and file called cacerts %JAVA_HOME\jre\security . If you have those files that mean you are successfully generate certificate file.

Now lets configure tomcat

1. Go to tomcat install location and go to conf folder (in my case D:\tomcat\conf)

2. Open server.xml file in your favorite text editor

3. Find the commented line and comment out it

Before

<!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

After


 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />


4. Add the following lines in to that and change the bolded lines according to your configurations


<Connector port="8443" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="C:\Users\madura\Documents\.keystore"
keystorePass="abcd@1234"
truststoreFile="D:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.22\security\cacerts" 
SSLEnabled="true" protocol="org.apache.coyote.http11.Http11NioProtocol"/>

Now it is time t test this.


  • Open command prompt and go to %CATALINA_HOME%\bin
  • Type catalina.bat start   (tomcat server will start now)
  • Open a web browser and type https://localhost:8443 (If the configurations are ok , you will see the official tomcat server page

Congratulations.....

If not success  please go to from the begining..




How to install tomcat in windows

In tnis tutorial i will explain how configure apache tomcat 7.x for SSL protocol (https://)
I used apache tomcat 7.xx for my configuration

1. download apache tomcat fresh copy from the official web site and extract it some location in your hard disk( i used D:/tomcat)

2. Right click on MyComputer and click on properties-> Advaced System Settings -> Environment Variables (in Advanced Tab)







3. Click on the New button in System variables and add Variable Name as CATALINA_HOME
Variable Value as tomcat installation location (in my case D:/tomcat)


Then click on OK

4. Find the path variable in system variable -> Click on Edit and add the ; at end of the value. Then append it to path to bin folder of tomcat (in my case D:/tomcat/bin). Click OK and exit




Now You are install tomcat in windows successfully.Then test the installation as below.

* Open command prompt
* go to tomcat install location/bin

  •  D:
  • cd %CATALINA_HOME%
  • cd bin

* type catalina.bat start

  • There will be new window prompt and tomcat server may start

* open a web browser and go to link localhost:8080

  • If tomcat installation successful , then official tomcat page will be loaded.



Monday, October 15, 2012

delete file using javascript


Only work with internet explorer

<script type="text/javascript">
    // initialize ActiveXObject and create an object of Scripting.FileSystemObject.
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    fso.DeleteFile("C:\\Temp\\myFolder\\file.txt", true);

    // OR

    fso.DeleteFile("C:\\Temp\\myFolder\\*.txt", true);

    fso = null;
</script>

create file in local directory using javascript


This is only work with Internet explorer

<html>
<head>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Test.txt", true);
s.WriteLine('Hello');
s.Close();
}
</script>
</head>
<body onLoad="WriteToFile()">

</body>
</html>

Sunday, October 14, 2012

replace all html tags in a String using java

StringName.replaceAll("\\<.*?\\>", "").replaceAll("(?s)<!--.*?-->", "");

get data between two special character using java

Let say you want to get data between character ' and " of the below string

String str= 21*90'89"

You can do it really easy by using regular expression and java


Pattern pattern = Pattern.compile("'(.*?)\"");
Matcher matcher = pattern.matcher(str);
 if (matcher.find())
{
         System.out.println(matcher.group(1));
 }

Tuesday, October 9, 2012