如题所示,本人现在在写一个结构比较简单的基于Jsp/Servlet的web系统。
不同jsp页面反馈后台数据处理的错误时候,如何只做一个error.jsp页面然后根据各个servlet情况显示错误信息?
比如在许多Servlet中,举一个例子:
//前略
if(MainDAO.createAssess(credit,usertype,ordersID,orderstype)>-1){
response.sendRedirect("/carpool/CreateAssessSuccess.jsp");
}else{
response.sendRedirect("/carpool/CreateAssessFailure.jsp");
System.out.println("创建评价失败!可能订单已被删除!");
}
//后略
类似这种的Servlet还有很多,然后那些个xxxxSuccess.jsp和 xxxxFailure.jsp其实都是很简单的jsp页面,比如
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="/carpool/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/carpool/css/flat-ui.min.css" rel="stylesheet">
<title>取消订单失败</title>
</head>
<body>
<br/><br/>
<center>
<h3>取消订单失败!可能对方已经先你一步取消订单或已被管理员删除!</h3>
<a href="/carpool/index.jsp"><b><u>返回主页</u></b></a>
</center>
</body>
</html>
所以能不能在Servlet怎样设置一下比如一个错误信息,然后传到同一个error.jsp里显示出来?
最近才接触前端脚本所以不太会,找了一下好像也都是说传参数(?但是好像重定向到error页面并不能传参数)。或者大多数解决方法是在后台里写
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1(response);
}
private void test1(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
response.setHeader("content-type", "text/html;charset=UTF-8");
String data = "发起订单失败!";
OutputStream os = response.getOutputStream();
os.write(data.getBytes("UTF-8"));
}
然后再用js写一行一行syso(<……>)来实现返回上一页按钮之类的。。但这样的话还不如一次复制一个jsp页面然后修改一行数据来得实在。。。所以有没有大神能教教我能不能如题那样实现。