创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码!

创建两个类实现共同的接口!

public interface Action { 

    String  execute();
}
public class LoginAction  implements Action{

    public  String  execute(){
System.out.println("LoginAction......");
return "success";
}
}
public class ListAction implements Action {

    public  String  execute(){
System.out.println("ListAction......");
return "success";
}
}

想让用户能访问到我们的后台代码,要么使用servlet  要么使用filter!

使用filter

创建一个filter用来拦截用户的请求

public class DoFilter implements Filter {
//全局的变量
Map<String,String> map=new HashMap<String, String>(); //初始化操作
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("DoFilter 初始化了.............................");
// key是用户请求的路径 value 是对应的全类名
map.put("/login","cn.bdqn.action.LoginAction");
map.put("/list","cn.bdqn.action.ListAction");
} //真正的处理
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//向下转型
HttpServletRequest httpServletRequest=(HttpServletRequest) request;
HttpServletResponse httpServletResponse=(HttpServletResponse) response;
//看一下 各个路径的区别
System.out.println("getContextPath()==>"+httpServletRequest.getContextPath());//项目名
System.out.println("getServletPath()==>"+httpServletRequest.getServletPath());//访问的路径
System.out.println("getRequestURI()==>"+httpServletRequest.getRequestURI());//项目下面的路径
System.out.println("getRequestURL()==>"+httpServletRequest.getRequestURL());//带协议的完整路径
//应该使用getServletPath
String path=httpServletRequest.getServletPath();
try {
if (path.equals("/index.jsp")) {
chain.doFilter(request, response); //放行
}else{
Action action=(Action) Class.forName(map.get(path)).newInstance();
action.execute();
//跳转到成功界面
httpServletRequest.getRequestDispatcher("/success.jsp").forward(request, response);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
@Override
public void destroy() { } }

前台页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<a href="login">登录 </a>
<a href="list">详情 </a>
</body>
</html>

sucess.jsp页面就是一个成功界面!!!省略掉!

我们使用xml文件来代替  map中 所保存的 键值对  信息!

key:用户的请求

value:对应的后台实现类 全类名!

最新文章

  1. inline-block元素vertical-align的问题分析
  2. php的memcache安装,在window10下面
  3. Java Web技术之Cookie
  4. 如何查看python 的api
  5. JS按回车键实现登录的方法
  6. [问题2015S02] 复旦高等代数 II(14级)每周一题(第三教学周)
  7. characterCustomezition的资源打包代码分析
  8. win7下以兼容模式安装oracle10g
  9. Struts文件上传机制
  10. Keil C51汉字显示的bug问题(0xFD问题)
  11. 解决tomcat占用8080端口
  12. ef code first 您没有所需权限
  13. Scrapy中使用Django的Model访问数据库
  14. key.go
  15. Python3学习笔记十八
  16. [原创]K8Cscan插件之端口扫描C#源码
  17. MVC aspx
  18. 菜鸟脱壳之脱壳的基础知识(五)——利用内存断点寻找OEP
  19. Django 学习第三式
  20. Android的数据的存储方式

热门文章

  1. (4.8)SET ANSI_NULLS ON、SET QUOTED_IDENTIFIER ON
  2. 如何在浏览器网页中显示word文件内容
  3. python16_day03【集合、编码、函数、递归、内置函数】
  4. MySQL 单表查询(Day42)
  5. 本地连不上远程mysql数据库(1)
  6. LeetCode:翻转二叉树【226】
  7. asp.net MVC 强类型视图表单Ajax提交的注意事项
  8. 优秀 H5 案例收集 vol.1(不定期更新)
  9. 利用C#查看特定服务是否安装
  10. Dubbo之RPC架构