学习了异常处理,在处理用户请求时,合理的异常处理能提升应用的稳定性。
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("/exampleServlet")
public class ExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Some code that might throw an exception
int result = 10 / 0;
} catch (ArithmeticException e) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("
An error occurred: " + e.getMessage() + "
");}
}
} 标签:PrintWriter,http,javax,10.18,import,servlet,response From: https://www.cnblogs.com/bawangji666/p/18475151