我正在尝试为我的应用程序编写一个简单的运行状况检查servlet,以查看我的Tomcat服务器是否正在运行。从本质上讲,它是一个带有“/ health”端点的servlet,它是一个空页面。以下是我的代码:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HealthCheckServlet extends HttpServlet
{
private static final String SUCCESS = "Success";
private static final String FAILURE = "Failure";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
isConnectionOK(request);
}
private static String isConnectionOK(HttpServletRequest request) throws IOException
{
String status = "";
try
{
URL connectionURL = new URL(request.getRequestURL().toString() + "/health");
HttpURLConnection connection = (HttpURLConnection) connectionURL.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int httpResponseCode = connection.getResponseCode();
if (httpResponseCode == 200)
{
status = SUCCESS;
}
}
catch (IOException e)
{
status = FAILURE;
}
return status;
}
}
当我导航到localhost:8080 / health并对其进行调试时,它会在URL connectionURL = new URL(request.getRequestURL().toString() + "/health");
到达我的断点,但不会进入任何后续断点。页面本身返回的状态代码为200,所以我只是想让代码接受它。
我是否在尝试在servlet中建立此连接时出错了,或者是否存在其他问题?