LSP framework

See the Javadoc.

This framework consists of a Servlet which dispatches requests to services. Each service is a Java class extending the nu.staldal.lsp.framework.EasyService class. The mapping is done based in the class name, so no explicit mapping file is needed.

Services may use the nu.staldal.lsp.framework.Parameter annotation to automatically populate fields with HTTP request parameter values.

Services may use the nu.staldal.lsp.framework.PageParameter annotation to automatically pass page parameters to templates.

This framework supports including from an LSP page by using the <s:include> element in the Servlet extension library.

If you want to use a database, add the following to your WEB-INF/web.xml file:

<context-param>
  <param-name>nu.staldal.lsp.servlet.framework.DB</param-name>
  <param-value>jdbc/TheDB</param-value>
</context-param>

<resource-ref>
  <description>The DB Connection</description>
  <res-ref-name>jdbc/TheDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

and you will get a connection to the database in dbConn.

There are two mapping strategies available.

The normal mapping strategy

If your web application is mounted on the URL http://www.example.com/myapp and ServicePackages is set to com.example.services, the following mapping is performed:

Request URLJava class
http://www.example.com/myapp/Foo.scom.example.services.Foo
http://www.example.com/myapp/Bar.scom.example.services.Bar

If no service is found, an attempt is made to load an LSP page with the same name and display it without any parameters. If neither any LSP page is found, a HTTP 404 Not Found response is generated.

Add the following to your WEB-INF/web.xml file:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>nu.staldal.lsp.framework.DispatcherServlet</servlet-class>

  <init-param>
      <param-name>ServicePackages</param-name>
      <param-value>list of Java packages, separated by comma</param-value>
  </init-param>
  
  <init-param>
      <param-name>RequestCharset</param-name>
      <param-value>charset to decode HTTP requests with</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.s</url-pattern>
</servlet-mapping>

RequestCharset should be set to the same charset as your HTML pages are encoded with.

If you want to use a service (e.g. Menu) as welcome page to your web application, add the following to your WEB-INF/web.xml file:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>nu.staldal.lsp.framework.DispatcherServlet</servlet-class>

  <init-param>
      <param-name>ServicePackages</param-name>
      <param-value>list of Java packages, separated by comma</param-value>
  </init-param>

  <init-param>
      <param-name>RequestCharset</param-name>
      <param-value>charset to decode HTTP requests with</param-value>
  </init-param>
  
  <init-param>
      <param-name>DefaultService</param-name>
      <param-value>Menu</param-value>
  </init-param>  
</servlet>

<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.s</url-pattern>
</servlet-mapping>

<welcome-file-list>
  <welcome-file>.s</welcome-file>
</welcome-file-list>

and create an empty file .s in your web application.

See the sample/framework directory for a complete example.

The RESTful mapping strategy

If your web application is mounted on the URL http://www.example.com/myapp and ServicePackages is set to com.example.services, the following mapping is performed:

Request URLJava classExtraArgs
http://www.example.com/myapp/Foocom.example.services.Foo[]
http://www.example.com/myapp/Barcom.example.services.Bar[]
http://www.example.com/myapp/foo/Barcom.example.services.foo.Bar[]
http://www.example.com/myapp/Foo/17com.example.services.Foo["17"]
http://www.example.com/myapp/Foo/17/Barcom.example.services.Foo["17","Bar"]

The first URL path component which doesn't begin with a Java identifier character (such as a digit) stop the mapping. Any extra URL path components are added to a List which is passed as an attribute "ExtraArgs" in the ServletRequest. This will also end up in extraArgs in nu.staldal.lsp.framework.ThrowawayService

If no service is found, a HTTP 404 Not Found response is generated. (No attempt to load an LSP page is made.)

Add the following to your WEB-INF/web.xml file:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>nu.staldal.lsp.framework.RestfulDispatcherServlet</servlet-class>

  <init-param>
      <param-name>ServicePackages</param-name>
      <param-value>list of Java packages, separated by comma</param-value>
  </init-param>
  
  <init-param>
      <param-name>RequestCharset</param-name>
      <param-value>charset to decode HTTP requests with</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

RequestCharset should be set to the same charset as your HTML pages are encoded with.

If you want to use a service (e.g. Menu) as welcome page to your web application, add the following to your WEB-INF/web.xml file:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>nu.staldal.lsp.framework.RestfulDispatcherServlet</servlet-class>

  <init-param>
      <param-name>ServicePackages</param-name>
      <param-value>list of Java packages, separated by comma</param-value>
  </init-param>

  <init-param>
      <param-name>RequestCharset</param-name>
      <param-value>charset to decode HTTP requests with</param-value>
  </init-param>
  
  <init-param>
      <param-name>DefaultService</param-name>
      <param-value>Menu</param-value>
  </init-param>  
</servlet>

<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

See the sample/restful directory for a complete example.