This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
ED: this question is not a duplicate. the alleged duplicate makes no mention of the error that was in my console, and the solution to my problem is not found in the linked question.
i'm using derek banas's tutorials to learn servlets right now. or at least, i'm trying to! i keep encountering a 404 error when i submit my form.
here is my code. first, the java class:
package helloservlets;
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;
/**
* Servlet implementation class Lesson41
*/
#WebServlet
public class Lesson41 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 {
String usersName = request.getParameter("yourname");
String theLang = request.getParameter("Language");
int firstNum = Integer.parseInt(request.getParameter("firstnum"));
int secondNum = Integer.parseInt(request.getParameter("secondnum"));
int sumONum = firstNum + secondNum;
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + usersName);
output.println("</h3><br />" + firstNum + " + " + secondNum);
output.println(" = " + sumONum + "<br />Speaks " + theLang + "</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
my web.xml file:
<?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">
<servlet>
<servlet-name>Lesson41</servlet-name>
<servlet-class>helloservlets.Lesson41</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Lesson41</servlet-name>
<url-pattern>/Lesson41</url-pattern>
</servlet-mapping>
</web-app>
and my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br /> <input name="yourname" /><br /> First
Number <input name="firstnum" /><br /> Second Number <input
name="secondnum" /><br /> <input type="hidden" name="Language"
value="English" /><br /> <input type="submit" />
</form>
</body>
</html>
in my console, i am getting this warning:
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Lesson41' did not find a matching property.
and that's the only warning i'm getting. i googled it and followed the advice i found here, but it hasn't fixed my problem.
thank you in advance for any help!
Are you sure the url in the form is correct? http://localhost:8080/Lesson41/
If you are using Eclipse, by default it deploys your application as http://localhost:8080/[Your-Project-Name-Here]/Lesson41/
Related
I am following derek banas' tutorial on youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) and when I try to run the code the browser displays a status-404 and
Message: /Lesson41/
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I was getting IllegalArguementsException saying something about 2 different servlets being mapped to the same url-pattern. So I removed the #WebServlet annotation.
Now I'm getting the 404 and I don't know what the cause is. I think eclipse doesn't see the sayhello.html
Here is the code:
Servlet: Lesson41.java
public class Lesson41 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String usersName= request.getParameter("yourname");
String theLang = request.getParameter("Language");
int firstNum = Integer.parseInt(request.getParameter("firstnum"));
int secondNum = Integer.parseInt(request.getParameter("secondnum"));
int sumONum = firstNum + secondNum;
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + usersName);
output.println("</h3><br />" + firstNum + " + " + secondNum);
output.println(" = " + sumONum + "<br />Speaks " + theLang);
output.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
The 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_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>Lesson41</servlet-name>
<servlet-class>helloservlets.Lesson41</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Lesson41</servlet-name>
<url-pattern>/Lesson41</url-pattern>
</servlet-mapping>
</web-app>
html: sayhello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br />
<input name="yourname" /><br />
First Number<br />
<input name="firstnum" /><br />
Second Number<br />
<input name="secondnum" /><br />
<input type="hidden" name="Language" value="English" /><br />
<input type="submit" />
</form>
</body>
</html>
The directory structure:
directory Structure
Expected Output:
It should show a form with fields for name, number1, number2 and a submit button. It correctly goes to localhost:8080/Lesson41/ but can't see the html.
Update the url-pattern to /Lesson41/
<url-pattern>/Lesson41/</url-pattern>
form action change to /Lesson41/ and try. might help you.
<form method="post" action="/Lesson41/">
</form>
Also, while mapping you are using servlet-class as 'helloservlets.Lesson41', need to add the package. 'helloservlets' in servlet 'Lesson41'.
This question already has answers here:
How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
(16 answers)
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 1 year ago.
I'm using Tomcat8 server and i'm getting following error.
It's url is http://localhost:8080/WeatherWebApp When i'm submitting the details then it's giving this error.
Here is WeatherServlet.java class
package org.akshayrahar;
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;
public class WeatherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
WeatherServlet(){
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("again");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("akshay rahar");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Here is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app version="2.4" 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">
<display-name>WeatherWebApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>WeatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/CurrentWeather</url-pattern>
</servlet-mapping>
</web-app>
Here is index.html file too:-
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script >
function initMap() {
var input =document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyACZhmkSaIz436Dt3kHt_cVEYKN-gHzfYo&libraries=places&callback=initMap"async defer></script>
</head>
<body>
<h1>Find weather of any city in the world</h1>
<h2>Enter City Name</h2>
<form id="form" action="CurrentWeather" method="GET">
<input id="pac-input" type="text" name="cityname">
</form><br>
<div class="button1">
<button type="submit" form="form" value="Submit">Submit</button>
</div>
</body>
</html>
I've also mentioned stylesheet.css file in comment. Please check it.
The error shows that Tomcat is unable to create an instance of your WeatherServlet class.
You should make its constructor and other methods public too. You can even make use of the default constructor by removing the less accessible constructor:
public class WeatherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WeatherServlet(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("again");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("akshay rahar");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Please provide a fully qualified class name on your web.xml. I was facing a similar issue and this is what fixed it.
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>org.akshayrahar.WeatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/CurrentWeather</url-pattern>
</servlet-mapping>
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'm new to Java Programming & having a tough time with Servlets & JSP, for the wide range of challenges it throws. For now, I'm unable to access the Servlet page due to this error:
HTTP Status 404 - The requested resource is not available
This might seem to be a naive question for many, however after trying all the tips and tricks ranging from Stack Overflow to resorting to other study materials, I couldn't figure out the exact cause of the problem.
Servlet file:
package coreservlets;
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("/GoodCodeServlet")
public class GoodCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Code Sample";
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType + "<html> \n" +
"<head> <title>" +title+ "</title></head>" +
"<body bgcolor=\"#eee\">" +
"<h1 align=\"center\">" +title+ "</h1>" +
// Text inside a <pre> tag is displayed in a fixed-width font,
//and it preserves both spaces and line breaks....
"<pre> \n" + getCode(request)+ "</pre>" +
"</body> </html>"
);
}
protected String getCode(HttpServletRequest request)
{
return (request.getParameter("code"));
}
HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#FDEFD6">
<center> <h1>Submit Code Samples</h1>
<form action="/coreservlets.GoodCodeServlet" >
Code: <br><br>
<textarea rows="12" cols="40" name="code"></textarea> <br><br>
<input type="submit" value="submit" />
</form>
</center>
</body>
</html>
web.xml file:
<?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">
<servlet>
<servlet-name>CodeSample</servlet-name>
<servlet-class>coreservlets.GoodCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CodeSample</servlet-name>
<url-pattern>/coreservlets.GoodCodeServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ShowParameters</servlet-name>
<servlet-class>/coreservlets.ShowParameters</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowParameters</servlet-name>
<url-pattern>/coreservlets.ShowParameters</url-pattern>
</servlet-mapping>
</web-app>
Context path is missing from form action.
Please refer :
Use Relative Path for Form Actions in jsp
Form Action sampleservlet giving me exception
Hi I am receiving the 404 HTTP status while submiting the below jsp.
HTTP Status 404 - /TestServlet1
Could you please help me to resolve this error
Note : The person and the dog class were defined
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
<form id="a" action="/TestServlet1">
<input type="submit">
</form>
<%
%>
Name = '${person.name}'
Dog = '${person.dog.name}'
</body>
</html>
TestServlet1
package test;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Dog g = new Dog();
g.setName("dogeee");
Person p = new Person();
p.setDog(g);
p.setName("xxx");
request.setAttribute("person", p);
RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
dispatch.forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
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>GlobalWeather</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet1</url-pattern>
</servlet-mapping>
</web-app>
First and Third answers are right. Either write action="TestServlet1" or action="/Projectname/TestServlet1". If you put /testServet1 in action, it means you are specifying the path the desired file which in this case is wrong while if you use testservlet1 in action , it means you are searching for a file name testservlet1 in your project to run.
404 means that the URL was not found. I suspect that you need the web application name in your URL.
So rather than using a form action of:
/TestServlet1,
try
/name_of_your_web_app/TestServlet1
Just remove tha / in your form action:
<form id="a" action="/TestServlet1">
change it to
<form id="a" action="TestServlet1">
In HTML, adding a / means the relative URL and without a slash means absolute URL. Or better to use the context as mentioned here:
<form id="a" action=${pageContext.request.contextPath}/TestServlet1>
you have to put your contextroot + your servlet name...
contextRoot usually is the name of your project.
action="/nameProject/TestServlet1"
I hope this helps you
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting HTTP method POST is not supported by this URL when calling my httpservlet through an html form. I can't see where I'm going wrong. I'm running this on a Tomcat server. Thanks in advance.
The servlet is supposed to print a set of random numbers to the browser.
Here is my servlet:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.RandNumSet;
public class RandomServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public RandomServlet() {
// TODO Auto-generated constructor stub
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
TreeSet<Integer> randNum = RandNumSet.generateRandNumSet();
Iterator<Integer> iterator = randNum.iterator();
String numString = "";
while (iterator.hasNext()){
numString = numString + iterator.next() + " ";
}
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> "+ numString + " </h1>");
out.println("</body>");
}
}
Here is the calling html form snippet:
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
Here are the web.xml snippets
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>servlets.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
I am using the following way and its works...
first: check the directory first you have made like-
com.ServletExample
|-JavaResource
|-src
|-com.servletExample
|-RandomServlet.java
|-WebContent
|-META-INF
|-WEB-INF
|-Lib
|-web.xml
|-index.jsp
second: index.jsp is here
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
</body>
</html>
third: RendomServlet.java is here
package com.servletExample;
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;
public class RandomServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RandomServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> " + "Hello" + " </h1>");
out.println("</body>");
}
}
fourth: web.xml is here
<?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>JSPExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>com.servletExample.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
</web-app>
I hope this will work.