我正在开发一个java servlet。我正在使用glassfish服务器4.
最终用户通过网址参数向我发送信息,如下所示:
http://myIP:8080/TestProject/TestServlet?param1=test1¶m2=test2¶m3=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>
}
答案 0 :(得分:3)
您应该可以使用response.sendError(int)
编辑:意味着该参数是您要发送的错误代码,因此在您的情况下为500。
答案 1 :(得分:1)
AFAIK ServletException也被翻译为错误500。但通常您需要在响应上设置状态并将其重定向(调度)到JSP,其中包含您的自定义错误页面。您可以设置请求属性以自定义JSP内容。
req.setAttribute("MY_ERROR", "Database request failed");
resp.setStatus(HttpServletResponse.SC_ERROR);
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/error.jsp");
dispatcher.forward(req, resp);