HTTP状态405 - 此URL不支持GET - Java servlet

时间:2016-01-10 12:39:38

标签: java xml http-status-code-405

我认为我遇到这个问题是因为一个愚蠢的错误,但我似乎无法修复它。所以,我在Netbeans上有html文件,用于接受用户详细信息并将其传递到servlet,其中值将被插入到users2表中

        <!DOCTYPE html>
        <html>
        <head>
           <title>Tennis Club Registration</title>
        </head>
        <body bgcolor='LightSkyBlue'>

           <h2 style='text-align:center'>Member Registration</h2>

           <p style='text-align:center'>Please enter the details of new member:</p>

           <form name='MemberRegistration' action='echousers' method='post'>

           <p style='text-align:center'>First Name: <input type='text' size='20' name='firstname'>
           <p style='text-align:center'>Last Name: <input type='text' size='20' name='lastname'><br>
           <p style='text-align:center'>Telephone Number: <input type='text' size='20' name='tel_no'>
           <p style='text-align:center'>Membership Number: <input type='text' size='20' name='member_no'>
           <p style='text-align:center'>Subscription Type: <input type="radio" name="sub_type" value="life"> Life
           <input type="radio" name="sub_type" value="annual"> Annual

           <p style='text-align:center'><input type='submit' value='Submit'></p>

           </form>

        </body>
        </html>

    I also have web.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
            <servlet>
                <servlet-name>User</servlet-name>
                <servlet-class>User</servlet-class>
            </servlet>
            <servlet-mapping>
                <servlet-name>User</servlet-name>
                <url-pattern>/echousers</url-pattern>
            </servlet-mapping>
            <session-config>
                <session-timeout>
                    30
                </session-timeout>
            </session-config>
        </web-app>


    And my User.java file looks like this:

        import java.io.* ;
        import java.util.* ;
        import javax.servlet.* ;
        import javax.servlet.http.* ;
        import java.sql.* ; 

        public class User extends HttpServlet
        { 

           public void doPost( HttpServletRequest request, HttpServletResponse response ) 
                                      throws IOException, ServletException
           {       
              response. setContentType( "text/html" ) ;   
              PrintWriter out = response.getWriter() ;


              HttpSession session = request.getSession() ;
              String firstname = request.getParameter( "firstname" ) ;
              String lastname = request.getParameter( "lastname" ) ;
              String tel_no = request.getParameter( "tel_no" ) ;
              String member_no = request.getParameter( "member_no" ) ; 
              String sub_type = request.getParameter( "sub_type" ) ;

              String sub ;

              if ( sub_type.equals("Life") )
              {
                  sub = "L" ;
              }
              else if ( sub_type.equals("Annual") )
              {
                  sub = "A" ;
              }
              else  // should never get to here!!!
              {
                  sub = "X" ;
              }

              try
              {

                 Connection conn = DriverManager.getConnection( 
                         secret login info) ;                                                    
                 Statement stmt = conn.createStatement();  


                 String strInsert = "INSERT INTO users2 VALUES ( '" + firstname + "', '" + lastname + "', '"
                                            + tel_no + "', '" + member_no + "', '" + sub + "');" ;

                 stmt.executeUpdate( strInsert ) ;

                 String query = "SELECT * FROM users2" ;

                 ResultSet rs = stmt.executeQuery(query) ;

                 while ( rs.next() )                              
                 { 
                    out.print( rs.getString( "first_name" ) ) ;   
                    out.print( "   " );
                    out.print( rs.getString( "last_name" ) ) ;            
                    out.print( "   " );
                    out.print( rs.getString( "tel_no" ) ) ;   
                    out.print( "   " );
                    out.print( rs.getString( "membership_no" ) ) ;
                    out.print( "   " );
                    out.print( rs.getString( "sub_type" ) ) ;
                    out.print( "<br />") ;
                  }
               }
               catch (SQLException e )
               {
                System.out.println( "SQLException: " + e.getMessage() ) ;  
               }  
          }
        } 


When I insert users on the index page:
Echousers loads very slowly and its blank,
when I refresh the echouser page I get the following error

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

    type Status report

    messageHTTP method GET is not supported by this URL

descriptionThe specified HTTP method is not allowed for the requested resource.
I would also like to add that the same code works on my colleagues computer!!

my files are in the following locations...
webapplication_test > web > index.html
webapplication_test > web > web-inf > web.xml
webapplication_test > src > java > User.java

3 个答案:

答案 0 :(得分:1)

您正在调用http get请求,但您尚未在servlet中定义doGet方法。您应该将其定义为:

 public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException

或者您应该将http方法(即Servlet)呼叫从Get更改为Post

答案 1 :(得分:0)

在表单定义中,您使用了Post方法而不是Get。并且get请求无法工作,因为您没有在servlet中定义doGet方法。

答案 2 :(得分:0)

检查你的servlet:

如果它接受POST,你必须有doPost:

public void doPost(HttpServletRequest request, HttpServletResponse response) 
{
// Your code here
}

然后如果它不接受GET,那么也许你没有做到。

就像那样(如果你想要相同的行为)重定向请求:

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