Skip to content

Library to integrate eta wai applications with servlet api

License

Notifications You must be signed in to change notification settings

jneira/wai-servlet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wai-servlet

CircleCI

Library to integrate eta wai applications with the servlet api

Getting Started

  • Visit the Getting Started eta instructions to build and install the library and the examples
  • In src/Network/Wai/Servlet/Examples.hs you can find some examples of wai applications and code to generate a servlet class that can be deployed in your favorite servlet container.
  • There are two options to deploy wai-servlet apps: generating a war file to be deployed in a servlet container or run it directly in a embedded one; this is the way more similar to use wai warp for haskell wai applications.

Running an application in a embedded servlet container

  • You need to install and set the wai-servlet-jetty-adapter package as a dependency. Currently is the unique adapter implemented for wai-servlet.
  • You have to import the module Network.Wai.Servlet.Handler.Jetty with the run function and call it with the port server and your application:
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Types                 (status200)
import Network.Wai
import Network.Wai.Servlet
import Network.Wai.Servlet.Handler.Jetty

appSimple :: Application
appSimple _ respond = respond $
   responseLBS status200 [("Content-Type", "text/plain")] "Hello World"

main = run 3000 appSimple

Generating a war to be deployed in a servlet container

  • This option supposes some manual steps. There is plans to make etlas build automatically a war file, tracked in this issue.
  • The main function to generate the servlet is Network.Wai.Servlet.makeServiceMethod for example:
{-# LANGUAGE OverloadedStrings #-}
import Java
import Network.Wai
import Network.HTTP.Types                 (status200)
import Network.Wai.Servlet

appSimple :: Application
appSimple _ respond = respond $
   responseLBS status200 [("Content-Type", "text/plain")] "Hello World"

servSimple :: DefaultWaiServletApplication
servSimple = makeServiceMethod appSimple

foreign export java "service" servSimple :: DefaultWaiServletApplication
  • This code will generate a network.wai.servlet.DefaultWaiServlet java class that extends javax.servlet.GenericServlet suitable to use in a standard war file
  • To generate the war you have to create the standard directory structure:
    • webApp
      • static resources (jsp,html,etc)
      • META-INF
      • WEB-INF
        • web.xml (required if you use servlets)
        • classes
        • lib
          • wai-servlet-app.jar
  • The web.xml for the default wai servlet could be
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>wai-servlet-test</display-name>

	<servlet>
		<servlet-name>wai-servlet</servlet-name>
		<servlet-class>network.wai.servlet.DefaultWaiServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>wai-servlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>