我想我遇到這個問題是因爲一個愚蠢的錯誤,但我似乎無法修復它。所以,我有一個,就是要接受用戶的詳細信息,並將其傳遞到其中的值將被插入到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
謝謝!我怎麼能改變http方法調用從獲取到發佈? – elmify
@elmify,你不能修改呼叫(這是網絡),但你可以重定向他們:看我的代碼。 –