Using NETBEANS6 to develop OSGi applications (4)--servlet and HTTP services [88250 original]

Source: Internet
Author: User

Reprint please retain the author information:

Author: 88250

blog:http:/blog.csdn.net/dl88250

MSN & Gmail & qq:dl88250@gmail.com


SummaryLast time, we analyzed the design of the KF (Knopflerfish) framework and practiced the design of an OSGI based application framework. In this context, we will implement the OSGi specification Http Service based on KF, do a simple servlet combined with html/javascript practice.

about Web development based on OSGiBefore we begin, let's take a look at some of OSGi's attempts at Web applications.

To develop Web applications, the most important is the Web server (or application server). IBM, BEA, and JBoss application servers in the server's vendors use OSGi, although our main tasks are currently being applied, but we can understand these basic things.

Well, briefly describe how OSGi is developed on the Web:
1. Insert Web server as bundle into OSGiAt present, Apache Jetty can do it. With the Spring DM (SPRING-OSGI), Tomcat integration should also be possible in the future. Refer to this
2. Bundle using OSGi within the Web serverUsing Spring DM (SPRING-OSGI) in a web container can be done.

for nowIn Java Web Development, the Spring framework is preferred, but if the application is based on OSGi as the bottom, the situation is different:

1. With spring, view/persistence/transaction management is convenient from the perspective of web application development, and can be combined with alternative frameworks or technologies
2. Using OSGi, from the overall development perspective of the system, scalability, maintenance, and the overall structure of the system is clear, the business/control logic can be very good component words
3. Combined with the use of these two (Spring DM), the supported extensions are limited and the technology is rather complex to outweigh the gains

So, after weighing it, I prefer to use OSGi directly as a Web application, because now one of the projects at hand is simply using the example Html,ajax and servlet.

let's do a little bit of practice. Ready toDitto:-)

Start: 1. Create the projectOpen NB6 to create a common Java application Engineering--osgiservlet:



Note that the WWW folder is where the HTML source files are stored and placed directly under the SRC folder. Then you want to import 3 jar Libs:
HTTP_ALL-2.0.0.JAR:OSGI HTTP Service Implementation
Knopflerfish-osgi.jar: Basic OSGi implementation
Servlet-api.jar:sun's servlet implementation

2. Writing activator and Servlet classes

/*
* @ (#) Activator.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package cn.edu.ynu.sei.osgiservlet;

Import Java.net.URL;
Import java.util.Hashtable;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.knopflerfish.service.log.LogRef;
Import Org.osgi.framework.BundleActivator;
Import Org.osgi.framework.BundleContext;
Import org.osgi.framework.BundleException;
Import org.osgi.framework.ServiceEvent;
Import Org.osgi.framework.ServiceListener;
Import org.osgi.framework.ServiceReference;
Import Org.osgi.service.http.HttpContext;
Import Org.osgi.service.http.HttpService;

/**
* OSGi activator for <code>infoservlet</code>.
* @author 88250
* @version 1.0.0.0, Mar 5, 2008
*/
public class Activator implements Bundleactivator {

This is our world
static Bundlecontext BC;

static LOGREF log;

Static final String Res_alias = "/"; The HTTP server root

Static final String Res_dir = "/www"; Bundle Resource Directory

Static final String Servlet_alias = "/servlet/firstservlet"; A small servlet

Hashtable registrations = new Hashtable ();

public void start (bundlecontext BC) throws Bundleexception {

THIS.BC = BC;
This.log = new Logref (BC);

Servicelistener listener = new Servicelistener () {

public void servicechanged (Serviceevent ev) {
servicereference sr = ev.getservicereference ();

Switch (Ev.gettype ()) {
Case serviceevent.registered:
Setroot (SR);
Break
Case serviceevent.unregistering:
Unsetroot (SR);
Break
}
}
};

String filter = "(objectclass=" + HttpService.class.getName () + ")";

try {
Bc.addservicelistener (listener, filter);

servicereference[] srl = bc.getservicereferences (null, filter);
for (int i = 0; Srl!= null && i < srl.length; i++) {
Listener.servicechanged (New Serviceevent (serviceevent.registered,
Srl[i]));
}
catch (Exception e) {
Log.error ("Failed to set up listener for HTTP service", E);
}
}

public void Stop (bundlecontext BC) throws Bundleexception {
}

void Setroot (ServiceReference sr) {

if (Registrations.containskey (SR)) {
Return Already done
}

Log.info ("Set root for" + SR);

Httpservice http = (httpservice) bc.getservice (SR);

HttpContext context = new HttpContext () {

public boolean handlesecurity (HttpServletRequest request,
HttpServletResponse response)
Throws Java.io.IOException {
return true;
}

Public URL getresource (String name) {

When registering the root, it seems
Like we get no separator before the file.
Is that a bug?? Code Below is a workaround
if (Name.startswith (Res_dir) &&
Name.length () > Res_dir.length () &&
'/'!= Name.charat (Res_dir.length ())) {
Name = Res_dir + "/" + name.substring (Res_dir.length ());
}

Default to Index.html
if (Name.equals (Res_dir)) {
Name = "/www/index.html";
}

and send the plain file
URL url = getclass (). getresource (name);

return URL;
}

public string GetMimeType (string reqentry) {
return null; Server decides type
}
};

try {
Http.registerresources (Res_alias, res_dir, context);
Http.registerservlet (Servlet_alias, New Infoservlet (SR),
New Hashtable (), context);

Registrations.put (SR, context);
catch (Exception e) {
Log.error ("Failed to register Resource", E);
}
}

void Unsetroot (ServiceReference sr) {
if (!registrations.containskey (SR)) {
Return Nothing todo
}

Log.info ("unset root for" + SR);

Httpservice http = (httpservice) bc.getservice (SR);

if (http!= null) {
Http.unregister (Res_alias);
Http.unregister (Servlet_alias);
Bc.ungetservice (SR);
}
Registrations.remove (SR);
}
}

/*
* @ (#) Infoservlet.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package cn.edu.ynu.sei.osgiservlet;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import org.osgi.framework.ServiceReference;

/**
* The Application
* @author 88250
* @version 1.0.0.0, Mar 5, 2008
*/
public class Infoservlet extends HttpServlet {

ServiceReference HTTPSR;

Public Infoservlet (ServiceReference httpsr) {
this. HTTPSR&AMP;NB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.