我试图在Ecclipse IDE上运行时遇到Apache 405错误...
下面是我的servlet代码。

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;


public class MyFirstServlet extends HttpServlet  {
    public void Service(HttpServletRequest request,HttpServletResponse response)throws IOException
    {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.write("<html>");
        out.write("<title>Welcome</title>");
        out.write("<body bgcolor=blue>");
        out.write("<h1>Hi Wassup</h1><br>");
        out.write("<h2>Great that you are studying Java</h2>");
        out.write("</body>");
        out.write("</html>");
        out.close();


    }

}


下面是我的web.xml文件。

<web-app>
<servlet>
<servlet-name>firstservlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstservlet</servlet-name>
<url-pattern>/homepage</url-pattern>]
</servlet-mapping>
</web-app>


有人请帮助我解决此问题..因为我是第一次尝试。
提前致谢

最佳答案

您已经在Servlet的构造函数中实现了响应HTML。您实际需要做的是实现doGet / doPost方法并将代码放入其中。

喜欢

protected void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException
{
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.write("<html>");
    out.write("<title>Welcome</title>");
    out.write("<body bgcolor=blue>");
    out.write("<h1>Hi Wassup</h1><br>");
    out.write("<h2>Great that you are studying Java</h2>");
    out.write("</body>");
    out.write("</html>");
    out.close();
}

10-06 07:10