Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Jetty 9 embedded Servlet 3.0 annotations without war file

Have a read of this page, which describes the important distinctions
between discovered annotations, introspected annotations and
ServletContainerInitializers:
http://www.eclipse.org/jetty/documentation/current/using-annotations.html

There is also a worked example on how to use annotations in an
embedded scenario. The example code shown is for a webapp packaged as
a war (and of course you can skip setup of jndi resources, login
services etc if you don't need them).  Your configuration will change
if you aren't deploying an assembled war. In that case, you need to
figure out if you're trying to support discovered annotations and
ServletContainerInitializers. If so, then these are on the classpath
of the executing embedding code, and considered as the equivalent of
the container classpath. You just need to figure out a pattern for the
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern" context
attribute value that will match the class dirs on the container
classpath.

Others have used annotations with this type of embedding when
createing an executable jar and have managed to find suitable settings
for the ContainerIncludeJarPattern - try googling the jetty lists for
postings about this. I might have also responded on StackOverflow to
questions about this, so try googling that there too.


regards
Jan

On 16 January 2015 at 17:16, Christian Reuschling
<reuschling@xxxxxxxxxxxxxx> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> indeed, I tried to use Johannes code, and it seemed to work well, but @MultipartConfig was not
> recognized. I used WebAppContext instead of ServletContextHandler, but this didn't do the trick:
>
>
>
>         WebAppContext contextHandler = new WebAppContext();
>         contextHandler.setContextPath("/");
>         contextHandler.setResourceBase(".");
>
>         server.setHandler(contextHandler);
>
>         // unnecessary, servlet will be started during annotation scanning
>         // contextHandler.addServlet(new ServletHolder(new ExampleDispatcherServlet()), "/*");
>
>
>         Set<AnnotationParser.Handler> handlers = new HashSet<>();
>         handlers.add(new WebServletAnnotationHandler(contextHandler));
>         handlers.add(new WebFilterAnnotationHandler(contextHandler));
>         handlers.add(new WebListenerAnnotationHandler(contextHandler));
>
>         AnnotationParser annotationParser = new AnnotationParser();
>         annotationParser.parse(handlers, ExampleDispatcherServlet.class.getName(), new
> ClassNameResolver()
>         {
>             @Override
>             public boolean shouldOverride(String name)
>             {
>                 return false;
>             }
>             @Override
>             public boolean isExcluded(String name)
>             {
>                 return false;
>             }
>         });
>
>         server.start();
>
>
> The @WebInitParams were recognized, the servlet was loaded on startup. But no Multipart support.
> What will do the trick? I could manually add Multipart support with this hack:
>
>
> WebAppContext contextHandler = new WebAppContext()
> {
>     @Override
>     public void doHandle(String target, Request baseRequest, HttpServletRequest request,
> HttpServletResponse response) throws IOException, ServletException
>     {
>         String contentType = request.getHeader("Content-Type");
>         if(contentType != null && contentType.startsWith("multipart/form-data"))
>         {
>             baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, new
> MultipartConfigElement(System.getProperty("java.io.tmpdir")));
>         }
>
>         super.doHandle(target, baseRequest, request, response);
>     }
> };
>
>
> but it doesn't feel like a final solution.
>
> by the way - is it planed to scan servlet 3.0 annotations by Jetty inside the addServlet(..)
> methods in the future? It's a gap, isn't it?
>
>
> Christian
>
>
> On 16.01.2015 16:50, Joakim Erdfelt wrote:
>> Traditionally annotation scanning is part of the WebApp layer. Embedded Jetty focuses on direct
>> declarations.
>>
>> Johannes Brodwall's solution is creative, but only addresses a small subset of what, and how,
>> annotations can be wired up. (which might be sufficient in most cases) Examples: * There's no
>> good way to handle @HandlesType and ServletContainerInitializer his way. * There's no support
>> for @MultipartConfig without a WebAppContext.metadata to hold that information * There's no
>> support for @ServletSecurity without a WebAppContext.metadata to hold that information
>>
>> Your example, however, uses @MultipartConfig, which is something that belongs in the
>> WebAppContext's metadata. That information, found during annotation scanning, isn't retained in
>> a ServletContextHandler.
>>
>>
>> -- Joakim Erdfelt <joakim@xxxxxxxxxxx <mailto:joakim@xxxxxxxxxxx>> webtide.com
>> <http://www.webtide.com/> - intalio.com/jetty <http://intalio.com/jetty> Expert advice,
>> services and support from from the Jetty & CometD experts eclipse.org/jetty
>> <http://eclipse.org/jetty/> - cometd.org <http://cometd.org/>
>>
>> On Fri, Jan 16, 2015 at 7:26 AM, Christian Reuschling <reuschling@xxxxxxxxxxxxxx
>> <mailto:reuschling@xxxxxxxxxxxxxx>> wrote:
>>
>> Hi,
>>
>> we have no war file, nor some extra classpath, but simply want to add our servlet object
>> instance to the embedded server, by taking it's servlet 3.0 parameters into account.
>>
>> All the documentation on Jetty, and all examples and snippets we found for embedded + servlet
>> 3.0 annotations deals with specifying a war file or a classpath, which will be scanned by
>> AnnotationConfiguration then.
>>
>> We tried a lot, but we were not able to do the same by simply adding the servlet with
>> context.addServlet().
>>
>> Here is our code:
>>
>>
>> Server server = new Server(iPort);
>>
>> // tried: ServletHandler handler = new ServletHandler(); // tried: WebAppContext contextHandler
>> = new WebAppContext(); ServletContextHandler contextHandler = new ServletContextHandler();
>> contextHandler.setContextPath("/");
>>
>> // tried: contextHandler.setConfigurations(new Configuration[] { //             new
>> AnnotationConfiguration() }); // tried: contextHandler.setConfigurationDiscovered(true);
>>
>> contextHandler.addServlet(new ServletHolder(new ExampleServlet()), "/*");
>> server.setHandler(contextHandler);
>>
>> server.start();
>>
>>
>> The Servlet is a simple HttpServlet with following annotations:
>>
>> @WebServlet(urlPatterns = { "/example/*" }, loadOnStartup = 1, initParams = {
>> @WebInitParam(name = "name1", value = "val1"), @WebInitParam(name = "name2", value = "val2")
>> }) @MultipartConfig(fileSizeThreshold = 1024*1024*10) public class ExampleServlet extends
>> HttpServlet {....}
>>
>>
>>
>> How is it possible that the embedded Jetty server simply takes the annotations of the Servlet
>> Object added to the server with context/handler.addServlet(..) into account, and nothing else,
>> as the most simple scenario.
>>
>>
>> Thanks for all answers!
>>
>> Christian
>>
>>
>>
>> _______________________________________________ jetty-users mailing list
>> jetty-users@xxxxxxxxxxx <mailto:jetty-users@xxxxxxxxxxx> To change your delivery options,
>> retrieve your password, or unsubscribe from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/jetty-users
>>
>>
>>
>>
>> _______________________________________________ jetty-users mailing list
>> jetty-users@xxxxxxxxxxx To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit https://dev.eclipse.org/mailman/listinfo/jetty-users
>>
>
> - --
> ______________________________________________________________________________
> Christian Reuschling, Dipl.-Ing.(BA)
> Software Engineer
>
> Knowledge Management Department
> German Research Center for Artificial Intelligence DFKI GmbH
> Trippstadter Straße 122, D-67663 Kaiserslautern, Germany
>
> Phone: +49.631.20575-1250
> mailto:reuschling@xxxxxxx  http://www.dfki.uni-kl.de/~reuschling/
>
> - ------------Legal Company Information Required by German Law------------------
> Geschäftsführung: Prof. Dr. Dr. h.c. mult. Wolfgang Wahlster (Vorsitzender)
>                   Dr. Walter Olthoff
> Vorsitzender des Aufsichtsrats: Prof. Dr. h.c. Hans A. Aukes
> Amtsgericht Kaiserslautern, HRB 2313=
> ______________________________________________________________________________
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.19 (GNU/Linux)
>
> iEYEARECAAYFAlS5OVwACgkQ6EqMXq+WZg8NRQCghGezR7YmFFRHJIKS1Y31WclA
> 8HUAn0hTuOUMLXj/roGWPg6NNL84+yqJ
> =ru1b
> -----END PGP SIGNATURE-----
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users



-- 
Jan Bartel <janb@xxxxxxxxxxx>
www.webtide.com
'Expert Jetty/CometD developer,production,operations advice'


Back to the top