Tomcat Servlet: - The requested resource is not available - eclipse

I am new to writing a Java Servlet, and am struggling to get a simple servlet to work properly.
The Servlet Test1.java class is:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
#WebServlet("/Test1")
public class Test1 extends HttpServlet {
public Test1() {
super();
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
The problem is when trying to run this simple servlet the servers shows an error which is :
message : /Projet1/Test
HTTP 404 : the requested resource is not available
Projet1 is the name of the project
for web.xml i"ve removed the declaration of this servelt as i saw in many question , which says that we cant not use the annotation in the servelt and in web.xml at the same time , anyway this is 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>Projet1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
for the structure and the results : Structure du projet
the result : the result after run
I am running Tomcat v7.0, and have already read similar questions, with responses referring to changing the invoker servlet-mapping section in web.xml ,
i've tried so many solutions pas no changes ! Help please .

Related

question about rest-api error or my mistake

I'm just a newbie in java, rest-api; so i'm trying to build a simple restful-api project used java, eclipse, and wildfly 10. The project was deployed but i cannot call the API via browser by URL. Do i have mistake or configuration has not been set ? Someone help me please, many thanks.
i am not good at english as well, so apologize if it's hard to understand
package it_kickstart;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/rest")
public class KickStartService {
#GET
#Path("/html")
#Produces("text/html")
public String getHTML() {
return "<p>JAX-WS with JBoss WildFly 10.x</p>";
}
#GET
#Path("/text")
#Produces("text/plain")
public String getText() {
return "JAX-WS with JBoss WildFly 10.x";
}
}
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>Rest_API_1</display-name>
</web-app>
URL used to call : http://localhost:8080/Rest_API_1/rest/html

HTTP 404 error. The requested resource is not available. I am using Jdk 1.8 and eclipse jee mars [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
I am putting my code where I am facing problem.
WriteToPdf1.java
import java.io.*;
import javax.servlet.http.*;
import com.darwinsys.spdf.PDF;
import com.darwinsys.spdf.Page;
import com.darwinsys.spdf.Text;
import com.darwinsys.spdf.MoveTo;
public class ServletPDF1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename='CreatePDFFile.pdf'");
PDF p = new PDF(out);
Page p1 = new Page(p);
p1.add(new MoveTo(p, 200, 700));
p1.add(new Text(p, "Hi This is created PDF file by using Servlet"));
p1.add(new Text(p, "by us...Bhumesh Patel...."));
p.add(p1);
p.setAuthor("Bhumesh Patel....");
p.writePDF();
}
}
index.html
CLICK HERE
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>WriteToPdf1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletPdf1</servlet-name>
<servlet-class>ServletPdf1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletPdf1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
</web-app>
Please help I am doing this in eclipse jee mars and tried several options but could not execute it.I have attached all code. plz help.
If you changed the url mapping of a servlet in your web.xml file during runtime of the server, (depending on your server) it will not pick up on the changes. Try cleaning, rebuilding and restarting the server.

Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer

Hi Trying to write a simple rest webservice. And following the tutorial below:
http://www.tutorialspoint.com/restful/index.htm
But i am getting
HTTP Status 500 - Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer
Exception:
<body>
<h1>HTTP Status 500 - Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer</h1>
<div class="line"></div>
<p>
<b>type</b> Exception report
</p>
<p>
<b>message</b>
<u>Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer</u>
</p>
<p>
<b>description</b>
<u>The server encountered an internal error that prevented it from fulfilling this request.</u>
</p>
<p>
<b>exception</b>
</p>
<pre>javax.servlet.ServletException: Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
</pre>
<p>
<b>root cause</b>
</p>
<pre>java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
Following is the code (Which is mostly same as the tutorial)
package com.abhi;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
#GET
#Path("/users")
#Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>MyRestPrjct</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</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.abhi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
I was just not placing the jars in proper way. Instead of placing the required jar in webapps\MyRestPrjct\WEB-INF\lib, I was placing it in webapps\MyRestPrjct\WEB-INF\lib\libs.
It is working fine after I the moved the jars from libs to lib.
Placing all the jars in WEB-INF/lib folder resolved the issue for me.
i too faced similar issue when i did the same tutorial, the problem i found out was the servelt class specified in the web.xml is
org.glassfish.jersey.servlet.ServletContainer
but when i checked in the jars there is no package in the libraries with the above path so i change it to the existing one
com.sun.jersey.spi.container.servlet.ServletContainer
and it solved the issue.
I too got the same exception when trying to run the same module from tutorialspoint. All I did is to download the below jars and added to project. It worked fine.
jersey-common-2.0-m03.jar
jersey-server-2.0-m03.jar
jersey-container-servlet-core-2.28.jar
In my case worked by adding maven dependencies in deployment assembly.(For Maven Project*)
Right click project name (in eclipse) > configure build path > Deployment Assembly (in Left bar) > Add > java build Path Entries > Maven Dependencies > Finish

Getting error while servlet program HTTP Status 404

I am new to Servlets. Anyhow, My Server runs just fine, I can access my index.html and everything is working but when I try to run my servlet on the server it gives me 404 error
I tried tp restart server, edit the web.xml file but no luck, there's also some older questions on SO for the same question, but all different from my case. Tried urlPatters{"/SimpleServlet"} also no luck..
Running on a Linux machine with Eclipse Luna and tomcat7.
Here's my servlet
package com.bgt.zaa;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SimpleServlet
*/
#WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello World");
}
}
and here's my 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>SinmpleServletProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
The error:
Seems a typo mistake in your URL.
try this
127.0.0.1:8080/SimpleServletProject/SimpleServlet
instead of
127.0.0.1:8080/SinmpleServletProject/SimpleServlet
your 404 URL has SinmPleServletProject. An extra n, I guess.
HTTP 404 means, requested resource is not found. So most probably the Servlet is not started properly (or) the project context root is different.

Simple Servlet with Eclipse Juno and J2EE Preview server

I'm trying to create my first HelloWorld servlet with Eclipse Juno and view it in the J2EE Preview Server.
This is my Servlet class:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorld
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
And this is my web.xml automatically generated by eclipse:
<?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>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>HelloWorld</display-name>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
When I select "Run on Server" > "J2EE Preview" I get this:
Error 404 - Not Found
No context on this server matched or handled this request. Contexts
known to this server are:
HelloWorld(/HelloWorld)
Where am I doing wrong?
In "J2EE Preview" Server, context path is the name of the project. When you start the server, this list all contextpaths available.
For example, if your applications is named "app1", your URL will be "http://localhost:8080/app1/HelloWorld"
I had the same issue, and couldn't make it work.
I created a much simplier application, just a hello world JSP page.
This process is very basic, you create a web dynamic project and create a index.jsp file under the WebContent directory, this should be good enough to start your application by run as -> run in server -> J2EE preview but always get:
404 not found
No context on this server matched or handled this request.
Contexts known to this server are:
test(/test)
I have heard eclipse juno is not as stable as indigo, i just downloaded the indigo Java EE version did exacly the same and it worked properly no issues.
Edit:
I Forgot to mention that you can download another Application Server such as JBoss or Glassfish and try to run on your application on them, that should fix your problem.
You can try to fix this issue by deleting all the files and folders into your workspace, delete the .metadata folder and all the content of it, the start eclipse and try again this may works.
Hope this can help you.
Regards!