Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use a servlet in my grails app?

Tags:

grails

I need to use some connectors which are actually servlets. How can I do this in Grails and what about the web.xml? How do I configure the url of the servlet?

I actually have a Spring application here and I am trying to convert it into a partial Grails app. I have a connector servlet in the spring-app, which I wish to use here but the mapping is a must to call the servlet in the gsp file. How can I do this? I basically need to know where the xml file is in case of Grails.

like image 673
Rajeev A N Avatar asked Mar 29 '12 11:03

Rajeev A N


2 Answers

To get the web.xml file, you can run:

grails install-templates

Then, the file can be found in:

<yourapp>/src/templates/war/web.xml

Edit this as usual to add <servlet> and <servlet-mapping> sections, then put your servlet code in:

<yourapp>src/java/your/package/structure/WhateverServlet.java

and you should be good to go

like image 82
tim_yates Avatar answered Sep 21 '22 09:09

tim_yates


If you are within a grails-plugin, then you have a defined place within your *GrailsPlugin.groovy, where to do such things. E.g. Look at the auto generated closure:

def doWithWebDescriptor = { xml ->
   []
}

In here you can add your custom servlet configurations:

    def servlets = xml.'servlet'
    servlets[servlets.size() - 1] + {
        servlet {
            'servlet-name'('yourName')
            'servlet-class'('yourpackage.YourClass')
        }
    }

    def mappings = xml.'servlet-mapping'
    mappings[mappings.size() - 1] + {
        'servlet-mapping' {
            'servlet-name'('yourName')
            'url-pattern'('/yourPattern/*')
        }
    }
like image 33
Chris Avatar answered Sep 20 '22 09:09

Chris