【问题标题】:How to show error message in JSP using json?如何使用 json 在 JSP 中显示错误消息?
【发布时间】:2018-04-11 06:47:22
【问题描述】:

我正在做一个只使用 JSP、Servlet、Mysql 和 Jquery 的应用程序。我想显示一条来自 Jquery 的错误消息,该消息设置为 json 对象(在 servlet 中)。 在这里我附上了我的代码

  • JSP/Jquery

    $("#submitbtn").click(function() {
            $.ajax({
                type : 'GET',
                url : 'createaction',
                data : {
                    code : document.getElementById("code").value,
                    descr : document.getElementById("descr").value,
                    checkboxVal : "create" 
                },
                success : function(responseText) {
                    alert(responseText);
                }
            });
    
  • 小服务程序

    JSONObject json      = new JSONObject();
    String chkVal = request.getParameter("checkboxVal");
    
    
        if (chkVal.equals("create")){
            try {
                create(request, response);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    json.put("error", e);
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                response.setContentType("application/json");
                response.getWriter().write(json.toString());
                System.out.println("aaaaaaaaaaaa");
            }
        }
    

这里我尝试向前端显示sqlexception(作为测试)..请帮助我..

提前谢谢..!

【问题讨论】:

    标签: jquery ajax jsp servlet-3.0


    【解决方案1】:

    你还没有发布你的全部。无论如何,如果您想在弹出窗口中显示错误。

    ErrorController.java

    public class ErrorController extends HttpServlet {
    
      @Override
      public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final JSONObject json = new JSONObject();
        final String chkVal = request.getParameter("checkboxVal");
    
        if (chkVal.equals("create")) {
          try {
            throw new SQLException();
          } catch (final Exception e) {
            e.printStackTrace();
            try {
              json.put("error", e);
            } catch (final JSONException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }
            response.setContentType("application/json");
            response.getWriter().write(json.toString());
          }
        }
      }
    }
    

    errorJsp.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Insert title here</title>
            <script>
                $(document).ready(function(){
                    $("#submitbtn").click(function() {
                        $.ajax({
                            type : 'GET',
                            url : '/message/createaction',
                            data : {
                                code : document.getElementById("code").value,
                                descr : document.getElementById("descr").value,
                                checkboxVal : "create" 
                            },
                            success : function(responseText) {
                                alert(responseText.error);
                            }
                        });
                    });
                });
            </script>
        </head>
        <body>
            <form>
                <input id="code" type="hidden" value="123456">
                <input id="descr" type="hidden" value="Description...">
                <input id="submitbtn" type="button" value="Submit" />
            </form>
        </body>
    </html>
    

    web.xml

    <!DOCTYPE web-app PUBLIC 
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
        <display-name>Archetype Created Web Application</display-name>
    
        <servlet>
            <servlet-name>ErrorController</servlet-name>
            <servlet-class>com.application.ErrorController</servlet-class>
    
            <description>Ma première servlet de test.</description>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>ErrorController</servlet-name>
            <url-pattern>/createaction</url-pattern>
        </servlet-mapping>
    </web-app>
    

    【讨论】:

    • responsetext.error 给了我警报中的错误.. 非常感谢.. 我使用的是 servlet 3.0.. 所以我没有使用 web.xml
    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2011-04-20
    • 2020-08-12
    相关资源
    最近更新 更多