Example of a basic servlet

The following simple Gosu servlet runs in the context of the PolicyCenter application, but provides no authentication. For a production servlet, you must add authentication. This example servlet responds to URL substrings that start with the string /test. If an incoming URL matches that pattern, the servlet displays the PathInfo property of the response object, which contains the path.

package mycompany.test.servlets

uses gw.servlet.Servlet
uses javax.servlet.http.HttpServletRequest
uses javax.servlet.http.HttpServletResponse
uses javax.servlet.http.HttpServlet

@Servlet( \ path : String ->path.matches("/test(/.*)?"))
class TestingServlet extends HttpServlet {

  override function service(req: HttpServletRequest, response: HttpServletResponse) {

    // ** SECURITY WARNING - FOR REAL PRODUCTION CODE, ADD AUTHENTICATION CHECKS HERE!

    // Trivial servlet response to test that the servlet responds
    response.ContentType = "text/plain"
    response.setStatus(HttpServletResponse.SC_OK)
    response.Writer.append("I am the page " + req.PathInfo)
  }
}

For PolicyCenter to provide access to the servlet, the PolicyCenter/configuration/config/servlet/servlets.xml file contains the following information.

<servlets 
  xmlns="http://guidewire.com/servlet">
  <servlet 
    class="mycompany.test.servlets.TestingServlet"/>
</servlets>

See also