0

HTTP Status 405 - HTTP method GET is not supported by this URL

i am having trouble getting the page to work, i have my form method to post and my servlet has doPost, however it keeps showing me that something i not supporting the Post method. i am just trying to do a simple website.

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
Hello
<%=request.getAttribute("name") %>
</body>
</html>


Servlet


package esempio2;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;

public class Nome extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{

        String name=request.getParameter("username");
        request.setAttribute("nome", name);

        RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request, response);
    }
}

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">
<servlet>
<servlet-name>nome</servlet-name>
<servlet-class>esempio2.Nome</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>nome</servlet-name>
<url-pattern>/tester.do</url-pattern>
</servlet-mapping>
</web-app>
Massimo Mannini
  • 98
  • 2
  • 10
  • Typo mistake while setting attribute "nome" – Braj Nov 12 '15 at 10:04
  • where is your form? From the exception "HTTP method GET is not supported by this URL" it's clear that form is requesting GET method whereas you haven't defined any GET method in servlet. by default form's method is GET. you have to set it explicitly to POST. [HTML Form Method](http://www.w3schools.com/tags/att_form_method.asp) – Braj Nov 12 '15 at 10:05

2 Answers2

0

Try add the doGet() method !

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
  //your code here
}

Try to add something like this

<input type="text" name="nome">

So try to add a method which wrap doGet and doPost, something like this :

public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        processRequest(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        processRequest(request,response);
    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        String nome = "PutYourNameHere";
        request.setAttribute("nome",nome);

        RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request, response);
    }
Dwhitz
  • 1,155
  • 6
  • 23
  • 33
-1

It looks like your form method is not correct

Nenad
  • 394
  • 1
  • 12