Eclipse Maven Web Project Servlet Mappings - eclipse

I have set up my maven project using the m2e plugin in eclipse indigo, and transformed it to an eclipse dynamic web project using mvn eclipse:eclipse -Dwtpversion=1.5. I have managed to get the project up and running in tomcat7, except for my servlets, for which I cannot create the servlet mappings.
I have tried modifying the web.xml file but it throws a ClassNotFoundException. Directory Structure and web.xml :
(ROOT)
+src
+main
+resources
+DrawInitialMap.java
+webapp
(WebContent here)
<web-app>
<servlet>
<servlet-name>DrawInitialMap</servlet-name>
<servlet-class>(groupId).(artifactId).src.main.resources.DrawInitialMap</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DrawInitialMap</servlet-name>
<url-pattern>/drawInitialMap.do</url-pattern>
</servlet-mapping>
(...)
</web-app>
While the #WebServlet annotation also fails to map the servlet :
#WebServlet(name="drawInitialMap", description="visualizes ttrp on html5 canvas", urlPatterns={"/drawInitialMap.do"})
Thank you in advance, and notify if you need any more of the code.
PS : Keep in mind that the servlet worked perfectly in Dynamic Web Project mode, without Maven

There are several issues.
You should stop using eclipse:eclipse. Instead, install WTP integration for M2E from Eclipse Marketplace
In Maven project, your DrawInitialMap should be in /src/main/classes folder. So, it will be compiled as per default Maven project conventions
The servlet-class element in web.xml requires full class name, i.e. no things like (groupId).(artifactId).src.main.resources.

Related

Maven web project servlet class not found

Just setup an eclipse project using Maven webapp architype. I created a servlet file and that got added to the resources folder under src/main/
When I run the app tomcat gives an error:
Error instantiating servlet class com.ABCompany.Demo.SampleDemo. java.lang.ClassNotFoundException: com.ABCompany.Demo.SampleDemo
The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java
My web.xml is below.
<web-app>
<display-name>Demo Example</display-name>
<servlet>
<servlet-name>SampleDemo</servlet-name>
<display-name>SampleDemo</display-name>
<description></description>
<servlet-class>com.ABCompany.Demo.SampleDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleDemo</servlet-name>
<url-pattern>/SampleDemo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Reading in the question:
The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java
Java source files, should go under src/main/java
src/main/java/com/ABCompany/Demo/SampleDemo.java
Other files, like properties, HTML/CSS files, images, templates, etc. Are normally placed in the src/main/resources. As they do not need to be compiled, just copied over into the build directory. So they can be used for running the application, or for packaging (creating a jar).
TIP: Package names, should not contains capitals, by confention. So prevered is com.abcompany.demo.
TIP: Also read How to create a Web Application Project with Maven which uses a Maven archetype, for setting it all up!
I had the same problem.
My solution is:
When the project is created please create the java folder under src/main manually, it is because the maven-archetype-webapp doesn't create the src/main/java by default.
Before: src/main
After: src/main/java
image:java folder manually created
When the first step is ready then you can create all the servlets that you need, please make sure that you create all your classes and servlets under src/main/java.
The problem is because the servlet was created under src/main/resources that means that all your .java files was packaged as a resource and not as .class compiled.

nullPointerException when deploy webLogic 12c application

