zoukankan      html  css  js  c++  java
  • JSP(2)—绝对路径与相对路径、配置Servlet与Servlet注解

    一、绝对路径和相对路径
    ①开发时建议使用据对路径,使用绝对路径肯定没有问题,但是用相对路径可能会有问题。
    在由Servlet转发到JSP页面时,此时在浏览器地址栏显示Sevvlet路径,若JSP页面的超链接还是相对与该JSP页面的地址,
    则可能会出现路径混乱的问题。

    例如: A.jsp———>B.jsp———>C.jsp

    
            (子目录)/Demo7_RelativePathAndAbsolutePath/A.jsp
                    :<a href="B.jsp">To B Page</a>
    
            (子目录)/Demo7_RelativePathAndAbsolutePath/B.jsp
                    :<a href="../C.jsp">To C Page</a>
    
        (根目录)/C.jsp
                :<a href="Demo7_RelativePathAndAbsolutePath/A.jsp">To A Page</a>

    一般开发过程中页面之间不会进行跳转,都使用Servlet进行中转之后在选择跳转的页面

    (子目录)
        <a href="<%=request.getContextPath()%>/Demo7_RelativePathAndAbsolutePath/B.jsp">To B Page</a>
    (子目录)<a href="<%=request.getContextPath() %>/C.jsp">To C Page</a>
    (根目录)<a href="<%=request.getContextPath()%>/pathServlet">To A Page</a>
    Servlet:
    @WebServlet("/pathServlet")
    public class PathServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                    throws ServletException, IOException {
    
            List<String> cities = Arrays.asList("北京","上海","广州");
            request.setAttribute("cities", cities);
    
            //通过转发的方式响应C.jsp
            request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
                forward(request, response);
        }
    
    }

    ②绝对路径:相对与当前web应用的根路径的路径。即任何路径都要带上ContextPath
    例如http://localhost:8080/Demo7_Cookie_Session/C.jsp,C.jsp就可以说是绝对路径

    ③如何使用绝对路径:
    若/代表的是站点的根目录,在其前面加上contextPath()即可,而contextPath可以由request或
    application的getContextPath()方法获取。
    例如:

            <a href="<%=request.getContextPath()%>/Demo7_RelativePathAndAbsolutePath/B.jsp">
                            To B Page</a>
            <a href="<%=request.getContextPath() %>/C.jsp">To C Page</a>
            <a href="<%=request.getContextPath()%>/pathServlet">To A Page</a>
        request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
            forward(request, response);     
    

    2.JavaWeb开发过程中/代表什么
    ①当前web应用的根路径:http://localhost:8080/Demo7_Cookie_Session/容器
    其中/交由Servlet来处理

    1).request请求转发时:
                request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
                forward(request, response);
    2).web.xml文件中Servlet-mapping<servlet-mapping>
                  <serlet-name>step2Servlet</servlet-name>
                  <url-pattern>/step2Servlet</url-pattern>
                </servlet-mapping>
    
    3).各种定制标签   

    ②web站点的根路径:http://localhost:8080/
    其中交由浏览器处理

            1).超链接
                <a href="/pathServlet">To A Page</a>
            2).表单中的action
                <form action="/index.jsp" method="post"></form> 
            3).做请求重定向的时候
                response.sendRedirect("/step-2.jsp");
    

    二、Servlet注解和Servlet配置文件

    在Servlet3.0之前使用Servlet都需要在web.xml中配置Servlet,Servlet3.0之后可以使用@WebServlet注解的方式进行配置,使用方便。所以不管是在超链接或者form表单的action中都可以使用这两种方式,但是还是action中使用多一点,一般不会页面之间互相访问,而且action中同时也使用绝对路径,以免造成混乱。

    方式一在web.xml中配置:

     <servlet>
        <display-name>step1Servlet</display-name>
        <servlet-name>step1Servlet</servlet-name>
        <servlet-class>com.shopCart.Servelt.Step1Servlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>step1Servlet</servlet-name>
        <url-pattern>/step1Servlet</url-pattern>
      </servlet-mapping>

    方式二:使用@WebServlet注解

    
    @WebServlet("/pathServlet")
    public class PathServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                    throws ServletException, IOException {
    
            List<String> cities = Arrays.asList("北京","上海","广州");
            request.setAttribute("cities", cities);
    
            //通过转发的方式响应C.jsp
            request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
                forward(request, response);
        }
    
    }

    注意:这两种方式不能同时使用,否则会出现异常:
    Server Tomcat v7.0 Server at localhost failed to start.
    Caused by: java.lang.IllegalArgumentException: The servlets named [step1Servlet] and [com.shopCart.Servelt.Step1Servlet] are both mapped to the url-pattern [/step1Servlet] which is not permitted

  • 相关阅读:
    Oracle 11g R2(11.2.0.4.0)+udev搭建RAC
    在 Linux 中用 nmcli 命令绑定多块网卡
    linux 配置聚合连接team(网卡绑定) , systemd 控制
    7.linux目录结构
    6.VMware备份linux操作系统
    5.linux关机和切换运行模式
    4.linux安装
    linux网络配置知识
    3.Virtual Box的安装
    2.vmWare安装
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10453982.html
Copyright © 2011-2022 走看看