HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET 并且请求的资源不允许指定的 HTTP 方法
Posted
技术标签:
【中文标题】HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET 并且请求的资源不允许指定的 HTTP 方法【英文标题】:HTTP Status 405 - HTTP method GET is not supported by this URL & The specified HTTP method is not allowed for the requested resource 【发布时间】:2021-04-27 00:43:09 【问题描述】:当我打开浏览器 (Chrome) 时 http://localhost:8084/Staff_Member_office_hours_management/EmailSendingServlet,然后我看到 doService() 中的“服务”正在登录控制台,并且我的线程正常工作,但是我没有看到 doGet() 中的“It Works”被记录,我在浏览器中得到以下错误:
此 URL 不支持 HTTP 方法 GET
这是怎么引起的,我该如何解决?
我有以下 servlet:
import java.io.IOException;
import static java.lang.System.out;
import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static org.apache.coyote.http11.Constants.a;
/**
* A servlet that takes message details from user and send it as a new e-mail
* through an SMTP server.
*
* @author www.codejava.net
*
*/
@WebServlet("/EmailSendingServlet")
public class EmailSendingServlet extends HttpServlet
private String host;
private String port;
private String user;
private String pass;
private static final Random RANDOM = new SecureRandom();
public static final int PASSWORD_LENGTH = 8;
public static String generateRandomPassword()
// Pick from some letters that won't be easily mistaken for each
// other. So, for example, omit o O and 0, 1 l and L.
String letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789+@";
String pw = "";
for (int i=0; i<PASSWORD_LENGTH; i++)
int index = (int)(RANDOM.nextDouble()*letters.length());
pw += letters.substring(index, index+1);
return pw;
public void init()
// reads SMTP server setting from web.xml file
ServletContext context = getServletContext();
host = context.getInitParameter("host");
port = context.getInitParameter("port");
user = context.getInitParameter("user");
pass = context.getInitParameter("pass");
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
response.setContentType("registeration.jsp");
String recipient = request.getParameter("mail");
String id = request.getParameter("id");
String subject = "Temporary Password to your account";
String content = generateRandomPassword();
String resultMessage = "";
try
Class.forName("com.mysql.jdbc.Driver");
String urldb = "jdbc:mysql://localhost:3306/staffmember";
String userdb = "root";
String passworddb = "";
String Line;
Connection Con = null;
Statement Stmt = null;
PreparedStatement preparedStatement = null;
Con = DriverManager.getConnection(urldb, userdb, passworddb);
Stmt = Con.createStatement();
//out.print("mmmmmm");
String query4 = "UPDATE student SET password = '" + content + "' WHERE id = '" + id + "';";
preparedStatement = Con.prepareStatement(query4);
int rows3 = preparedStatement.executeUpdate(query4);
catch (SQLException cnfe)
System.err.println("Exception: " + cnfe);
catch (ClassNotFoundException ex)
Logger.getLogger(EmailSendingServlet.class.getName()).log(Level.SEVERE, null, ex);
try
EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
content);
resultMessage = "The e-mail was sent successfully";
catch (Exception ex)
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
finally
request.setAttribute("Message", resultMessage);
getServletContext().getRequestDispatcher("/Result.jsp").forward(
request, response);
我还有另一个如下 servlet:
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.http.HttpSession;
public class registeration extends HttpServlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
HttpSession session = request.getSession();
response.setContentType("text/html");
String id = request.getParameter("id");
String name = request.getParameter("name");
String mail = request.getParameter("mail");
String phone = request.getParameter("mobile");
String password ="";
response.setContentType("text/html;charset=UTF-8");
Connection con = null;
Statement stmt = null;
ResultSet RS = null;
PreparedStatement pst = null;
boolean status = true;
try (PrintWriter out = response.getWriter())
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/staffmember";
String user = "root";
String passworddb = "";
con = DriverManager.getConnection(url, user, passworddb);
//out.println("Registered Successfully 1");
if (mail != null && mail.trim().length() > 0)
stmt = con.createStatement();
String fetchEmail = "Select mail from student where mail=" + "'" + mail + "'";
ResultSet rs = stmt.executeQuery(fetchEmail);
//out.println("Registered Successfully 2");
if (!rs.next())
String sql = "insert into student values('" + name + "','" + mail + "','" + phone + "','" +id + "','" + password +"')";
status = stmt.execute(sql);
out.println("Registered Successfully");
response.sendRedirect("EmailSendingServlet");
out.println("Click " + "<a href=\"login.jsp\">Here</a>" + " to signin");
else
out.print("<span id=\"msg\" style=\"color:red;\">" + mail + "</span>" + " is already registered. Click "
+ " <a href=\"login.jsp\">Here</a>" + " to signin");
else
String sql = "insert into student values('" + name + "','" + mail + "','" + phone + "' ,'" + id + "','" + password +"')";
status = stmt.execute(sql);
out.println("Registered Successfully");
out.print("Click " + "<a href=\"login.jsp\">Here</a>" + " to signin");
catch (Exception e)
out.println(e);
finally
try
con.close();
catch (SQLException e)
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
processRequest(request, response);
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
processRequest(request, response);
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
return "Short description";
// </editor-fold>
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>FileUploadPdf</servlet-name>
<servlet-class>FileUploadPdf</servlet-class>
</servlet>
<servlet>
<servlet-name>Search</servlet-name>
<servlet-class>Search</servlet-class>
</servlet>
<servlet>
<servlet-name>showStaff</servlet-name>
<servlet-class>showStaff</servlet-class>
</servlet>
<servlet>
<servlet-name>makeReservation</servlet-name>
<servlet-class>makeReservation</servlet-class>
</servlet>
<servlet>
<servlet-name>canelTransaction</servlet-name>
<servlet-class>canelReservation</servlet-class>
</servlet>
<servlet>
<servlet-name>cancelReservation</servlet-name>
<servlet-class>cancelReservation</servlet-class>
</servlet>
<servlet>
<servlet-name>notification</servlet-name>
<servlet-class>notification</servlet-class>
</servlet>
<servlet>
<servlet-name>messagetostaff</servlet-name>
<servlet-class>messagetostaff</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>registeration</servlet-name>
<servlet-class>registeration</servlet-class>
</servlet>
<servlet>
<servlet-name>EmailSendingServlet</servlet-name>
<servlet-class>EmailSendingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadPdf</servlet-name>
<url-pattern>/FileUploadPdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Search</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>showStaff</servlet-name>
<url-pattern>/showStaff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>makeReservation</servlet-name>
<url-pattern>/makeReservation</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>canelTransaction</servlet-name>
<url-pattern>/canelTransaction</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cancelReservation</servlet-name>
<url-pattern>/cancelReservation</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>notification</servlet-name>
<url-pattern>/notification</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>messagetostaff</servlet-name>
<url-pattern>/messagetostaff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>registeration</servlet-name>
<url-pattern>/registeration</url-pattern>
</servlet-mapping>
<context-param>
<param-name>tls</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>host</param-name>
<param-value>smtp.gmail.com</param-value>
</context-param>
<context-param>
<param-name>port</param-name>
<param-value>587</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>mohamedmagdysoffar@gmail.com</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value>wcfhecitqrdkfkka</param-value>
</context-param>
<servlet-mapping>
<servlet-name>EmailSendingServlet</servlet-name>
<url-pattern>/EmailSendingServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
【问题讨论】:
【参考方案1】:这可能是因为您没有覆盖 EmailSendingServlet 中的 doGet 方法。此类仅覆盖了 doPost 方法。您必须将代码移动到 doPost 中的 doGet 覆盖方法。
【讨论】:
我这样做给我null 你能从控制台在这里添加堆栈跟踪以获取空错误以上是关于HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET 并且请求的资源不允许指定的 HTTP 方法的主要内容,如果未能解决你的问题,请参考以下文章
HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET [重复]
HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST
Apache Tomcat HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET [重复]
HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET
HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET - RequestDispatcher 出现错误