Eclipse JSP in dependency project without Maven - eclipse

I have the following setup:
Project A - A Dynamic Web project, which depends on project B.
Project B - A Dynamic Web project, that defines a Test.jsp file.
If I launch project B on the server, or move the Test.jsp to project A and launch project A on the server. it works just fine, and I can access the .../Test page'.
But when the Test.jsp remains in project B and I launch project A, although I do see the project-b.jar in the war file and the classes from project B does load, which means most of the process works ok, and only the jsps are not added...
How can I solve this?

In Case anyone would get to this question, the solution described in here works well, though was not very clear at first:
In my project B, place your jsp files so:
src/main/webapp/META-INF/resources/${path-to-jsp}/file.jsp
In src/main/webapp/META-INF/ create a web-fragment.xml with your variation of the following content:
<web-fragment
metadata-complete="true"
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<servlet>
<servlet-name>jspTest</servlet-name>
<jsp-file>/${path-to-jsp}/file.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>jspTest</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-fragment>
and now if your buildpath is defined correctly, then clean-build both project B, and project A and run the server again... it works!

Related

No project can be run from eclipse on Tomcat

When trying to create Dynamic Web project or even using old one( which used to work before) and running it on the tomcat(v8.0.35) server from my eclipse (mars.2 v4.5.2) I started getting Http status code 404 when accessing the recourse:
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>hello</display-name>
<servlet>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.javawebtutor.controller.LoginServlet</servlet-class>
</servlet>
<servlet>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.javawebtutor.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
For reference, I have done everything step by step in this tutorial:
http://www.javawebtutor.com/articles/hibernate/mvc-appliction-using-servlet-jsp-and-hibernate.html
Java I'm using for the project is jdk1.8. For server - jre1.8
I'm 99% sure I've not deviated from the tutorial and something wrong must be with my Tomcat installation. As I mentioned it worked before on other projects I have not modified and it stopped working as well.
I have tried cleaning projects, cleaning Tomcat server, work directory, remove tomcat server, restart eclipse, add tomcat server again. I've even converted project into Maven project and tried to build with clean and install goals. Nothing helped.
Here is server location snippet:
Related answer here: Tomcat is not deploying my web project from Eclipse
And yes, I did try everything in there.
If it helps I'm developing on Win10
EDIT:
Project topology:
With the limited amount of info, I suggest you do the following:
Check if you have a login.jsp directly under your webapp or webContent(your tutorial uses this), because your welcome file in web.xml specifies this as your home page.
Try access http://localhost:8080/ instead of http://localhost:8080/hello/
Please provide your complete expanded project tree if the above does not solve your problem.
Also, here is one of my youtube video introducing how to set up a running java-web app with eclipse and tomcat. It is only a couple of minutes.
Following this tutorial makes sure you have a minimal java web project running, you can start from there if your problem cannot be fixed.

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.

eclipse webapp welcome file display error

I m new to eclipse and facing difficulties at the beginning of a new webapp.
index.jsp doesnt display.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PizzaOrderApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I already tryed with http://localhost:8080/PizzaOrderApp/index.jsp and http://localhost:8080/PizzaOrderApp/jsp/index.jsp
after remove-readd tomcat server:
Have you tried putting the .jsp file directly in the WebContent folder rather than creating a folder for the JSP pages?
You did not define the Spring dispatcher servlet in web.xml. You can either copy index.jsp outside of WEB-INF directory (into webcontent), or you should have a deep look into the Spring MVC documentation.
I also faced this problem but what i was doing wrong is, I created
Dynamic project in eclipse with Dynamic Web Module version 3.0. It
won't give you Web.xml file.I added Web.xml file from other project
but still Welcome File is not getting.
Then i deleted the project and i created the project with Dynamic Web
Module Version 2.4 and it given me Web.xml file. I again Configure the
Welcome File in the Web.xml file, After that its working fine.Server
which i am using is Tomcat 7 and java 1.7,Eclipse Luna.

Eclipse not deploying my web app properly

I am trying to create a base Spring project with Maven on Eclipse Kepler (with m2eclipse installed). No errors are reported by Eclipse but when I hit Run on Server (or run Maven Install) only the original files from META-INF and WEB-INF files are deployed. This does not include my dispatcher-servlet.xml config file thus causing a FileNotFoundException:
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
How can I fix this?
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And this is a screencap of my project's folder:
As you can see only selected files are shown in Deployed resources.
Since it is a maven project .
you might have converted maven project to dynamic web project I guess.
Follow the steps
Go to project properties (right click at last click peoperties)
click/select Deployment assembly at the left side
the root(/) deploy path may be pointing to src/main/webapp , select this and click remove button
click on add button , select a directive type folder, select your WebContent folder
Now try to clean and run your application on server.
Solution 2
Actually you need to copy/put all your WebContent files and folder to src/main/webapp
and restart your server.
Note that the maven dynamic web project it always look in to src/main/webapp by default.
Hope it helps
Add <sourceDirectory>src</sourceDirectory> in the <build> block.
Then Run Maven with "clean install" goal.
And refresh your project.
Now check your target folder. You should see war file or * extension file.
Hope it will solve

Eclipse Maven Web Project Servlet Mappings

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.