when i deploy my first web app on webLogic i get this error:
weblogic.application.ModuleException: java.lang.NullPointerException
at weblogic.application.internal.ExtensibleModuleWrapper.prepare(ExtensibleModuleWrapper.java:114)
at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:100)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:172)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:167)
at weblogic.application.utils.StateMachineDriver$ParallelChange.run(StateMachineDriver.java:80)
Truncated. see log file for complete stacktrace
Caused By: java.lang.NullPointerException
at weblogic.servlet.internal.WebAnnotationProcessor.processMultipartConfigAnnotation(WebAnnotationProcessor.java:286)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotationForClasses(AnnotationProcessingManager.java:169)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotations(AnnotationProcessingManager.java:114)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotationsOutsideWebFragment(AnnotationProcessingManager.java:141)
at weblogic.servlet.internal.AnnotationProcessingManager.processAnnotations(AnnotationProcessingManager.java:102)
Truncated. see log file for complete stacktrace
I had this problem and resolved it by removing JSF 2.2 Mojarra 2.2.0 from the application libraries. It only needs to be in the classpath.
In Eclipse, edit properties for the project, go to Project Facets-->Java Server Faces and uncheck "Include libraries with this application" and Apply change.
I deploy from Eclipse (OEPE), same problem here without JSF. Here is what solved my problem:
Quit Eclipse
Delete the workspace's .metadata folder
Launch Eclipse
The same solution worked on NetBeans8. From projects, go to your war project, right click over Libraries > JSF2.2 and click Remove. Rebuild the project and deploy. Remember to deploy as shared library the JSF on WLS.
This seems to be a duplicate of Maven generated ear file fails during deploying weblogic server. Some details about the solution can be found in this post https://roundwheeltech.wordpress.com/2016/06/15/deploying-a-jsf-2-2-to-weblogic-12-1-3/.
Because WebLogic 12.1.3 ships with JSF 2.1, the required JSF jars have to be bundles within the WAR file (specified as dependencies in pom.xml). WebLogic must also be instructed to use the libraries packaged in the WAR file instead of the ones that are provided by the container.
I had the exact same NPE. It was started by an instantiation of an factory that went wrong. I had to remote debug in to find this. An exception in the initialisation of the factory is swallowed and we don't get the real exception.
In my case the problem was antlr.Token and antlr.CommonToken not arriving from the same source. This gave me an ClassCastException. I solved it by moving all libraries from the ear to war to get more control over them.
Edit the file web.xml and put:
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

Spring context:component-scan fails to find components in another Eclipse project dependency referenced in POM

