LSP Servlet interface

LSP pages can be used as an alternative to JSP in a Java Servlet based web application. LSP supports Servlet 2.3 and 2.4.

Follow these steps to use LSP in your web application.

  1. Put lsprt.jar in some shared CLASSPATH of your application server, or put lsprt.jar in the WEB-INF/lib directory of each web application using LSP.
  2. Compile your LSP pages into the WEB-INF/classes directory of your web application.
  3. Put any XSLT stylesheets used in the WEB-INF/classes directory of your web application.
  4. Use the nu.staldal.lsp.servlet.LSPManager class from your Servlets to execute LSP pages.

See the sample/servlet directory for a complete example.

LSP comes with a simple framework for building web applications. If you don't like this framework, you can also use LSP pages with the MVC frameworks Struts and Maverick.

Internationalization

The user's locale is determined from the Accept-Language header in the HTTP request. If the request gives more than one locale, they are tried in order of preference. This can be overriden by setting a locale in the user session, using the key LOCALE_KEY.

The localized strings can be accessed from LSP pages using the extension library, or from your code using the getLocalizedString(HttpServletRequest,String,String) method in nu.staldal.lsp.servlet.LSPManager.

Localized strings can be loaded from any source by implementing the interface nu.staldal.lsp.servlet.LocaleBundleFactory, and set the Servlet context init parameter "nu.staldal.lsp.servlet.LocaleBundleFactory" to your implementation of nu.staldal.lsp.servlet.LocaleBundleFactory.

Property files

The default implementation is nu.staldal.lsp.servlet.PropertyLocaleBundleFactory, which loads from property files (in standard Java property format, see java.util.Properties) in the WEB-INF/classes directory of your web application. The property files are named LSPLocale_locale.properties (e.g. LSPLocale_en.properties). You may place default strings in a file named LSPLocale.properties (for use when no locale match). Note: Java property files must be encoded with iso-8859-1, and characters not present in this encoding must be escaped with Java Unicode escapes. If you don't like this, use the XML format instead.

XML files

Use nu.staldal.lsp.servlet.XMLLocaleBundleFactory to load localized strings from an XML file. Add this to WEB-INF/web.xml:

<context-param>
  <param-name>nu.staldal.lsp.servlet.LocaleBundleFactory</param-name>
  <param-value>nu.staldal.lsp.servlet.XMLLocaleBundleFactory</param-value>
</context-param>

The XML files use the same naming convention as the property files, but with .xml extension. The XML files must adhere to this DTD (but does not need to have a DOCTYPE declaration). Use the page element to specify that a string only applies to a specific LSP page, strings outside any page element specifies global strings.

<!-- root element -->
<!ELEMENT locale ((string|page)*)>

<!ELEMENT page (string*)>
<!ATTLIST page
	name NMTOKEN #REQUIRED
>

<!ELEMENT string (#PCDATA)>
<!ATTLIST string
	key NMTOKEN #REQUIRED
>

SQL Database

Use nu.staldal.lsp.servlet.SQLLocaleBundleFactory to load localized strings from an SQL database. Add this to WEB-INF/web.xml:

<context-param>
  <param-name>nu.staldal.lsp.servlet.LocaleBundleFactory</param-name>
  <param-value>nu.staldal.lsp.servlet.SQLLocaleBundleFactory</param-value>
</context-param>

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

<context-param>
  <param-name>nu.staldal.lsp.servlet.LOCALETABLE</param-name>
  <param-value>LOCALEBUNDLE</param-value>
</context-param>

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

Where TheDB can be any identifier for your database. The database table should be setup like this:

CREATE TABLE LOCALEBUNDLE (
    LOCALE VARCHAR(8) NOT NULL, 
    LSPPAGE VARCHAR(16) NOT NULL, 
    THEKEY VARCHAR(16) NOT NULL, 
    VALUE VARCHAR(255), 
    PRIMARY KEY (LOCALE,LSPPAGE,THEKEY)
);

The length of the fields may be different, and the VALUE field may be of some "long text" type, as long as you can fetch it using ResultSet.getString(). The default locale is specified with LOCALE="*", and global strings are specified with LSPPAGE="*".