Servlets are useful while handling requests coming from web browsers and setting responses according to the requests and after process them.
In WSO2 carbon environment,OSGI UI bundles are exposed to web and sometimes there might be cases where we need to use a servlet and process on it according to web browser requests.Following steps will help you in such cases when you need to add a servlet for an UI bundle.
Pre-requesties-
- Any product server from WSO2 Carbon products [1]
- IntelliJ IDEA 10.0
- Maven 2.1.0 or higher
- JDK 1.6 or higher
1.First create a servlet class by extending HttpServlet abstract class inside an OSGI bundle.You can create a new UI-bundle by creating a new maven project and add this servlet class inside it..You can refer the WSO2 dashboard UI bundle code structure from here[2].Else For a new bundle you need to import 'javax.servlet' package from bundle pom.xml as we did for wso2 dashboard UI bundle pom.xml[3].
2.Then you need to add a runtime reference to 'servlet.context.service' and made available it to the bundle by the Service Component Runtime as below.
You can get an idea on this,by refering how we have done for WSO2 dashboard UI bundle as in DashboardUiServiceComponent.
3.Then you need to register that created servlet to the OSGI bundle by adding following entry to component.xml file which is in resources/META-INF/component.
In above
4.Add the functionalities that you need to process in the servlet to the overridden doGet() method or doPost() methods of the servlet.
4.Build the bundle you added the servlet.Then to test the servlet,if you have created a new bundle(maven project) build it and copy it to the location WSO2_Product_server_home/repository/
http(s)://hostName:port/
Here
For your easier I have uploaded a sample OSGI UI bundle that has registered one servlet in it.You can download it from here.This UI bundle named as HelloWorld and a servlet called 'HelloWorldServlet' has registered in it by following above steps.Download it,unzip it and have a look on the code of it.Then build it and add the generated .jar file in target folder to the location server_home/repository/
http(s)://hostName:port/hello
When accessing above url you'll see 'helloWorld' word will print on web browser.
[2] https://svn.wso2.org/repos/ wso2/trunk/carbon/components/ dashboard/org.wso2.carbon. dashboard.ui/
[3]https://svn.wso2.org/repos/ wso2/trunk/carbon/components/ dashboard/org.wso2.carbon. dashboard.ui/pom.xml
[4]https://svn.wso2.org/repos/ wso2/trunk/carbon/components/ dashboard/org.wso2.carbon. dashboard.ui/src/main/java/ org/wso2/carbon/dashboard/ui/ internal/ DashboardUiServiceComponent. java
[5]https://svn.wso2.org/repos/ wso2/trunk/carbon/components/ dashboard/org.wso2.carbon. dashboard.ui/src/main/ resources/META-INF/component. xml
[3]https://svn.wso2.org/repos/
[4]https://svn.wso2.org/repos/
[5]https://svn.wso2.org/repos/
When I deploy the example you have provided to WSO2 BPS and try to access it I am redriected to the login screen. If I am logged in, the servlet works correctly. is this expected?
ReplyDeleteHi DIG,
DeleteYes it's the expected way.If you want to access the servlet without login,then you need to by-pass the servlet url by adding the 'bypass' parameter of the framework to component.xml file as in [http://code.google.com/p/open-space-blog-samples/downloads/detail?name=component.xml&can=2&q=].
You can view how we have done this for dashboard UI component,by referring above link [5].
Hi.
ReplyDeleteAll the URL of this article send to a not found page like this:
Not Found
The requested URL /repos/wso2/trunk/carbon/components/dashboard/org.wso2.carbon.dashboard.ui/src/main/resources/META-INF/component.xml was not found on this server.
Hi,
DeleteSorry for the late reply.I have updated the downloaded link.
i have manven compilation issues with the provided source. I was planning to add a servlet to pre load some data which we use during mediation. My idea was to add it and configure it tomcat as a listner. Please suggest if there are any issues with that.
ReplyDelete