Loading my ServletContextListener in Tomcat 7 - java

New to Tomcat and running Tomcat 7 in an Eclipse environment.
I followed a tutorial for creating a ServletContextListener class. My question is, simply, how do I get this loaded and running in Tomcat? Can I use annotations? Do I need to edit an xml file?

Javadoc to the rescue:
In order to receive these notification events, the implementation
class must be either declared in the deployment descriptor of the web
application, annotated with WebListener, or registered via one of the
addListener methods defined on ServletContext.

As an example in the web.xml:
<listener>
<listener-class>fully qualified class name of your listener class</listener-class>
</listener>

When you download Tomcat 7.0 from the Apache Tomcat website you'll get a version that includes an examples application. There is source code and configuration for some of the Servlet 3.0 features, like annotations.
Have a look at those examples - they're useful.

Related

How to define the correct startup order of a Java Tomcat application?

In my Java Tomcat application I setup some properties to be used in the whole application. But apparently other functions (eg Hibernate) are already starting before and need some of these properties.
The properties are initialized in the class defined in web.xml with:
<listener>
<listener-class>com.mycode.ApplicationContextListener</listener-class
</listener>
Is there a way (in web.xml?) to define the very first function to be started when Tomcat starts the application? Or another method?
Thanks,
Frank
In my understanding you have some properties you want to access/reference from some loaded classes before your Servlet context is initialised.
In case this is some 3rd party library like Hibernate then they have
their own properties file to use for the reason.
You can use Tomcat's properties during Tomcat init before any 3rd party libraries loaded into the JVM used by Tomcat.
The standard Java Properties file
that is loaded in the JVM during class path init before invoking any Java application from the JVM.
You can use the XML tag "loadOnStartup" as described here, to define which class has to startup first.

java servlet context listener does not start

Under tomcat7, I used both #WebListener on the class and
<listener>
<listener-class>helpers.MyListener</listener-class>
</listener>
in web.xml. It starts on my localhost tomcat and runs fine, but not on tomcat7 on another server. No error message in the logs I added a static initializer to log it when it starts, but there's no logged message, so the listener is just not starting. Any tips?
I think that you are trying to use tomcat 7 for Servlet 3.0 which includes annotations support. I am no servlet expert, I have just started learning servlets 3.0(especially). For annotations I came across this resource called caucho technology. This website helped me a lot to learn annotations. I encountered the same problem as yours.

importing servlets from library in web.xml

I have included a library in my webapp. This library has its own web.xml. When I deploy the project on jetty, I find that the servlets from the library are not loaded. I have to manually add these servlets into the web.xml of my project. Is it possible to include these servlets in my project without me having to enter them all in my web.xml
If additional information regarding my problem is required, I would be happy to provide it.
you have to tell the servlet container that it has to initialize a sevlet. The usually way is to use the deployment descriptor (web.xml). Since JEE6 there is another option with annotations:
#WebServlet(urlPatterns="/forward")
public class ForwardServlet extends HttpServlet {
//code goes here...
}
But you'll have to try it, if it works with servlets inside a referenced library.

Is there any way to do servlet mapping in eclipse IDE other than manually?

Recently,i have started to develop servlets using Eclipse.Every time i write a servlet program,i need to manually map them into web.xml.Is there any way to do automatic mapping of servlets??.Also the Eclipse asks for URL pattern whenever i create a new servlet file.Why is it asking when it doesnt maps into web.xml by itself??Note:also recommend any useful plugin for servlets/jsp development...
Upgrade to Servlet 3.0 (Apache Tomcat 7.0, Glassfish 3, etc), then all you need to do is to add the #WebServlet annotation to the servlet class.
#WebServlet("/foo")
public class FooServlet extends HttpServlet {
// ...
}
That's it.
If you're still sticking to Servlet 2.5 or older, then you need to create the Servlet class as a Servlet class, not as a Java class. Rightclick project, choose New > Servlet and complete the wizard. This way Eclipse will just autogenerate the necessary web.xml mapping.
If you want to use web.xml for servlet mapping then you need to select dynamic web facet 2.5 version instead of 3.0 in dynamic web project

Annotations (#EJB, #Resource, ...) within a RESTful Service

I'm trying to inject a EJB within my RESTful Service (RESTEasy) via Annotations.
public class MyServelet implements MyServeletInterface {
...
#EJB
MyBean mybean;
...
}
Unfortunately there is no compilation or AS error, the variable "mybean" is just null and I get a NullPointerException when I try to use it.
What I'm doing wrong?
Here are some side-informations about my architecture:
JBoss 4.2.2.GA
Java version: 1.5.0_17
local MDB-Project
remote EJB-Project
WAR Project with the RESTful Service which uses the remote EJB and sends messages to the local MDB-Project
Thanks in advance!
br
Dominik
p.s: everything is working fine when I use normal context lookup.
I had a similar problem (though without #Remote beans). The way it worked for me - sample application is here: https://github.com/kubamarchwicki/rest-app/ (this works: https://github.com/kubamarchwicki/rest-app/blob/master/service-webapp/src/main/webapp/WEB-INF/web.xml#L9)
The crack with context lookup is that the name changes with a change of the ear name. If you fancy things like versions, it makes the whole thing hard to trace or forces you to hardcode ear name somewhere in the code.
Just a few cents to an old discussion ;-)
This is not exactly my forte, so maybe I am way off... but, can you do EJB stuff in a WAR? I was under the impression you needed to do EJB work in an EAR.
JBoss 4.2.2.GA is not a fully compliant Java EE 5 server, it does not support EJB references injection in servlets or application clients, only in the EJB layer. Use JBoss 5 for that (or perform a lookup).
JBoss 4.2.2.GA supports only Servlet 2.4. There is no support of DI on Servlet 2.4. Hence you always get 'null' for myBean variable. As suggested, please migrate to JBoss 5.0 which supports Servlet 2.5 which makes use of Java 5 features like annotations.

Categories