tomcat可以发布,但是有404错误,找了很久,问题不知道在哪里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public class GlobalController extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { /** .do /login.do DefaultController login /Cake/detail.do CakeController detail /admin/Cake/add.do CakeController add */ HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String path = request.getServletPath(); if (path.indexOf( "/admin" )!=- 1 ){ path = path.substring( 7 ); } else { path = path.substring( 1 ); } /** * path = Catalog/list.do * path = 以下一种 login.do DefaultController login cake/detail.do CakeController detail Cake/add.do CakeController add */ int index = path.indexOf( "/" ); //indexof,判断是否包含 (index!=-1) 意思为包含 String className = null ; String methodName = null ; if (index!=- 1 ){ className = "com.imooc.icake.controller." +path.substring( 0 ,index)+ "Controller" ; methodName = path.substring(index+ 1 ,path.indexOf( ".do" )); } else { className = "com.imooc.icake.controller.DefaultController" ; methodName = path.substring( 0 ,path.indexOf( ".do" )); } try { Class cla = Class.forName(className); // Object object = cla.getDeclaredConstructor(int.class).newInstance(); Object object = cla.newInstance(); Method method = cla.getMethod(methodName,HttpServletRequest. class ,HttpServletResponse. class ); method.invoke(object,request,response); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | public class CatalogController { private CatalogBiz catalogBiz = new CatalogBizImpl(); /** * /admin/Catalog/list.do * /admin/Catalog/toAdd.do * /admin/Catalog/add.do * /admin/Catalog/remove.do */ // /admin/Catalog/list.do public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Catalog root = catalogBiz.getRoot(); request.setAttribute( "root" ,root); request.getRequestDispatcher( "/WEB-INF/pages/admin/catalog_list.jsp" ).forward(request,response); } // /admin/Catalog/toAdd.do public void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { Catalog root = catalogBiz.getRoot(); request.setAttribute( "root" ,root); request.getRequestDispatcher( "/WEB-INF/pages/admin/catalog_add.jsp" ).forward(request,response); } // /admin/Catalog/add.do public void add(HttpServletRequest request,HttpServletResponse response) throws IOException { String[] titles = request.getParameterValues( "title" ); String[] pids = request.getParameterValues( "pid" ); String[] infos = request.getParameterValues( "info" ); List<Catalog> list = new ArrayList<Catalog>(); for ( int i = 0 ; i < titles.length; i++){ Catalog catalog = new Catalog(); catalog.setTitle(titles[i]); catalog.setPid(Integer.parseInt(pids[i])); catalog.setInfo(infos[i]); list.add(catalog); } catalogBiz.add(list); response.sendRedirect( "list.do" ); } // /admin/Catalog/remove.do public void remove(HttpServletRequest request,HttpServletResponse response) throws IOException { int id = Integer.parseInt(request.getParameter( "id" )); catalogBiz.remove(id); response.sendRedirect( "list.do" ); } } |
14
收起
正在回答
6回答
同学你好,建议同学再如下位置处再输出一下path,查看是否有值。另外建议同学在获取path的位置处打上断点,使用debug启动项目,查看是否有执行到该位置。
如果控制台有报错,建议同学将报错贴一下。
祝:学习愉快~
慕码人5437048
2019-11-13 16:45:42
有的,xml的配置是这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version = "3.1" > < filter > < filter-name >encoding</ filter-name > < filter-class >com.imooc.icake.global.EncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encoding</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < servlet > < servlet-name >GlobalServlet</ servlet-name > < servlet-class >com.imooc.icake.global.GlobalController</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >GlobalServlet</ servlet-name > < url-pattern >*.do</ url-pattern > </ servlet-mapping > </ web-app > |
慕码人5437048
2019-11-13 10:49:36
输入的网页是:http://localhost:8080/admin/Catalog/list.do
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