IDE:MyEclipse 2014(自带Tomcat 7)

Web项目路径:

Web项目配置信息:

WebRoot

  --WEB-INF

    --web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyPro02</display-name>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.zhiyuan.web.MyServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>MultiMappingServlet</servlet-name>
    <servlet-class>com.zhiyuan.web.MultiMappingServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/servlet/MyServlet</url-pattern>
  </servlet-mapping>

 <!--  一个Servlet映射多个Mapping -->
  <servlet-mapping>
    <servlet-name>MultiMappingServlet</servlet-name>
    <url-pattern>/admin/multiMappingServlet.do</url-pattern>
    <url-pattern>/user/multiMappingServlet.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

</web-app>

  admin

    --index.jsp

<%@ 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>管理员角色</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>
      <%request.setAttribute("jiaose", "admin");// 服务端跳转
      request.getRequestDispatcher("multiMappingServlet.do").forward(request, response);
       %>
  </body>
</html>

src

  --com.zhiyuan.web

    --MultiMappingServlet.java

 package com.zhiyuan.web;
 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 public class MultiMappingServlet extends HttpServlet {

     /**
      * 串行号
      */
     private static final long serialVersionUID = 7091664030094770793L;
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         this.doPost(request, response);
     }

     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         response.setCharacterEncoding("gbk");
         PrintWriter pw=response.getWriter();
         pw.write("角色管理:");
         String jiaose=(String) request.getAttribute("jiaose");
         if ("admin".equals(jiaose)) {
             pw.write("管理员");
         }else if ("user".equals(jiaose)) {
             pw.write("用户");
         }
     }

 }

MultiMappingServlet

Web项目运行结果:

注意:如果使用了下面的跳转形式:

<%@ 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>管理员角色</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>
      <%
      request.setAttribute("jiaose", "admin");
       %>
    <a href="multiMappingServlet.do">管理员角色</a>
  </body>
</html>

或者

<%@ 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>管理员角色</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>
      <%
      request.setAttribute("jiaose", "admin");
      // 重定向(客户端跳转)
      response.sendRedirect("multiMappingServlet.do");
       %>
  </body>
</html>

MultiMappingServlet.java代码不变的情况下,

发现Web项目启动时,地址栏改变了,设置的属性也消失了。

如果希望无论怎样跳转,属性都可以被保存下来,可以扩大到session范围。

修改后的代码:

<%@ 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>管理员角色</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>
      <%
      pageContext.setAttribute("jiaose", "admin", pageContext.SESSION_SCOPE);
      // 重定向(客户端跳转)
      response.sendRedirect("multiMappingServlet.do");
       %>
  </body>
</html>

或者

<%@ 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>管理员角色</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>
      <%
      pageContext.setAttribute("jiaose", "admin", pageContext.SESSION_SCOPE);
       %>
    <a href="multiMappingServlet.do">管理员角色</a>
  </body>
</html>

MultiMappingServlet.java的代码:

 package com.zhiyuan.web;
 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 public class MultiMappingServlet extends HttpServlet {

     /**
      * 串行号
      */
     private static final long serialVersionUID = 7091664030094770793L;
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         this.doPost(request, response);
     }

     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         response.setCharacterEncoding("gbk");
         PrintWriter pw=response.getWriter();
         pw.write("角色管理:");
         String jiaose=(String) request.getSession().getAttribute("jiaose");
         if ("admin".equals(jiaose)) {
             pw.write("管理员");
         }else if ("user".equals(jiaose)) {
             pw.write("用户");
         }
     }

 }

MultiMappingServlet

最新文章

  1. [LeetCode] Simplify Path 简化路径
  2. 魅族M8时期写过几个app,纪念一下曾经的自己
  3. VMware克隆SUSE网卡配置
  4. CSS 居中方法集锦
  5. Xshell
  6. Doxygen Tool For Unity
  7. ASP.NET中的常用快捷键
  8. cocos2d-x3.2中加入Android手机震动
  9. eclipse加入辅助线,配合代码格式化使用
  10. 增强遍历和Object多参数遍历
  11. Python中的切片符
  12. MongoDB 搭建可复制群集
  13. C# SQLiteHelper
  14. JAVA8方法引用
  15. 每天学点Linux命令之grep 和 wc命令
  16. Js中,Map对象的使用
  17. Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)
  18. C#编程(七十二)----------DynamicObject和ExpandoObject
  19. 《转》python学习(10)-集合
  20. Java导出引用jar包的文件

热门文章

  1. jquery 中 $(&#39;div&#39;,&#39;li&#39;)
  2. Android手机图片适配问题
  3. 记一次坑die(误)的题目--HDU2553--(DFS)
  4. fzu Problem - 2232 炉石传说(二分匹配)
  5. 我终于有案例库啦(github 提供的)
  6. PHP开源CRM-推荐几个
  7. 一个appium 博客
  8. Excel相关问题
  9. 服务器遭受 ssh 攻击
  10. article标签