Sunday, February 24, 2013

Installing and Configuring Apache Tomcat Server

Downloading Apache Tomcat
  • Download the latest version of Apache tomcat viz. Apache Tomcat 7 from Tomcat 7 
  • According to your system configuration download the appropriate version.
  • I'm showing you for Windows 7 32-bit configuration viz. apache-tomcat-7.0.34-windows-x86.zip
  • Extract it in appropriate folder viz. C :\.
Setting up JDK Path 
  • Create an environment variable "JAVA_HOME" as follows
    • Click Start, right click on Computer and select Properties.
    • Select Advanced System Setting , click on Environment Variables.
    • In the User Variables Pane click on New .
    • In Variable name write JAVA_HOME and in Variable value give the address of JDK installation directory viz. if your java is installed inside program file of c:\ then give the address up to JDK1.6 directory.
 
Apache Tomcat Overview
  • The folder structure after extracting is shown below.
    • bin: Contains startup.bat for starting Tomcat and shutdown.bat for stopping the Server.
    • conf: Contains server.xml for adjusting Server configuration like port number, host name etc., tomcat-users.xml for adjusting user roles for authentication, web.xml i.e the deployment descriptor.
    • lib: Contains library files for supporting web applications viz. servlet-api.jar for Servlet application.
    • webapps: This is for application deployment. This contains actual project may be in .war file or in normal folder structure.
Adjusting Server Configuration
  • Typical server.xml contains information about server port, connector port, protocol, redirect port etc. If you want to change the corresponding ports u can change by editing it with your favorite WYSIWYG  editor. Normally you need to change the connector port if you have installed Oracle database because Oracle takes 8080 port.

    • server port:
    • connector port & redirect port:

  • Typical tomcat-users.xml contains information about users information i.e login information for manager status admin GUI etc.
    • First Leave this step and go to Start the Server step.
    • Original Content of tomcat-users.xml file given below

    • Change the content of this by the following line. This is actually new feature in version 7.
<role rolename="tomcat"/>
 <role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <user username="tomcat" password="tomcat" roles="tomcat"/>
 <user username="tomcat" password="tomcat" roles="manager-gui"/>
 <user username="tomcat" password="tomcat" roles="manager-script"/>
 <user username="tomcat" password="tomcat" roles="manager-jmx"/>
 <user username="tomcat" password="tomcat" roles="manager-status"/>
 <user username="tomcat" password="tomcat" roles="admin-gui"/>
 <user username="admin" password="admin" roles="tomcat,manager-gui,manager-script,manager-jmx, manager-status,admin-gui"/>
    • Make sure to remove comment line before replacing this above content.

Starting the Server

  • Start the server by double clicking on startup.bat present in the bin folder.
  • Open your favorite browser viz. Mozilla Firefox  and type http://localhost:8090/ and hit enter.
  • If you get this screen then congratulation...
  • Now if you click on either "Server Status, Manager App, Host Manager", it will ask you for authentication. Now go to previous paragraph where we skipped earlier with tomcat-user.xml configuration.
  • Now put username as admin and password as admin and press enter and congratulation again...        
Developing Web Application
  • Develop your web project and put it in webapps directory and access it..
  • In another blog i will show you how to develop servlet application and deploy it in Tomcat Server.

Friday, February 22, 2013

Create a .EXE file from a .JAR file

     The output of a java program is a .class file unlike the output of C program i.e .exe file. So, it is easy to execute a C program but to run a java program you need java command. In a bigger view we address the .class files into an executable .jar(Java Archive) file. So just imagine if we convert the .jar file into an .exe file, how seamlessly we can execute a program, just double click on it and run the program. So to perform this task we have many free software tools available, out of this Lunch4j  is one of the tool. Now i am going to show you a simple demo using it.

Step-1:
    Download the Launch4j software from Launch4j. You can download .exe file or .zip file as per your wish. But, I recommend you to download the launch4j-3.1.0-beta1-win32.zip.
Step-2:
   Extract the file and save it in appropriate location.The folder structure is shown below.
Step-3
  Lunch the lunch4j.exe application by double clicking on it. A window opens like given below
Step-4
 Fill the required fields as follows in the Basic tab.
 Output file: It's your required .exe file.
 jar: It's your created .jar file.
 For the demo purpose i have used the demo jar file available in JDK installation directory.
 You can also fill the optional fields like icon , Priority etc.
Step-5
If you try to save it now you will find an pop up window like below
So go to JRE tab and fill the minimum JRE version according to your JDK installation viz. 1.6.0_14 for simplicity.
Step-6
     Save Configuration by clicking the   button.The Configuration file is saved as .xml or .cfg file. You can edit that in your favorite XML editor viz Notepad ++.
Step-7
     Click the Build Wrapper button  to convert the .jar to .exe . You can see the log file
   That's all you have successfully created the .exe file.
Step-8
 Execute the .exe file created by double clicking on it to watch Java 2D Demo.
 Step-9
  The Content of the .xml file is shown below.
Step-10
  Let's check the other tabs like Classpath, Header etc. which is self explanatory.

                             I hope you will enjoy it.



Wednesday, January 2, 2013

Simple Program to Connect to MS SQL Server from java Using JDBC

  1. Download the latest JDBC Driver for MS SQL Server from Microsoft JDBC Driver 4.0 for SQL Server. 
  2. Download the zip file and extract it.And find sqljdbc4.jar from it and copy it for later use in classpath or also you can put it in classpath environmental variable.
  3. Write the java program. Before writing java program make sure you have installed appropriate JDK version and correctly set the environment variable.
  4. import java.sql.*;
    class TestMSSql
    {
     public static void main(String args[]) throws Exception
     {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","123");//repalce your databse name and user name
     Statement st=con.createStatement();
     ResultSet rs=st.executeQuery("Select * from login");//replace your table name
     while(rs.next())
     {
      String s1=rs.getString(1);
      String s2=rs.getString(2);
      System.out.println("UserID:"+s1+"Password:"+s2);
     }
    con.close();
    }
    }
  5. Create a databse named "JDBCDemo" using Microsoft SQL Server Managemnt Studio.
    1. Start>All Programs>Microsoft SQL Server 2008> Microsoft SQL Server Managemnt Studio
    2.  Connect to Database Engine using your windows authentication or SQL Server authentication. In this case i am using "sa" as my username and "123" as password for login.
    3. In the Object Explorer right click on Database and click New Database
    4. Give the database name viz.JDBCDemo and choose the owner viz. user of the computer
    5. Create a table viz. login with two fields UserID and Password as nvarchar data type.
    6. Insert data into table to check the program.
  6. Before  running the program make sure TCP/IP Service is enabled as follows
    1. Start>All Programs>Microsoft SQL Server 2008>Configuration tools>SQL Server Configuration Manager
    2. Expand Sql Server Network Configuration choose your MS SQL Server Instance viz. MSQSLSERVER and enable TCP/IP..that's it
  7. Open a command prompt viz. Ctrl+R>cmd hit enter.
  8. Compile the program viz. javac TestMSSql.java
  9. Set the jar classpath viz. set classpath=C:\sqljdbc4.jar;.; 
  10. Run the program viz. java TestMSSql