index.html
<!DOCTYPE html>
<html>
<body>
<form action="MyServlet">
<input type="submit" value="Get Count">
</form>
</body>
</html>
MyServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
@WebServlet(urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
private String mymsg;
//private String message;
int hc;
public void init() throws ServletException {
mymsg = "Hello World!";
hc=0;
//message = "Hey";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Setting up the content type of webpage
response.setContentType("text/html");
// Writing message to the web page
PrintWriter out = response.getWriter();
out.println("<h1>" + mymsg + "</h1>");
out.println("Count: ");
out.println(hc++);
}
public void destroy() {
}
}
HTTP Status 405 – Method Not Allowed
Type Status Report
Message HTTP method POST is not supported by this URL
Description The method received in the request-line is known by the origin server but not supported by the target resource.
===================================================
i dont understand why because im only using get method and not using post at all...Please help
In that servlet corresponding POST method handling is not implemented. To solve this overriding the below method from HttpServlet,
void javax.servlet.http.HttpServlet.doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
Sample code,
@WebServlet(urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
private String mymsg;
//private String message;
int hc;
public void init() throws ServletException {
mymsg = "Hello World!";
hc=0;
//message = "Hey";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Setting up the content type of webpage
response.setContentType("text/html");
// Writing message to the web page
PrintWriter out = response.getWriter();
out.println("<h1>" + mymsg + "</h1>");
out.println("Count: ");
out.println(hc++);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.