一般都是路径出错.
这里展示的两段代码都会引发上图的405.
1.首先看源码中忘记删除super.doGet()方法.
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.io.IOException;
@WebServlet(name = "TestErrorServlet", value = "/testerr")
public class TestErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//代码中忘记注释super()方法
super.doGet(request, response);
}
}
2.下述中代码只书写了doPost请求的方法.但是通过浏览器访问默认是GET方法.
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.io.IOException;
@WebServlet(name = "TestErrorServlet", value = "/testerr")
public class TestErrorServlet extends HttpServlet {
//只书写了doPost方法,但是浏览器默认访问GET方法.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("test");
}
}
代码中抛异常未被处理.异常抛到了Tomcat,Tomcat直接返回500
doGet中除零异常代码.除零异常未捕获会向上抛出.
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.io.IOException;
@WebServlet(name = "TestErrorServlet", value = "/testerr")
public class TestErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//除零异常未捕获向上抛出
System.out.println(5 / 0);
}
}
一般是正常访问.但是代码中没有给页面返回数据.
代码中什么都没有书写,前端就什么也没有接收到.但是没有错误.
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.io.IOException;
@WebServlet(name = "TestErrorServlet", value = "/testerr")
public class TestErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
可能是Tomcat没有启动 或者启动失败了.(比如Tomcat端口被占用,启动失败)
我们写 Servlet 代码的时候, 首先第一步就是先创建类, 这个类就要继承自 HttpServlet.那我们来看看这个类.
方法名称 | 调用时机 |
---|---|
init | 在 HttpServlet 实例化之后被调用一次 |
destory | 在 HttpServlet 实例不再使用的时候调用一次 |
service | 收到 HTTP 请求的时候调用 |
doGet | 收到 GET 请求的时候调用(由 service 方法调用) |
doPost | 收到 POST 请求的时候调用(由 service 方法调用) |
doPut/doDelete/doOptions/… | 收到其他请求的时候调用(由 service 方法调用) |
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.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "helloServlet", value = "/hello")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
System.out.println("会在HttpServlet实例化时调用一次");
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置返回数据的类型
response.setContentType("text/html");
System.out.println("在收到GET请求的时候调用");
// Hello
//获取流对象用于给页面返回数据
PrintWriter out = response.getWriter();
out.println("");
out.println(""
+ message + "");
out.println("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("在收到POST请求的时候调用");
}
public void destroy() {
System.out.println("会在HttpServlet销毁时会调用");
}
}