如何使用 json 在 JSP 中显示错误消息?
How to show error message in JSP using json?
我正在做一个仅使用 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(作为测试)..请帮助我..
提前致谢..!
你还没有post你的全部。不管怎样,如果你想在弹出窗口中显示错误。
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>
我正在做一个仅使用 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(作为测试)..请帮助我..
提前致谢..!
你还没有post你的全部。不管怎样,如果你想在弹出窗口中显示错误。
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>