如何使用json在JSP中显示错误消息?

时间:2018-04-11 06:47:22

标签: jquery ajax jsp servlet-3.0

我只使用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);
                }
            });
    
  • 的Servlet

    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(作为测试)..请帮助我..

提前致谢..!

1 个答案:

答案 0 :(得分:0)

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

<强> 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>