This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I have a problem with my simple servlet that I am trying to run, Hello.java. I made it in eclipse, then placed the file it in the webapps/ServletTest/WEB-INF/classes folder and compiled it, creating the file Hello.class in the same folder. I then modified my web.xml file to map the servlet and tried to run it through the following address
http://localhost:8080/ServletTest/Hello
However, this did not work, giving the following error
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.42
The mapping in the web.xml file looks like this:
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Main.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
The code of the servlet:
package Main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String path = request.getContextPath();
String ip = request.getRemoteAddr();
out.print("<html>" +
"<title>Hello</title>" +
"Hello World"+ "<br>" +
"Your ip is: " + ip + "<br>" +
"Your path is: " + path
+ "</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
The compiled Hello.class file should be in the folder
webapps/ServletTest/WEB-INF/classes/Main
since it's declared to be in package Main.
Also, you can see Tomcat's startup logs in /logs/catalina.out or /logs/catalina.log, depending.
Also, Suresh is right in the comments, use either a <servlet> declaration or #WebServlet. Don't use both.
Try to delete web.xml.
Annotation #WebServlet("/Hello") is enough for Tomcat 7+
If the root folder not having proper permission you will face this issue. So please check the root folder property and remove read only permission and add user to full access permission in security tab.
Related
I know that there were similar problems already, but non of the solutions worked for me. I checked the directories and I edited my `web.xml file a couple of times but it still does not work.
I am writing a simple servlet in Java running on Tomcat and I am getting the error:
HTTP Status 404 – Not Found
Type Status Report
Message /WorkshopForm/MainWorkshopForm
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one exists.
My servlet class is:
package workshop;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(description = "This registration form", urlPatterns = {
"/WorkshopForm" })
public class WorkshopForm {
public class MyServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 13425L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String participantName = request.getParameter("participantName");
String participantSurname = request.getParameter("participantSurname");
String participantEmail = request.getParameter("participantEmail");
PrintWriter writer = response.getWriter();
writer.println("Welcome" + participantName + " " + participantSurname + " " + participantEmail);
}
}
}
My web.xml:
Tree in Eclipse:
Is it a problem with the web.xml file? I am thinking that maybe I have some mismatch with names or paths but I tried to solved it already and no idea why it is not working.
The URL pattern /WorkshopForm matches only the exact URL path /WorkshopForm. If you want the servlet to also handle longer paths like /WorkshopForm/MainWorkshopForm, you need to change the URL pattern to /WorkshopForm/*. Then you can call request.getPathInfo() in your servlet code to obtain the variable part of the path.
Another alternative is to use some JAX-RS framework to handle the mapping from URL paths to Java methods that handle individual paths.
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I am new to java, I just tried to read initialization parameters from Deployment Descriptor file (web.xml), But got above error?
My web.xml and java file coding coding in snap attached.
My directrory structure is
c:\....tomcat\webapps\dd\web-inf\classes
No error in java class file.
Java file code which is compiled successfully
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
String fileName;
public void init(ServletConfig config) throws ServletException {
super.init(config);
fileName = config.getInitParameter("logfilename");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(fileName);
out.close();
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
<init-param>
<param-name>logfilename</param-name>
<param-value>value1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/mc11</url-pattern>
</servlet-mapping>
</web-app>
Other detail of my directory and error page i think that my web.xml not working
There are two problems I can see at the moment...
Servlet init parameters
You currently have:
//defining param1
param1
value1
That's not how you define the parameter. You should specify a param-name element containing the name of the parameter, rather than using the name of the parameter as an XML element name.
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
Also note that // isn't how you write comments in XML - if you wanted a comment, you should have:
<!-- Define the first parameter -->
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
(The param-value element should have been a hint - if you could really just specify your own element, I'd have expected <logfilename>value in here</logfilename> - having the name specified as an element name, but the value specified with a fixed element name of param-value would have been an odd scheme.)
Servlet mapping
Currently your mapping is:
<servlet-name> FormServlet</servlet-name>
<url-pattern>/ss/</url-pattern>
</servlet-mapping>
I suspect that mapping won't match http://localhost:8080/dd/ss/s.html because you don't have any wildcards in there - it you may well find that it matches exactly http://localhost:8080/dd/ss/. It's not clear where the dd part comes in, but I assume that's a separate part of your configuration. You should try:
<!-- I would recommend removing the space from the servlet
- name *everywhere*. -->
<servlet-name>FormServlet</servlet-name>
<url-pattern>/ss/*</url-pattern>
</servlet-mapping>
If that doesn't work for http://localhost:8080/dd/ss/s.html, see whether it maps http://localhost:8080/ss/s.html - it may be that your engine isn't configured the way you expect elsewhere.
There is no problem with code,I try net beans tool and done assignment perfectly with the help of above code.Before that may be problem in tomcat.
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
To be honest i am a learner and this is my first ever servlet program.
I made the basic servlet and intalled tomcat version 6 and even tomcat version 8.
the server starts up correctly and i am able to see the tomcat start up page on going to
http://localhost:8080
but after logging to tomcat manager when i click on my folder name it gives me an error saying
http status 404-/online/ (online is my folder created in webapps)
type Status report
message /online/
description The requested resource is not available.
here's my codes
web.xml-> (in folder online->WEB-INF)
- <web-app>
- <servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
</web-app>
FirstServlet.java->
import javax.servlet.*;
import java.io.*;
class FirstServelet implements Servlet
{
public void init(ServletConfig config)
{
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
PrintWriter out;
out=response.getWriter();
out.println("hello");
out.println("<html>");
out.println("<head>");
out.println("<title>MY First Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<marquee>ban ja tar pls</marquee>");
out.println("</body>");
out.println("</html>");
}
public String getServletInfo()
{
return null;
}
public ServletConfig getServletConfig()
{
return null;
}
public void destroy ()
{
}
}
please resolve the 404 error
The problem is you don't welcome-file-list, I think the default page is index.html which I suppose is not there in you folder. You can provide any html or jsp file as default file but NOT a servlet as below.
<welcome-file-list>
<welcome-file>myfile.html</welcome-file>
</welcome-file-list>
You can access your servlet by hitting http://localhost:8080/online/FirstServlet URL.
You can create a default page which will redirect to FirstServlet i.e.
myfile.html
<meta http-equiv="refresh" content="0; url=http://localhost:8080/online/FirstServlet" />
And also what #Braj said in the comment extend HttpServlet instead of implement Servlet.
Edit
You have a typo in servlet name. change the servlet name to FirstServlet from FirstServelet.
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 1 year ago.
I am writing a Java Servlet, and I am struggling to get a simple HelloWorld example to work properly.
The HelloWorld.java class is:
package crunch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
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. This section actually doesn't exist in mine, and when I added it the same problem still occurred.
Try this (if the Java EE V6)
package crunch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
#WebServlet(name="hello",urlPatterns={"/hello"}) // added this line
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
now reach the servlet by http://127.0.0.1:8080/yourapp/hello
where 8080 is default Tomcat port, and yourapp is the context name of your applciation
You definitely need to map your servlet onto some URL. If you use Java EE 6 (that means at least Servlet API 3.0) then you can annotate your servlet like
#WebServlet(name="helloServlet", urlPatterns={"/hello"})
public class HelloWorld extends HttpServlet {
//rest of the class
Then you can just go to the localhost:8080/yourApp/hello and the value should be displayed. In case you can't use Servlet 3.0 API than you need to register this servlet into web.xml file like
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>crunch.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Writing Java servlets is easy if you use Java EE 7
#WebServlet("/hello-world")
public class HelloWorld extends HttpServlet {
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World");
out.flush();
}
}
Since servlet 3.0
The good news is the deployment descriptor is no longer required!
Read the tutorial for Java Servlets.
this is may be due to the thing that you have created your .jsp or the .html file in the WEB-INF instead of the WebContent folder.
Solution: Just replace the files that are there in the WEB-INF folder to the Webcontent folder and try executing the same - You will get the appropriate output
For those stuck with "The requested resource is not available" in Java EE 7 and dynamic web module 3.x, maybe this could help: the "Create Servlet" wizard in Eclipse (tested in Mars) doesn't create the #Path annotation for the servlet class, but I had to include it to access successfuly to the public methods exposed.
You have to user ../../projectName/Filename.jsp in your action attr. or href
../ = contains current folder simple(demo.project.filename.jsp)
Servlet can only be called with 1 slash forward to your project name..
My problem was in web.xml file. In one <servlet-mapping> there was an error inside <url-pattern>: I forgot to add / before url.
I have installed JDK 1.7 and Tomcat 7.0. I am unable to execute basic servlet program. Kindly tell me the process of execution. And just give me details what are new things in Tomcat 7.0.
If I have to place any annotation like #WebServlet, tell me in which file I have to place and which packages I have to import.
web.xml
<web-app>
<servlet>
<servlet-name>kiru</servlet-name>
<servlet-class>DatesrvApp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>kiru</servlet-name>
<servlet-pattern>/classes/date</servlet-pattern>
</servlet-mapping>
</web-app>
DatesrvApp.java
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class DatesrvApp extends GenericServlet {
public void service(ServletRequest req,ServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Date d = new Date();
pw.println("<b><center>Date and Time is" + d.toString() + "</center></b>");
pw.close();
}
}
GenericServlet servlet can't read your URL pattern, Please use HttpServlet.
you should put
<url-pattern>/classes/date</url-pattern>
instead of
<servlet-pattern>/classes/date</servlet-pattern>
And put servlet-api.jar file from lib folder of the directory where Tomcat 7.0 is installed in your classpath.
Please use HttpServlet as suggested by Masud.