Getting a 404 error on click on submit button on the form. Getting error (The requested resource is not available.) for the URL mapping http://localhost:8080/HelloWorld/HelloServlet. I tried the reference this as well, Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available" but this also doesn't seem to work.
1, index,html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HelloServlet">
<input type="submit" value="HIT">
</form>
</body>
</html>
2. HelloServlet
package com.example.aman;
import java.io.IOException;
import javax.servlet.ServletConfig;
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 HelloServlet
*/
#WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public HelloServlet() {
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("DIVESH");
}
/**
* #see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
response.getWriter().println("DIVESH");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3. 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>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>
</web-app>
http://localhost:8080/HelloWorld/HelloServlet.
Straight away that tells me what the problem is. You are trying to access a servlet from within the same folder directory. This is what happens if you are not careful with your relative URLS
In your form, change the URL mapping to this:
<form action="/HelloServlet">
<input type="submit" value="HIT">
</form>
Or if that didn't work, do this:
<form action="../HelloServlet">
<input type="submit" value="HIT">
</form>
Adding a / to the path makes it absolute.
Adding ../ makes it go to the parent of the current directory.
Hope that helps.
Related
I have a problem with only one servlet, all the others (declared in the same way) work nice
Servlet with problem :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Connexion extends HttpServlet {
public static final String VUE = "/WEB-INF/connexion.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
Another Servlet who works :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Authentification extends HttpServlet {
public static final String VUE = "/WEB-INF/authentification.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
The JSP who call the servlet "Connexion" :
<form id="form_authentification" class="formulaire" method="post" action="connexion">
<input type="text" class="input_text" title="<fmt:message key="mail_ou_nom" />" placeholder="<fmt:message key="mail_ou_nom" />" required />
<input type="submit" class="btn" value="<fmt:message key="suivant" /> "/>
And the web.xml :
<!-- Authentification -->
<servlet>
<servlet-name>Authentification</servlet-name>
<servlet-class>com.myproject.servlets.Authentification</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Authentification</servlet-name>
<url-pattern>/authentification</url-pattern>
</servlet-mapping>
<!-- Connexion -->
<servlet>
<servlet-name>Connexion</servlet-name>
<servlet-class>com.myproject.servlets.Connexion</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Connexion</servlet-name>
<url-pattern>/connexion</url-pattern>
</servlet-mapping>
So If I put the method "get" in the form : "form_authentification" of the JSP authentification.jsp it works but I have in line the browser :
http://localhost:8080/myproject/connexion?
Where from "?" is coming in the line browser?
If I put the method "post" in the form : "form_authentification" of the JSP authentification.jsp I have a white page for connexion.jsp and the browser tag icon of apache server.
Thank you for your answer
i'm trying to solve this problem since 2 days, there are the same title of problems in many post on stackoverflow but it was all different, because they allways had an error in code, but in my case i have a sample servlet, and when i run the project i get this error
HTTP Status 405 - HTTP method GET is not supported by this URL
this is my class servlet
package web;
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 Controller extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void init() throws ServletException {
}
#Override
protected void doGet(HttpServletRequest requet, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out =response.getWriter();
out.println("hello");
}
}
and 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>test2</display-name>
<servlet>
<servlet-name>cs</servlet-name>
<servlet-class>web.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cs</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
please tell me how solve this problem, thanks ...
Simple doGet Servlet Or You Can add try catch blog to debug code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logout extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.println("Hello");
}
catch(Exception e)
{
out.println(e.toString())
}
}
}
I think you probably have not add GAT method on form attribute Like this
<form action="web" method="GET"></form>
I created a dynamic web project in Eclipse 4.5, called HelloServlet and modified web.xml. I also added Tomcat 8 in Eclipse. I think I did everything right, but I still can't run the servlet.
In Project explorer: /src/HelloServlet/HelloWorldServlet.java.
HelloWorldServlet.java:
package HelloServlet;
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 HelloWorldServlet
*/
#WebServlet("/HelloWorldServlet")
public class HelloWorldServlet 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 {
// TODO Auto-generated method stub
response.getWriter().println("Hello world.");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
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>HelloServlet</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>HelloWorldServlet</servlet-name>
<servlet-class>HelloServlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>
Sometimes, I get class not found without /HelloWorldServlet. I searched and knew how to set servlet-class correctly, but it still doesn't run.
It seems in newer versions (I don't know of what. Servlet or Eclipse?), I don't have to add anything to web.xml. #WebServlet("/HelloWorldServlet") seems to be enough. I discovered it by trying.
But I noticed strange behavior: While web.xml was modified as in the question, I could run the server then get errors in the browser like Reuqested resource isn't available or class not found exception in /HelloServlet & /HelloServlet/HelloWorldServlet. After restarting the computer, I couldn't run the server (I think, because of modifying web.xml). I don't remember adding anything new.
What can a JSP form action be?
I have a Login.jsp page for the user to end the details.
Can i give the servlet class in the form action?
here is the the servlet code.
package mybean;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
System.out.println("In the Login Servlet");
LoginBean user = new LoginBean();
user.setUemail(request.getParameter("uemail"));
user.setUpass(request.getParameter("upass"));
user = LoginDAO.login(user);
if(user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("LoginSuccess.jsp");
}else
response.sendRedirect("LoginFailed.jsp");
} catch (Throwable exc)
{
System.out.println(exc);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
As per the specs , it can take any valid URI
This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined.
Can i give the servlet class in the form action ?
Yes if the servlet class name resolves to a valid URL mapped in the web.xml or else you will encounter a 404 .
Let us consider your JSP is at the root of the application, then
<FORM action="someServletName" method="post">
Now this will be resolved as protocol://servername:port/context/someServletName .Now somewhere you should have a mapping for /someServletName , either in web.xml or through annotation to a Servlet or JSP.
<servlet>
<servlet-name>someServletName</servlet-name>
<servlet-path>packageName.servletName</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>someServletName</servlet-name>
<url-pattern>/someServletName</url-pattern>
</servlet-mapping>
I am trying to call a servlet when submitting the FormPanel in GWT.I am setting the url on form.setAction("myurl") and also done mapping on the web.xml but while on the form suvmit the servlet is not called.Here is the code:
public class MainEntryPoint implements EntryPoint {
public void onModuleLoad() {
AbsolutePanel panel=new AbsolutePanel();
FileUpload upload = new FileUpload();
upload.setName("file");
final FormPanel form = new FormPanel();
form.setWidget(panel);
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction("/NewServlet");
RootPanel.get().add(new Label("path="+GWT.getModuleBaseURL()+"/NewServlet"));
Button sub=new Button("Submit");
sub.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
RootPanel.get().add(new Label("In click event submiting form"));
try{
form.submit();
}catch(Exception e){RootPanel.get().add(new Label(e+""));}
}});
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) {
// This event is fired just before the form is submitted. We can take
// this opportunity to perform validation.
RootPanel.get().add(new Label("On submit"));
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
// When the form submission is successfully completed, this event is
// fired. Assuming the service returned a response of type text/html,
// we can get the result text here (see the FormPanel documentation for
// further explanation).
RootPanel.get().add(new Label("On submiting complete"));
}
});
panel.add(upload);
panel.add(sub);
RootPanel.get().add(form);
}
}
Here is the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>org.fileup.client.UploadServlet2</servlet-class>
</servlet>
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/uploadservlet2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>welcomeGWT.html</welcome-file>
</welcome-file-list>
</web-app>
And NewServlet.java
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// /* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* #return a String containing servlet description
*/
#Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Please tell me how can I call the NewServlet on submitting the FormPanel.
You probably want form.setAction(GWT.getHostPageBaseURL() + "NewServlet")
Or as an alternative you could use: form.setAction("NewServlet");
Very similar code, with your exact setAction statement, works for me. Possibly the problem was just that
<servlet-class>NewServlet</servlet-class>
left out the dotted path, e.g. org.fileup.server., to NewServlet.