Registerresources ()
1. Introduction
Registerresources (...) is a method provided in the org. osgi. Service. http. httpservice class. You can directly register static resources with the jetty server.
2. Instructions for use
Registerresources (string alias, string name, httpcontext context)
A. Parameter alias
Purpose:
Set the URL address relative to the Access Content
Constraints:
1) cannot be null
2) It must start /.
3) it can be set/
4) cannot end/
5) it cannot be the same as the alias that used, including the alias used by registeservlet.
6) wildcard characters *
7) it can be used as a filter. See example 2.
8) the configuration of Alias complies with the servlet 2.5 specification. The following figure illustrates the settings and usage (see section 2.5 "srv.3.4 Request Path elements" of servlet specification)
If alias is a specified file, you must point to this file in the name parameter (directory). Otherwise, the server will access the directory instead of accessing the file, of course, you cannot obtain the content of the required file in the access directory. Therefore, an error will be reported that the file cannot be found.
B. Parameter Name
Purpose:Set the Directory and file position in the bundle in alias.
Name is actually a directory name, which contains the access content set in alias.
When alias is set to a specific file name, the name also needs to be set to the full name of the file in the corresponding path
Constraints:
1) name cannot end with a slash (/)
2) name can be/
C. Parameter Context
Purpose: context content of the current bundle.
If it is null, equinox automatically creates a default context.
3. Example
Example 1:
Assume the website is http://www.teamlet.org alias = "/test"
Registerresources ("/test", "/", null)
You can access all static file contents under test through the http://www.teamlet.org/test
Example 2:
Assume the website is http://www.teamlet.org alias = "/test/*. jsp"
Registerresources ("/test/*. jsp", "/", null)
You can access all JSP content under test through the http://www.teamlet.org/test, and all other static content such as HTML cannot access
4. Comparison between resourceservlet and registerresources
A. Same:
Registerresources is a method provided by httpservice. You can directly register static resources with the jetty server.
The resourceservlet class registers static resources with the jetty server through several servletadaptors.
Both methods can only be used in three HTTP request methods: "Get", "Post", and "head". Other HTTP request methods cannot be executed.
The HTTP specification defines eight possible request methods:
Get retrieves a simple request that identifies a resource in a URI.
The head method is the same as the get method. The server only returns the status line and header label, and does not return the request document.
The Post Server accepts requests for data written into the output stream of the client.
The put server saves the request data as a request to specify the new content of the URI.
Delete server request to delete the resource named in URI
Options requests for server-supported request methods
The trace web server provides feedback on HTTP requests and headers.
Connect is a documented but not implemented method. It is reserved for tunnel processing.
B. Differences
Registerresources can directly register static resources without filter instances or servletadaptor adapters.
Resourceservlet can use servletadaptor to set filter to protect resources and set character sets and other preprocessing.
5. Practical code:
Package org. teamlet. osgi. Test. servlet. filter;
Import java. Io. ioexception;
Import javax. servlet .*;
Import javax. servlet. filter;
Import javax. servlet. filterconfig;
Import javax. servlet. servlet;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import org. Eclipse. Equinox. http. helper. bundleentryhttpcontext;
Import org. Eclipse. Equinox. http. helper. contextpathservletadaptor;
Import org. Eclipse. Equinox. http. helper. filterservletadaptor;
Import org. Eclipse. Equinox. http. helper. resourceservlet;
Import org. Eclipse. Equinox. jsp. Jasper. jspservlet;
Import org. osgi. Framework. bundleactivator;
Import org. osgi. Framework. bundlecontext;
Import org. osgi. Framework. servicereference;
Import org. osgi. Service. http. httpcontext;
Import org. osgi. Service. http. httpservice;
Import org. osgi. util. tracker. servicetracker;
Public class activator implements bundleactivator {
Private servicetracker httpservicetracker;
Public void start (bundlecontext context) throws exception {
Httpservicetracker = new httpservicetracker (context );
Httpservicetracker. open ();
}
Public void stop (bundlecontext context) throws exception {
Httpservicetracker. Close ();
}
Private class httpservicetracker extends servicetracker {
Public httpservicetracker (bundlecontext context ){
Super (context, httpservice. Class. getname (), null );
}
Public object addingservice (servicereference reference ){
Final httpservice = (httpservice) Context. getservice (reference );
Try {
Httpcontext commoncontext = new bundleentryhttpcontext (context. getbundle (), "/webroot ");
Httpservice. registerresources ("/JSP/*. jsp", "/", commoncontext );
Httpservice. registerresources ("/JSP/*. html", "/test", commoncontext );
} Catch (exception e ){
E. printstacktrace ();
}
Return httpservice;
}
Public void removedservice (servicereference reference, Object Service ){
Final httpservice = (httpservice) service;
Httpservice. unregister ("/jsp ");
Httpservice. unregister ("/JSP/*. jsp ");
Super. removedservice (reference, Service );
}
}
}