gpt4 book ai didi

java - HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST :glassfish server using netbeans

转载 作者:行者123 更新时间:2023-12-01 09:45:57 24 4
gpt4 key购买 nike

I am getting this error on using post method in my form-- HTTP Status 405 - HTTP method POST is not supported by this URL.  

我的register.html文件如下:

    <html>        
<head>
<title>Register form</title>
</head>
<body>
<form method="post" action="Register">
Name:<input type="text" name="name" /><br/>
Email ID:<input type="text" name="email" /><br/>
Password:<input type="text" name="pass" /><br/>
<input type="submit" value="register" />
</form>
</body>
</html>

我的Register.java servlet代码如下

package Glassfish;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* @author Intel I 5
*/
public class Register extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String name = request.getParameter("name");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
try{

//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");

//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/test","user","pass");

PreparedStatement ps=con.prepareStatement
("insert into register values(?,?,?)");

ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, pass);
int i=ps.executeUpdate();

if(i>0)
{
out.println("You are sucessfully registered");
}

}
catch(Exception se)
{
se.printStackTrace();
}

}

}

我的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>Register</servlet-name>
<servlet-class>Glassfish.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/Register</url-pattern>

</servlet-mapping>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

我已经尝试了 http 错误 405 中提供的所有实例,用于堆栈溢出中的 post 方法,但没有用。

最佳答案

更改您的 public void service() 方法,并将其更改为 public void doGet() 并删除方法内的 doPost(req,resp) 调用。请告诉我这是否有帮助。这将 100℅ 工作!!

关于java - HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST :glassfish server using netbeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38031146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com