怎么让xml文件只解析一次就可以了 在控制器中,重写了HttpServlet的service()方法,我把xml文件解析放在这个方法中,但是,每次从浏览器得到请求,都要解析一次xml文件,效率太低。。应该把xml文件放在那,只要解析一次就行了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // 声明数据库连接 Connection conn = null; //得到用户请求的URI String path = (String) request.getRequestURI(); //获取jsp的连接名 String actionName = path.substring(path.lastIndexOf("/") + 1, path .lastIndexOf(".")); Action action = new Action(); String forwardPage = null; Service actionService = null; String result = null; String type = null; try { //连接数据库 conn = DBConn.getConnection(); //手动保存 conn.setAutoCommit(false); } catch (SQLException e) { logger.error("数据库异常"); request.setAttribute("error_msg", "数据库异常"); request.getRequestDispatcher("/error.jsp").forward(request,response); } catch (DataException e) { logger.error("数据异常"); request.setAttribute("error_msg", "数据异常"); request.getRequestDispatcher("/error.jsp").forward(request,response); } if(map == null){ try{//获取xml解析文件 map = ModelFactory.getInstance().parseXml(); }catch (Exception e) { logger.info("文件解析异常"); request.setAttribute("error_msg", "文件解析异常"); request.getRequestDispatcher("/error.jsp").forward(request,response); } } if (map.containsKey(actionName)){ // 根据key获得value值 action = map.get(actionName); try { actionService = (Service) Class.forName(action.getClasses()).newInstance(); // 获得映射 forwardPage = actionService.Service(request, response, conn); // 得到Result的type type = action.getMap().get(forwardPage).getType(); // 得到Result的value result = action.getMap().get(forwardPage).getValue(); // 如果返回success就根据配置文件actions.xml中的跳转方式转发或重定向到指定的页面 if(forwardPage.equals("success")){ //数据库提交 conn.commit(); // 根据type值判断响应方式 if (type != null && type.equals("redirect")) { response.sendRedirect(result); // 重定向 } else { request.getRequestDispatcher(result).forward(request, response);// 转发 } //如果返回false就返回到提交请求的页面,并提示相应的错误信息 }else if(forwardPage.equals("false")){ //数据库回滚 conn.rollback(); request.getRequestDispatcher(result).forward(request, response); } }catch (Exception e) { if(e instanceof DataException){ logger.error("数据异常", e); request.setAttribute("error_msg", "数据异常"); try { conn.rollback(); } catch (SQLException e1) { logger.error("数据库回滚出错"); } request.getRequestDispatcher("/error.jsp").forward(request,response); } }finally { try { DBConn.close(conn); } catch (DataException e) { logger.error("数据库关闭异常"); request.setAttribute("error_msg", "数据库关闭异常"); request.getRequestDispatcher("/error.jsp").forward(request,response); } } }else { request.getRequestDispatcher("/error.jsp").forward(request,response);// 转发到错误页面 } } 不得已铁代码的,大家帮忙看看,该怎么改改。文件解析的那块代码是:if(map == null){try{//获取xml解析文件map = ModelFactory.getInstance().parseXml();}catch (Exception e) {logger.info("文件解析异常");request.setAttribute("error_msg", "文件解析异常");request.getRequestDispatcher("/error.jsp").forward(request,response);}} //重写这个方法@Overridepublic void init(ServletConfig config) throws ServletException { this.config = config; this.init(); //解析xml文件 } FLEX+JAVA高频率读取数据库内存泄露 数据库分页 还是用JavaBean分页好 帮我找个东西 关于上传jsp文件的问题,请高手赐教。 沈阳的朋友们! 如何在linux9中安装tomcat? 困惑,关于路径问题 大家帮我登陆看看,这个网站的“add photo”功能用什么写的? 在tomcat 5.0中servlet的问题 26分求画饼图的applet(只给得了这么多) 最近在学HTML5,想用HTML5做前台java后台,想求明白人给讲解下~ filter的问题?求指教
// 声明数据库连接
Connection conn = null;
//得到用户请求的URI
String path = (String) request.getRequestURI();
//获取jsp的连接名
String actionName = path.substring(path.lastIndexOf("/") + 1, path
.lastIndexOf("."));
Action action = new Action();
String forwardPage = null;
Service actionService = null;
String result = null;
String type = null;
try {
//连接数据库
conn = DBConn.getConnection();
//手动保存
conn.setAutoCommit(false);
} catch (SQLException e) {
logger.error("数据库异常");
request.setAttribute("error_msg", "数据库异常");
request.getRequestDispatcher("/error.jsp").forward(request,response);
} catch (DataException e) {
logger.error("数据异常");
request.setAttribute("error_msg", "数据异常");
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
if(map == null){
try{//获取xml解析文件
map = ModelFactory.getInstance().parseXml();
}catch (Exception e) {
logger.info("文件解析异常");
request.setAttribute("error_msg", "文件解析异常");
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
}
if (map.containsKey(actionName)){
// 根据key获得value值
action = map.get(actionName);
try {
actionService = (Service) Class.forName(action.getClasses()).newInstance();
// 获得映射
forwardPage = actionService.Service(request, response, conn);
// 得到Result的type
type = action.getMap().get(forwardPage).getType();
// 得到Result的value
result = action.getMap().get(forwardPage).getValue();
// 如果返回success就根据配置文件actions.xml中的跳转方式转发或重定向到指定的页面
if(forwardPage.equals("success")){
//数据库提交
conn.commit();
// 根据type值判断响应方式
if (type != null && type.equals("redirect")) {
response.sendRedirect(result); // 重定向
} else {
request.getRequestDispatcher(result).forward(request, response);// 转发
}
//如果返回false就返回到提交请求的页面,并提示相应的错误信息
}else if(forwardPage.equals("false")){
//数据库回滚
conn.rollback();
request.getRequestDispatcher(result).forward(request, response);
}
}catch (Exception e) {
if(e instanceof DataException){
logger.error("数据异常", e);
request.setAttribute("error_msg", "数据异常");
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("数据库回滚出错");
}
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
}finally {
try {
DBConn.close(conn);
} catch (DataException e) {
logger.error("数据库关闭异常");
request.setAttribute("error_msg", "数据库关闭异常");
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
}
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);// 转发到错误页面
}
}
文件解析的那块代码是:if(map == null){
try{//获取xml解析文件
map = ModelFactory.getInstance().parseXml();
}catch (Exception e) {
logger.info("文件解析异常");
request.setAttribute("error_msg", "文件解析异常");
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
}
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init(); //解析xml文件 }