2016-01-10 46 views
0

我想我遇到這個問題是因爲一個愚蠢的錯誤,但我似乎無法修復它。所以,我有一個,就是要接受用戶的詳細信息,並將其傳遞到其中的值將被插入到users2表HTTP狀態405 - 此URL不支持GET - Java servlet

 <!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 

回答

1

要調用一個HTTP GET請求該servlet的NetBeans的HTML文件,你有沒有在你的servlet中定義了doGet方法。你應該把它定義是這樣的:

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

或者你應該改變HTTP方法(即Servlet的)從GetPost

+0

謝謝!我怎麼能改變http方法調用從獲取到發佈? – elmify

+0

@elmify,你不能修改呼叫(這是網絡),但你可以重定向他們:看我的代碼。 –

0

呼叫在您使用Post方法,而不是讓你的表單定義。一個get請求不能工作,因爲你沒有在servlet中定義一個doGet方法。

+0

嗨,因爲我在我的html中使用post方法,並且在servlet中定義了post,所以我認爲該程序不需要get方法。一旦我在servlet中定義了doGet方法,我應該複製並粘貼與doPost相同的代碼? – elmify

+0

只有當你點擊提交按鈕時纔會調用你的servlet的doPost,但是當你通過輸入url來請求你的頁面時,doGet會被調用(如果存在) –

+0

發生get error,因爲你刷新並且沒有get方法定義。我認爲你的servlet邏輯有一些錯誤。嘗試一步步調試 – sebter

0

檢查你的servlet:

如果接受POST,你必須擁有的doPost:

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

然後,如果它不接受GET,它也許是你沒有的doGet。

只要把這樣的(如果你想相同的行爲)來重定向請求:

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