服务器启动成功,运行正常,可无法跳转页面,同时,localhost8080无法打开
package com.liujia.cake.global;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class GlobalController extends GenericServlet {
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
/*
/.do
* /login.do DefaultCtroller login
* /Cake/detail.do CakeCtroller detail
* /admin/Cake/add.do CakeCtroller add
* */
System.out.println("开始处理");
HttpServletRequest request=(HttpServletRequest) servletRequest;
HttpServletResponse response=(HttpServletResponse) servletResponse;
String path=request.getContextPath();
if(path.indexOf("/admin")!=-1){
path=path.substring(7);
}else{
path=path.substring(1);
}
/*
* login.do
* Cake/detail.do
* Cake/add.do
*
* */
int index=path.indexOf("/");
String className=null;
String methodName=null;
if (index!=-1){
className="com.liujia.cake.controller."+path.substring(0,index)+"Controller";
methodName=path.substring(index+1,path.indexOf(".do"));
}else {
className="com.liujia.cake.controller.DefaultController";
methodName=path.substring(0,path.indexOf(".do"));
}
try {
Class cl=Class.forName(className);
Object object=cl.newInstance();
Method method= cl.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(object,request,response);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.liujia.cake.controller;
import com.liujia.cake.biz.CatalogBiz;
import com.liujia.cake.biz.CatalogBizImpl;
import com.liujia.cake.entity.Catalog;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CatalogController {
private CatalogBiz catalogBiz=new CatalogBizImpl();
public CatalogController() throws IOException {
}
// /admin/Catalog/list.do
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Catalog root=catalogBiz.getRoot();
request.setAttribute("root",root);
System.out.println("调用这个玩意了");
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[] infors=request.getParameterValues("infor");
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(infors[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");
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星