java web项目发送404页面

有些时候,我们要向页面发送404错误页面


比如说:

浏览帖子页面,在帖子不存在、或者未审核的时候,我们要向页面发送404

做法如下


一:

在web.xml里面配置

<error-page>
  	<error-code>404</error-code>
  	<location>/page/404.html</location>
  </error-page>


二:

/**
 * 为空(无此帖子,或者没有审核)
*/
Topic to = loadTopic(request.getParameter("id"));
if(to == null || to.getStatus() == 0)
{
	response.sendError(404);
	return ;
}


你可能感兴趣的:(java web项目发送404页面)