I have a done MVC Java project and I need to make an API for it. I'm trying to use Jersey. I installed it according to this instruction:
http://www.mazong1123.com/use-jersey-2.0-without-maven/
Since my project was already done and it was not a maven. Also, I'm following this instruction: http://www.mazong1123.com/use-jersey-2.0-without-maven/
The thing is I've downloaded and put all.jar files into my project WEB-INF, then I created RESTful Web Services from Patterns (created class within my package where the model, servlet, and .jdbc are. After that BookResource.java and ApplicationConfig.java were created as on the screen:
and edited my web.xml to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" id="WebApp_ID"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>parking-space-booking-system</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PBS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.pbs.web.jdbc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PBS</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
I'm using Tomcat server. Whenever I provide following URL: http://localhost:8084/PBS/webapi/booksresource I'm getting 404. Is it something wrong with my web.xml?
Regards
Related
When trying to configure jax rs in intellij I get this error:
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1945)
My webconfig looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.umbrella.server.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
I manually downloaded the entire jersey pack from the official jersey website. Which should contain all the dependencies.
The weird thing is that whenever I create a class that derives from the ServletContainer it doesn't give any errors.
I found the anwser with the help of Eugen Covaci.
In the Intellij project settings there is a tab called problems. When clicking solve on the problems listed there it started working again.
Another issue was that the jar files were located in web/WEB_INF/libs/api instead of the correct folder (web/WEB_INF/lib)
can someone tell me how to setup my project structure and my web.xml?
In my dynamic web project in the java resources folder i have a servlet (class name: RestFulService) which delivers a JSON file.
The web pages are in the WebContent-folder, for example index.jsp.
The project is published with a glassfish server.
This is my current 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>RestFulService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javasrc, com.jersey.jaxb, com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestFulService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
The link to access the JSON works well (http://localhost:8080/BCode/liveHelp/getText) but if i want to access the index.jsp via "http://localhost:8080/BCode/index.jsp" all i get is an error 404 page.
Can anyone please tell me where the problem is an what i have to change to get it working?
I guess the problem is in the web.xml but I don't have any idea what to change.
I want to call a Servlet as a very first file to execute like welcome file.
In this servlet I am retrieving data from database and rendering it to display page at a very first page.
what I need is when I run program
either
url should be-http://localhost:8083/projectName/servletUrl
not http://localhost:8083/projectName/
or
if url is http://localhost:8083/projectName this should hit my servlet(/servletUrl) not welcome file.
Edit this file WebContent->WEB-INF->lib->web.xml.
It will only be visible if you have ticked the Generate web.xml deployment descriptor while creating the project.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>Database_Conn</display-name>
<welcome-file-list>
<welcome-file>ServletURLpattern</welcome-file>
</welcome-file-list>
</web-app>
Configure your servlet URLpattern as <welcome-file> in web.xml file located in WEB-INF folder of webapp like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ProjectName</display-name>
<welcome-file-list>
<welcome-file>ServletURLpattern</welcome-file>
</welcome-file-list>
</web-app>
Assuming you use eclipse as IDE and servlet version 3 or 3.1 than you have to create web.xml manually.
I have used such servlet mapping:
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The key part is URL pattern that matches all possible URLs unless you add another servlet-mapping for other servlets.
<servlet>
<servlet-name>PenServlet</servlet-name>
<servlet-class>com.sun.PenServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
Here load-on-startup is an attribute of web.xml that will loaded first
if it has a lowest Integer Number.for example if you have 3 servlet that
is mentioned in the web.xml like
<servlet>
<servlet-name>PenServlet1</servlet-name>
<servlet-class>com.sun.PenServlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>PenServlet0</servlet-name>
<servlet-class>com.sun.PenServlet0</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>PenServlet2</servlet-name>
<servlet-class>com.sun.PenServlet2</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Here then load-on-startup 0 is loaded first in your web application
then 1 and 2 and so on..
you need to use this to get data and set it to your first page or return
your page from servlet with loaded data.
I am developing a website and i am finding hosting issues. I have put all my .jsp files in the root directory.
I am able to see the JSPs (accessing directly to them).
But, when i try to perform the same check the WEB-INF/classes, I get the error:
Not Found The requested URL /AC/SearchController was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This is my Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AC</display-name>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdvisorProfileServlet</servlet-name>
<servlet-class>org.AC.controller.AdvisorProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorProfileServlet</servlet-name>
<url-pattern>/AC/AdvisorProfilePage?aId=*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<servlet-class>org.AC.controller.ForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<url-pattern>/ForgotPasswordAdvisor</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<servlet-class>org.AC.controller.AdvisorMyAccountRequestController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<url-pattern>/AC/requests.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<servlet-class>org.AC.controller.UserForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<url-pattern>/ForgotPasswordUser</url-pattern>
</servlet-mapping>
</web-app>
This might also be a server config, because I created the project on Tomcat server 8, but I guess the server which is now hosting it is Tomcat 7.
How can I resolve this?
Please declare "/AC/SearchController" as url pattern in your web.xml
I tried a lot of differents configurations following some other posts, but anything worked. I'm trying to run this simple tutorial(http://www.vogella.com/tutorials/REST/article.html) but i'm still having the message: "The requested resource is not available". Anyone?
Running on Eclipse Kepler, Tomcat 7 and MAC OS 10.8.5.
I tried this configuration for the web.xml, but didn't work too (reference: Running Jersey project (Rest web service) to tomcat)
<?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>Hello</display-name>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
and those are the jars in the last try:
asm-3.3.1.jar
jersey-bundle-1.12.jar
jersey-server-1.12.jar
jersey-servlet-1.12.jar
jsr311-api-1.0.jar
tried the following urls:
http://localhost:8080/Hello/
http://localhost:8080/de.vogella.jersey.first/rest/hello
You mapped the rest request to the path <url-pattern>/rest/*</url-pattern> so your request should be something like http://localhost:8080/rest/Hello/