Servlet: HTTP Status 404 - The requested resource is not available [duplicate] - java

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

Related

method="post" in form tag of html does not work in servlet in java

method="post" does not work in servlet
any help would be appreciated. Thanks in advance.
I get error of -- HTTP method GET is not supported by this URL-- eventhough i use dopost method in my servlet class.
Here is my code:
index.html
<!DOCTYPE html>
<html>
<body>
<form action="add" method="post">
Enter 1st number: <input type="text" name="num1">
<br>
Enter 1st number: <input type="text" name="num2">
<br>
<input type="submit">
</form>
</body>
</html>
AddServlet.java
package com.practice;
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 AddServlet extends HttpServlet
{ private static final long serialVersionUID = 1L;
#Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = i+j;
System.out.println("result is:" + k);
PrintWriter out1 = res.getWriter();
out1.println("result is: dopost method" + k);
}
}
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="WebAppID" version="4.0">
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.practice.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>

Its my first servlet program and I have issues [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 3 years ago.
My index looks like this:
<HTML>
<head>
<title> JSP Servlet Example</title>
</head>
<body>
<div align="center" style="margin-top: 50px;">
<form action="ServletManager">
Please enter your Username: <input type="text" name="username" size="20px"> <br>
Please enter your Password: <input type="text" name="password" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</div>
</body>
</HTML>
My servlet:
package helloweb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ServletManager extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// reading the user input
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=ISO-8859-1\"> \n" +
"<title> JSP Servlet Example </title> \n" +
"</head> \n" +
"<body> <div align='center'> \n" +
"<style= \"font-size=\"12px\" color='black'\"" + "\">" +
"Username: " + username + " <br> " +
"Password: " + password +
"</font></body> \n" +
"</html>"
);
}
}
My XML:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
JSPServletExample
<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>Hello</servlet-name>
<servlet-class>helloweb.ServletManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/ServletManager.java</url-pattern>
</servlet-mapping>
Here is my issue:
Firefox can’t find the file at /C:/Users/HP/IdeaProjects/ourdemo /web/ServletManager?username=hello&password=hello.`
I host at the tomcat 7 server.
The first page appears. I type in the username and password, and I get this.
Please help.
Please change the url pattern change this in web.xml from /ServletManager.java to /ServletManager and try
https://javabelazy.blogspot.com/2010/02/student-registration-form-in-java.html (Working code)

Requested Resource is not available error [duplicate]

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 trying to build a twitter search using jsp, servlet, Tomcat-6.0.43 and eclipse and getting HTTP Status 404 error. Can anyone please check where am I going wrong.
My Code:
first.jsp:
<%# 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>
<form action="ServletValues.java" method="get">
Enter Twitter Search Details : <input type="text" name="first"><br>
<input type="submit">
</form>
</body>
</html>
TwitterServlet.java:
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
public class TwitterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TwitterServlet() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String CONSUMER_KEY = "[data]";
String CONSUMER_KEY_SECRET = "[data]";
String AccessToken = "[data]";
String AccessTokenSecret = "[data]";
response.setContentType("text/html");
String input1 = request.getParameter("first");
try{
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
AccessToken oathAccessToken = new AccessToken(AccessToken, AccessTokenSecret);
twitter.setOAuthAccessToken(oathAccessToken);
List<Status> status = twitter.getUserTimeline(input1);
for (Status status2 : status)
{
System.out.println("---Tweet---"+status2.getText());
}}catch (TwitterException te){
System.out.println("Error occured "+te);
}
super.doPost(request, response);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
Twitter12</display-name>
<servlet>
<description>
</description>
<display-name>
TwitterServlet</display-name>
<servlet-name>TwitterServlet</servlet-name>
<servlet-class>
TwitterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TwitterServlet</servlet-name>
<url-pattern>/TwitterServlet</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>
Error: HTTP Status 404 - /Twitter12/ServletValues.java
type Status report
message /Twitter12/ServletValues.java
description The requested resource is not available.
In your jsp file form action, Replace
<form action="ServletValues.java" method="get">
With
<form action="TwitterServlet" method="get">
Since in your web.xml your servlet mapping pattern is TwitterServlet for the Servlet class that you intend to call.
Also in your servlet you are just printing to System.out while you should write into the reponse stream using
response.getOutputStream().write("---Tweet---"+status2.getText());
so that it shows up in the response
Change your form action to:​
<form action="/TwitterServlet" method="get">
your servlet url is /TwitterServlet as you have defined
<servlet-mapping>
<servlet-name>TwitterServlet</servlet-name>
<url-pattern>/TwitterServlet</url-pattern>
</servlet-mapping>

404 with simple java servlet/html form [duplicate]

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/

Basic Servlet doesn't work

I tried to create a simple servlet from the book, but to no avail.
I use GlassFish Server Open Source Edition 3.1.2.2, jdk1.7.0_10, Notepad.
root\WEB-INF\classes\net\ensode\glassfishbook\formhandling\FormHandlerServlet.class :
package net.ensode.glassfishbook.formhandling;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormHandlerServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)
{
String enteredValue;
enteredValue = request.getParameter("enteredValue");
response.setContentType("text/html");
PrintWriter printWriter;
try
{
printWriter = response.getWriter();
printWriter.println("<p>");
printWriter.print("You entered: ");
printWriter.print(enteredValue);
printWriter.print("</p>");
}
catch (IOException e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h2>");
printWriter.println("If you are reading this, your application server is good to go!");
printWriter.println("</h2>");
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
}
root/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>dataentry.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FormHandlerServlet</servlet-name>
<servlet-class>
net.ensode.glassfishbook.formhandling.FormHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormHandlerServlet</servlet-name>
<url-pattern>/formhandlerservlet</url-pattern>
</servlet-mapping>
</web-app>
root/dataentry.html :
<!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=UTF-8">
<title>Data Entry Page</title>
</head>
<body>
<form method="post" action="formhandlerservlet">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Please enter some text:</td>
<td><input type="text" name="enteredValue" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
I create the WAR file by the following command in the console:
cd e:\root\
jar cvf formhandling.war *
Next, I copy my WAR file into the ‘autodeploy’ directory.
The link: http://localhost:8080/formhandling/
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/dataentry.html
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/formhandlerservlet
gives me the right response: “If you are reading this, your application server is good to go!”
It seems that Glassfish couldn’t find the dataentry.html file, but I copy all code from the book and don't know what to do.
The code from the book didn't work because dataentry.html was created in the folder WEB-INF, I correct this mistake:
<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>
Now, I have my html file in the root directory. It works.

Categories

Resources