简体   繁体   中英

Why it is shown HTTP Status 405 - HTTP method POST is not supported by this URL?

I have worked on demo login application on spring. This is my LoginController.java:

       package com.pran.pal.controller;


    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse;   
    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView; 

   @Controller

public class LoginController extends HttpServlet{

     @RequestMapping(value="/login" ,method=RequestMethod.GET) 
     public ModelAndView login(HttpServletRequest request, HttpServletResponse response) { 

         String userName=request.getParameter("userName"); 
         String password=request.getParameter("password"); 
         String message; 
         if(userName != null && !userName.equals("") && userName.equals("admin") 
         && password != null && !password.equals("") && password.equals("123")){ 
             message = "Welcome " +userName + "."; 
        return new ModelAndView("index", "message", message);   
        }
         else{ 
             message = "Wrong username or password."; 
        return new ModelAndView("index", "message", message); 
        } 
    } 

I have used eclipse Neon, Tomcat v9.

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.pran.pal</groupId>
  <artifactId>LoginSystemPAL</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>LoginSystemPAL Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.4.RELEASE</version>
   </dependency>
  </dependencies>
  <build>
    <finalName>LoginSystemPAL</finalName>
  </build>
</project>

and this is my web.xml

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet> 
  <servlet-name>LoginController</servlet-name> 
  <servlet-class>com.pran.pal.controller.LoginController</servlet-class> 
  <load-on-startup>1</load-on-startup> 
  </servlet>   
  <servlet-mapping> 
  <servlet-name>LoginController</servlet-name> 
  <url-pattern>/login</url-pattern>
   </servlet-mapping>   

</web-app>

It has shown that input username and password that:

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

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.

How can I solve this problem?

Your <servlet-class> should be com.pran.pal.controller.LoginController , but you should also have a dependency to Spring Web from here .

Also, you are not using properly the Spring @Controller annotation. Please follow a step by step tutorial like the one here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM