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

Jsp属性范围的更多相关文章

  1. java web学习总结(二十六) -------------------JSP属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  2. JavaWeb---总结(十八)JSP属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  3. javaweb学习总结(十八)——JSP属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  4. javaWeb学习总结(8)- JSP属性范围(5)

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  5. JavaWeb学习 (十七)————JSP属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  6. Idea破解办法+idea免费生成注册码+jsp属性选择器+注解什么的都报错

    Idea破解办法: http://blog.csdn.net/bitcarmanlee/article/details/54951589 idea免费生成注册码: http://idea.iteblo ...

  7. javaweb学习总结(十八)——JSP属性范围(转)

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  8. Java Web学习总结(17)——JSP属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  9. JavaWeb——JSP内置对象application,JSP属性范围

    application application语法 application对象 JSP属性范围 范围的分类 page request session application pagecontext延伸 ...

  10. JSP 属性范围

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3781056.html 一.属性范围 所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围 ...

随机推荐

  1. 为什么做Web开发要选择PHP

    大部分互联网公司做WEb开发都选择PHP,PHP的优势在哪?你应该知道的 以前偶尔被人问到,为什么你(和大部分互联网公司)做Web开发要选择PHP, PHP有什么好处.简单的回答便是“PHP简单,开发 ...

  2. listview的简单封装

    package com.itheima.googleplay.ui.view; import android.content.Context; import android.graphics.Colo ...

  3. kill -QUIT <pid>

    On Solaris and Linux a thread dump is also printed if the J2SE process receives a QUIT signal. So ki ...

  4. 在ubuntu 上创建 ssl 证书

    soap webservice 调试工具: soap UI, 可以下载下来玩一玩. Introduction TLS, or transport layer security, and its pre ...

  5. The Wall (medium)

    The Wall (medium) Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, f ...

  6. C​# ​日​期​时​间

    //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...

  7. (转)多个MapReduce作业相互依赖时,使用JobControl进行管理

    原文地址:http://mntms.iteye.com/blog/2086990 要处理复杂关系的数据,一个工程里面绝对不止一个MapReduce作业,当有多个MapReduce作业时,       ...

  8. php正则表达式手册

    (http://deerchao.net/tutorials/regex/regex.htm)转载:作者:deerchao php的正则表达式很强大,学好了的确有很大的用处,但是正则表达式的规则很繁琐 ...

  9. 修炼dp(1)

    从最简单的开始: POJ:The Triangle #include <cstdio> #include <algorithm> #include <cstring> ...

  10. STM32开发指南-跑马灯实验

    简单对I/O口的控制,主要通过对寄存器的读写控制.主要通过I/O的寄存器来控制:1. 控制I/O的方向2. 控制I/O的输出电平或上下来电阻3. 存储I/O口当前的输入状态(高低电平) 对使用LED灯 ...