Showing posts with label Apache Tomcat. Show all posts
Showing posts with label Apache Tomcat. Show all posts

Sunday, November 10, 2013

How to configure Tomcat to support SSL or https

first you need to create a keystore with following command.

 keytool -genkey -keyalg RSA -keystore /home/chathuranga/test_chathu.keystore

then answers for the questions that prompts sequentially. once the keystore is created, you can use the following command to check whether your keystore is there.

keytool -list -keystore /home/chathuranga/test_chathu.keystore


Now it is the time to do the tocat SSL configuration.

In tomcatHome/conf/server.xml file, change the SSL configuration as follows.

 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
           keystoreFile="/home/chathuranga/test_chathu.keystore"
           keystorePass="password" />


Then restart the tomcat server and try to access the following URL.

https://locahost:8443


you will notice that, your tomcat installation supports SSL(HTTPS) now.


Thanks
Chathuranga Tennakoon
chathuranga.t@gmail.com

Thursday, September 26, 2013

Deploy JAX-WS web service in Tomcat Server

if the web service is going to be deployed in the tomcat server,it has to be developed as a web application project with some added configurations.
in order to start the web application project, i prefer to use maven archetype generate command as follows.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp


then it will give me a basic web application project that can be built with maven.
now this is the time to integrate my predefined web service with the web application project.

1. first create the relevant package structure in the web-app project and copy the web service src files there.


2. create the web service deployment descriptor ( sun-jaxws.xml ) file under the webapp/WEB-INF directory. (same directory where the web.xml resides).
 the endpoints should be defined as follows.

<?xml version="1.0" encoding="UTF-8"?>

<endpoints
        xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
        version="2.0">
    <endpoint
            name="calcService"     
            implementation="com.chathurangaonline.jaxws.samples.impl.CalculatorServiceImpl"

            url-pattern="/calcServiceUrl"/>
</endpoints>




3. in web.xml file (standard deployment descriptor) defines the WSServletContextListener and WSServlet class as follows.

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>

  <display-name>Archetype Created Web Application</display-name>

   <listener>
      <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener
       </listener-class>
    </listener>

    <servlet>
        <servlet-name>calcService</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>calcService</servlet-name>
        <url-pattern>/calcServiceUrl</url-pattern>
    </servlet-mapping>

</web-app>



3.  if you do all the above steps correctly, you project structure should be as follows.




now it is the time to build the web service. since the web service is published in the tomcat server, it should be built as a WAR file similar to simple web app.



4.  once the web service is compiled and package, copy the WAR file to the webapps directory in the tomcat server.


5. sometimes the web service not be loaded because the tomcat does not have the required libraries to server for web services (By default, Tomcat does not comes with any JAX-WS dependencies). In that case you need to download those dependencies and copy to the “${Tomcat}/lib” directory.

  •  Go here http://jax-ws.java.net/
  • Download JAX-WS RI distribution.
  • Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.
    • jaxb-impl.jar
    • jaxws-api.jar
    • jaxws-rt.jar
    • gmbal-api-only.jar
    • management-api.jar
    • stax-ex.jar
    • streambuffer.jar
    • policy.jar

once the above depdencies are copied, restart the tomcat server and try to access the web serice with following URL.

http://localhost:8080/calculator-service/calcServiceUrl

if the web service is working, it should display the following page in the browser.
then we are done.




Summary

the all steps taken for publishing a web service in tomcat can be summarized as below.
  1. Create a web service
  2. Create a sun-jaxws.xml, defines web service implementation class.
  3. Create a standard web.xml, defines WSServletContextListener, WSServlet and structure of a web project
  4. Copy JAX-WS dependencies to “${Tomcat}/lib” directory.
  5. Generate WAR file and copy to “${Tomcat}/webapp” directory.
  6. access the web service from browser and check whether it is up and running.


 SOURCE CODE : - https://github.com/chathurangat/jaxws-calculator-service-webapp

Hope this might be helpful for you!

Cheers
Chathuranga Tennakoon


Saturday, March 12, 2011

Enable HTTPS in Apache Tomcat server

I have assumed that you have properly installed Apache tomcat server and it is perfectly running on HTTP protocol.if you don't know how to install Apache tomcat , you can refer my previous blog post on  Installing Apache Tomcat .In order to configure HTTPS support in the tomcat server, you are required to folllow the steps given below.

1. it is required to have a self-signed server certificate for https. therefore you have to use keytool utility to generate such certificate for your tomcat server. type the keytool command in the command prompy \t to check whether the key tool utility is working.

C:\> keytool

if it is working, it should display a list of commands that are owned to the keytool utility. otherwise it will show you an error message indicating that keytool is not recognized as an internal or external command. if you get such error message, the reason is exactly you have not set JDK/JRE classpath in your command prompt. in such case, it is required to append C:\Program Files\Java\jdk1.6.0\bin (in my case) to the end of the path environment variable. then it will run the keytool utility properly.

Environment Variable
path = C:\Program Files\Java\jdk1.6.0\bin

2. then type the following command in the command prompt for generating the server certificate.

keytool -genkeypair -alias tomcat -keyalg RSA -keysize 1024 -dname "CN=localhost, OU=Organization, O=Company Name, L=City, S=State, C=US"
-validity 365 -keystore keystore

Enter keystore password: <enter a new password here>

 
Enter key password for <tomcat>

        (RETURN if same as keystore password): <just hit enter here>


The password you enter in the first password prompt will be the password for the keystore where your server certificate is stored.

3.Next, edit your Tomcat's conf/server.xml to enable the HTTPS connector. Look for a connector that looks like this:

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


by default, this has been commented and you are required to remove the comment.

Tuesday, February 8, 2011

Installaing Apache Tomcat in Windows Platform

Today i am going to discuss how to install Apache Tomcat server in the windows platform. i think this post will be helpful to most of the people ho are new to java(specially J2EE) developments. i decided to compose this article because the most of my friends were confusing and messing the things up when installing and configuring the Apache tomcat server. I will be installing Apache tomcat 7 in my PC.

prerequisite for Apache Tomcat 7:
JDK 1.6 or higher version

you can download the Apache tomcat 7 or latest through the Apache Software Foundation official website for Tomcat server. i have downloaded the binary distribution version for the installation.

then follow the installation instructions as below.

1. extract the ZIP file and copy the  extracted apache-tomcat-7.0.23 directory to the C:\Program Files

2. then create the following environmental variables.

 JAVA_HOME(the value should refers the java installation directory e.g:- C:\Program Files\Java\jdk1.6.0)

CATALINA_HOME(the value should refers the Apache tomcat extracted directory e.g:- C:\Program Files\apache-tomcat-7.0.23)


3. now  the tomcat server is installed  and you can run and test the server as follows.
   run the startup.bat file that is inside the bin directory of the tomcat server  using the windows command prompt(cmd). then it will startup and run the server.

4. browse http://localhost:8080 url and you will be able to see apache server is running.

(Note: by default, the apache tomcat server is running on port 8080. if you need to change the port it can be done by changing the server port declaration done in the server.xml file located inside the conf directory (conf/server.xml))

Hope this will helpful for you!

regards
Chathuranga Tennakoon
chathuranga.t@gmail.com