I am working on a GWT web application split across two Eclipse Projects (myclient & myservice).
The myclient project references the myservice project via a dependency in the POM.
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myservices</artifactId>
<version>1.0.0</version>
</dependency>
The myclient project has a WAR directory src/main/webapp. The output folder for the myclient project is src/main/webapp/WEB-INF/classes.
The myclient project has a Spring descriptor application-context.xml with the following
<context:component-scan base-package="com.myproject.myclient, com.myproject.myservices"/>
and the web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
I have several files in the myservices project annotated as spring #Component, #Service, #Configuration but these are not picked up by the component scan when I run the GWT application in Eclipse. As a test I experimented with placing an #Component in the myclient project and this was successfully created.
I believe the following log entry during application startup indicates the source of the problem
org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:com/myproject/myservices/**/*.class] to resources []
the location pattern for the myclient project resolves to all the resources on the classpath but for myservices no resources are found.
I experimented with building the myservices project JAR and placing this JAR into the src/main/webapp/WEB-INF/lib folder of the myclient project. When I do this the component scanning works. However for development I don't want to have to build and copy a JAR everytime I make changes to the myservices project. I imagine that the component scanning should work on a project referenced through the POM without having to the build that project but after much experimenting I have been unable to get this working.
Be sure that in the deployment assembly (right click your web project and select "deployment assembly" of your myclient project it is configured to deploy the jar that is outputted by the myservices project. If you are using maven, the m2e, m2e-wtp project configurators should do this deployment assembly setup automatically.
Once you have deployment assembly settings properly configured, now when you deploy a project to your server using the Eclispe server adapter publish mechanism, everything should get deployed and the myservices jar would get placed in the right spot for your myclient project.
But make sure you the latest version of m2e-wtp installed. This way your configuration in your pom.xml and deployment assembly will get correctly configured.
Try splitting your application-context.xml into 2 separate files:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:service-context.xml,classpath:client-context.xml</param-value>
</context-param>
myservices/src/main/resources/service-context.xml:
<context:component-scan base-package="com.myproject.myservices"/>
myclient/src/main/resources/client-context.xml:
<context:component-scan base-package="com.myproject.myclient"/>
Try this :
<context:component-scan base-package="com.myproject"/>
Please check the following things:
Is your serviceproject present "only" as jar or is the whole project available via workspace resolution. This can be checked via maven-context-menue (Disable/Enable Workspaceresolution)
The layout of the webapp under src/main/webapp is okay. BUT do i get you right that all classes are copied there? If so, you should make sure everything is under target directory. So please check whether a maven call "clean package" generates a webappstructure under the target folder and all required libs (e.g. myservice) exist under target\$your-webarchivename\WEB-INF\lib
Check that packaging in myservice pom.xml is set to jar (you probably have this, right?)
it's obvious that your service jar is not included in your client project's build path. this is the only root cause.
Make sure 3 points:
you have run mvn clean install under your service project which has correct pom.xml.
you have run mvn eclipse:eclipse under your client project. this will pull out all your dependency project.
check your client eclipse project's build path dialog. is there your service jar in the list? Make sure this
You better once look this tutorial
http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html

How to enable compile-time aspectj weaving for Eclipse embedded Tomcat

I'm having a problem trying to make eclipse and aspectj work for Dynamic Web Projects. I'm looking for compile time weaving so I can use the Eclipse Visualisation features.
I've followed the steps given here:
https://forums.oracle.com/forums/thread.jspa?messageID=8591748
using Eclipse Indigo (3.7) with the latest Aspectj eclipse plugin (2.1.3).
The steps were as follows:
[1] Create basic servlet
//imports omitted
public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
PrintWriter out= null;
try {
out = response.getWriter();
out.write("hello from MyServlet");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(out!=null)
out.close();
}
}
}
[2] Add servlet to deployment discriptor (web.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.myCompany.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet/*</url-pattern>
</servlet-mapping>
</web-app>
[3] Create aspect
package com.myCompany;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public aspect MyServletAspect {
pointcut doGet() :
execution(public void MyServlet.doGet(HttpServletRequest,HttpServletResponse));
after() returning : doGet() {
System.out.println("Hello from doGet (via aspectj)");
}
}
However when I run this no dice - the aspect just doesn't run (servlet writes to response, aspect to console). I did something similar for a regular java project and that works fine.
I note there are guidelines for adding aop.xml into the web app's META-INF directory, however this didn't work for me.
Our goal is to run aspectj non-invasively in development via eclipse for a non-Spring framework (or Maven) project - this should be easy.. but I haven't been able to make it work.
Any suggestions / reference to tutorial for compile-time weaving for web apps in eclipse would be useful. The app server is embedded Tomcat 6 (but can be upgraded to tomcat 7 if required).
The ability to tweak the development environment at runtime without impacting the production code would be great - if it can be made to work.
Responses much appreciated.
I recommend you just download and use Spring STS (Spring's Eclipse) and download/create a Spring Roo project.
Your just going to use the Roo project to boostrap your own project with the correct AspectJ libraries. That is you'll just use its pom file that it generates. You can try to use plain Eclipse and download all the plugins (which is what I do) but its PITA to get everything setup correctly.
The key thing is to get the AspectJ compiler to run instead of the regular Java compiler. This requires a special Maven plugin or Ant plugin. Also you do not need the aop.xml file.
If your using Eclipse you need to make sure that the AspectJ nature is added to the project (usually you right click on the project and select "add natures" "Or convert to...".)
You'll also in Eclipse need to add the Spring Aspects jar to the "Aspect Libraries" which is not the classpath/buildpath.
To use compile time weaving in maven, you need to use the aspectj compiler plugin. See this:
http://maven.apache.org/maven-1.x/plugins/aspectj/
To get your project working in Eclipse, you need to install the AspectJ project configurator for m2eclipse (assuming you are using m2eclipse). You can install it from going to Preferences -> Maven -> Discovery. Open the catalog and look for the AJDT configurator.

Why Jetty has not found CometServerServlet from Rocket package?

I have a common GWT application that is using (trying to) Rocket-Framework to implement a CometServerServlet.
After I have registered my servlet in web.xml and my app.gwt.xml, like this:
App.gwt.xml:
<servlet path="/server" class="myapp.server.MyCometServlet"/>
web.xml
<servlet>
<servlet-name>myCometServlet</servlet-name>
<servlet-class>myapp.server.MyCometServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myCometServlet</servlet-name>
<url-pattern>/myapp/server</url-pattern>
</servlet-mapping>
Jetty apparently doesn't find the CometServerServlet from which my MyCometServlet extends:
WARNING: Error starting handlers
java.lang.NoClassDefFoundError:
rocket/remoting/server/comet/CometServerServlet
Caused by:
java.lang.ClassNotFoundException:
rocket.remoting.server.comet.CometServerServlet
That is strange, because I've added the Rocket jar to my project.
Could someone explain why this error happened?
I could understand if I "registered" the servlet in a wrong way or something, but at what point Jetty (or who?) looks for the CometServerServlet and fails at finding it?
Adding the jar to your project (e.g. in some IDE) will remove compilation errors, because the jar can be found at compile time.
But Jetty needs the library at runtime, and the usual way to provide it, is by putting it in the WEB-INF/lib folder of your project.
Of course, make sure to redeploy the app afterwards (e.g. if you're using Dev Mode, click refresh in the "Development Mode" view).