Tuesday, October 8, 2013

Enable HTTPS/SSL on Tomcat Server

By default Tomcat server HTTPS or SSL is disabled. To enable this two steps need to be followed.

  1. Create a SSL certificate
  2. Enable the SSL Configuration

  • Create a SSL certificate

Tomcat server is based on JDK Environment, and JDK provides a tool to create a SSL certificate.
just execute a command Keytool from Command prompt or terminal as
keytool -genkey -alias <alias name> -keyalg <Encryption Algorithm> -keystore <keystore file path>

Here Alias name: can be any text
Encryption Algorithm: is usually RSA for cereating SSL certificate
keystore file path: path for the file to be created

During keystore creation process it will ask to set keystore password and Certificate details followed by tomcat password.
Once its done, our certificate for SSL is created & ready to use.

  • Enable the SSL Configuration
To enable SSL Configuration open server.xml inside tomcat server's conf directory.

Here search for

'<Connector port="8443"
               protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https"
               secure="true"
               clientAuth="false" sslProtocol="TLS" />'

Change it to

'Connector port="8443" 
               protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" 
               secure="true"
               clientAuth="false" sslProtocol="TLS"
               keystoreFile="<keystore file path>"
               keystorePass="<keystore password>" />'

Now we need restart tomcat server. on successfull restart our SSL is ready to use.
Navigate to "https://localhost:8443" in browser to test it.

Wednesday, August 7, 2013

Jetty Server : How to deploy a war file?

Jetty is an open-source project providing an HTTP server, HTTP client, and javax.servlet container.Jetty implements aspects of the Java EE specification, primarily the Servlet Specification.

Downloading the Jetty Distribution
The standalone Jetty distribution is available for download from the Eclipse Foundation:
http://download.eclipse.org/jetty
It is available in both zip and gzip formats; download the one most appropriate for your system. When you download and unpack the binary, it is extracted into a directory called jetty-distribution-VERSION. Put this directory in a convenient location.

jetty [JETTY_HOME]
+---bin
+---etc
+---lib
+---logs
+---resources
+---start.d
+---webapps
\---webapps.demo
    +---ROOT
    +---test-jaas.d
    \---test.d

Now extract the application war example.war to jetty/webapps/example.

Create a file example.xml in jetty/webapps.demo with following content

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<configure class="org.mortbay.jetty.webapp.WebAppContext">
<set name="contextPath">/example</set>
<set name="example"><systemproperty name="jetty.home" default="."/>/webapps/example</set>
</configure>


save it &amp; start the jetty server with command: java –jar start.jar
Now done.Open the web browser type the address as http://localhost:8080/example/

Wednesday, March 13, 2013

Moving Listitems between two listbox using javascript.

This example shows how to move items between two ListBox. Basically it allows us to move multiple list items from right to left & Left to right on click event.


<!DOCTYPE html>
<html>
    <head>
        <title>move accross List box</title>
        <style>
            .list{
                float: left;
            }
            #list1,#list2{
                width: 100px;
                height:100px;
            }
            input[type='button']{
                width: 30px;
                float: left;
                margin-top: 30px;
            }
        </style>
    </head>
    <body>
        <div class="list">
            <select name="list1" id="list1" multiple="multiple">
                <option value="opt1">opt1</option>
                <option value="opt2">opt2</option>
                <option value="opt3">opt3</option>
                <option value="opt4">opt4</option>
                <option value="opt5">opt5</option>
                <option value="opt6">opt6</option>
                <option value="opt7">opt7</option>
                <option value="opt8">opt8</option>
                <option value="opt9">opt9</option>
                <option value="opt10">opt10</option>
                <option value="opt11">opt11</option>
            </select>
        </div>
        <input type="button" name=">" value=">" onclick="moveacross('list1','list2')">
        <input type="button" name="<" value="<" onclick="moveacross('list2','list1')">
        <div class="list">
            <select name="list2" id="list2" multiple="multiple">
            </select>
        </div>
        <script>
            function moveacross(sourceID, destID) {
                var src = document.getElementById(sourceID);
                var dest = document.getElementById(destID);

                for(var count=0; count < src.options.length; count++) {

                    if(src.options[count].selected == true) {
                            var option = src.options[count];

                            var newOption = document.createElement("option");
                            newOption.value = option.value;
                            newOption.text = option.text;
                            newOption.selected = true;
                            try {
                                     dest.add(newOption, null); //Standard
                                     src.remove(count, null);
                             }catch(error) {
                                     dest.add(newOption); // IE only
                                     src.remove(count);
                             }
                            count--;
                    }
                }
            }
        </script>
    </body>
</html>

and the output is:

Enjoy...

Tuesday, July 17, 2012

Cannot modify header information - headers already sent.


When ever we are dealing with header function the common warnning we get is "Cannot modify header information - headers already sent". 

Lets see php.net mannual -"header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called."  So as it clearly indicate that if our code is generating a space or empty lines also result into this warnning. 

Now the solution: A solution for this is dont allow any output to print any thing on browser till all headers are executed. So we are adding ob_start() in the begnning of page & ob_flush() at the end of page. then what ever is the output will be there in the output buffer, So this time we can use header function without any worry. And your code will somthing like:

<?php
ob_start();
//Your code
header('Location: example.php');
exit()
//your code
ob_flush();
?>
In case we are dealing with session, then session should start after ob_start() otherwise again it will through same warnning.