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.
Related
I am new to spring and currently struggling in understanding the settings required for using an existing spring project in my current project
I have a spring project which has all the service for communicating with the database and webservice.
I am writing a new application which neeeds to talk to the database and webservice.
I thought of re-using the existing implementation in the other project.
I included the project in the build path of the current project.
However I am getting dependency injection error.
Now I am kind of stuck to see what all the other files I need to have in my current project settings so that I can re-use the existing project.
Should I need to import the context file of the other project in to my current one? If so can someone point me to the documentation where I can refer for some guidance?
I'll give it a shot. Hope I can bring about clarity to your question... =D
When you say "I included the project in the build path of the current project.", I assumed you added a spring web project as a dependency to another spring web project. In summary, you can't add a spring web project as a dependency of another spring web project. Let's take some time to understand how you wire a bean to do injection in spring container first...
When you use spring framework, you will find a similar code in your web.xml like the following:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
What you have just done is to use spring framework's servlet to handle every request URL that ends with .htm. Of course you may configure it otherwise.
You should also have another xml that contains all your bean tags. By default, it is spring-servlet.xml. In this spring-servlet.xml, you specify which bean shall be injected to which bean if you are going the xml configuration approach. If you are using annotation approach, you should have a tag to scan all your existing #Component (and its derived annotations) annotated classes, then use #Inject or #Autowired annotation to inject the declared instance(bean). For example:
<context:component-scan
base-package="org.companyname.webappname" />
<context:annotation-config />
So far, what I have been explaining is how to declare a bean in a spring container. An "injection" happens only when you declare a bean (to live in spring container when web server starts) in a spring container, and specify which class it should be injected into.
So now that you have two spring web projects (assumption), with two web.xml and two spring container, you will have a problem getting the base-project's spring container to find the dependent spring web project's packages/classes. I suggest you move all the classes in the dependent project into the base-project and only have one spring container (one web.xml).
Another way is to create a Java Library Project, place all your services, DAO and web services classes in it and build it as jar. In your base project, add the jar file (as dependencies) and either do a component-scan on the jar's package or declare it in xml with the jar's package. Either way, you should only have one spring container, one web.xml.
Hope I am not confusing you.
You got to understand how the IoC container work first. Here is where you need to read and digest how spring IoC works: http://docs.spring.io/spring/docs/3.0.x/reference/beans.html
Update: if you are doing a non web spring project, the concept of having only one spring container still stands. The solution of moving dependent project's classes to a jar file is still valid. Except that you don't configure spring via web.xml. =D
You need to import the file containing the bean definitions of the service layer(say, service-context.xml) into the new project.
It can be done as:
<import resource="classpath:service-context.xml"/>
It depends on how you have setup the project.
In general you would need to use
An example of this can be seen here (see the directions on how to include the project in your own application)
I understand that Servlet 3.0's enhancements have made it possible to display a .jsp from a .jar, based on Can I serve JSPs from inside a JAR in lib, or is there a workaround?
However, I don't seem to be able to connect my View (jsp in jar WEB-INF/lib Tomcat 7 and classic spring MVC context configuration in a War) with the Model and the Controller of my Web App.
Is there a good way to share the dispatcher Servlet, or perhaps create a CustomViewResolver which could scan .jsps included in external JARs, and actually plug my jar into a unique spring context?
With JSP you have the problem of compilation.
So you at least need to precompile them, to have them included. Then it should be possible, since after compilation a JSP is basically a Servlet.
If you would use another view technology like Velocity, Freemarker or JSF based on Facelets, you can very easily use a classpath based ViewResolver.
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.
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
The following Guice Servlet configuration doesn't work when I remove the Maven dependency to the Servlet API:
filter("/*").through(TransactionFilter.class);
The compiler tells me:
cannot access javax.servlet.Filter
class file for javax.servlet.Filter not found
filter("/*").through(TransactionFilter.class);
Any idea?
What do you expect to happen when you remove the Servlet API dependency? Guice Servlet depends on the Servlet API.
Clarification edit: Guice Servlet has a provided scope dependency on the Servlet API so that the jar for it isn't pulled in to the final artifact when building an application... the API classes are provided by the application server at runtime. Thus, you must declare a dependency on the Servlet API in your application's POM yourself (preferably in provided scope as well) in order to use it. This really makes the most sense, though, since you have to declare Servlets and Filters for your application regardless of whether you're using Guice Servlet or not.