gpt4 book ai didi

Java servlet 故意抛出 http 500 错误

转载 作者:行者123 更新时间:2023-11-29 03:26:25 26 4
gpt4 key购买 nike

我正在开发一个 java servlet。我正在使用 glassfish 服务器 4。

最终用户通过 URL 参数向我发送信息,如下所示:

http://myIP:8080/TestProject/TestServlet?param1=test1&param2=test2&param3=test3

我从 param1、param2 和 param3 获取值,我想将它们写入我的数据库。如果我在我的数据库中写入信息时遇到 SQL 异常,我想抛出“500 内部服务器错误”让他们知道我有一些技术问题并重新发送他们的请求。我想知道是否有默认方式来执行此操作、设置一些状态、显示文本...?

代码如下:

@WebServlet(urlPatterns = {"/TestServlet"}, initParams = {
@WebInitParam(name = "param1", value = ""),
@WebInitParam(name = "param2", value = ""),
@WebInitParam(name = "param3", value = "")})
public class TestServlet extends HttpServlet {

String param1;
String param2;
String param3;
boolean dbOK;

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get parametars from the request
param1 = request.getParameter("param1");
param2 = request.getParameter("param2");
param3 = request.getParameter("param3");
//Input in db
dbOK = Database.saveParams(param1,param2,param3);

//Print response
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>T-Mobile Interface</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> dbOK=" + dbOK + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

最佳答案

你应该可以用 response.sendError(int) 来做到这一点

编辑:意思是说参数是您要发送的错误代码,因此在您的情况下为 500。

关于Java servlet 故意抛出 http 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20772837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com