zoukankan      html  css  js  c++  java
  • JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

    String path = request.getContextPath(); 
    String basePath = request.getScheme()
    +"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
        <base href=" <%=basePath%>"> 

    这个语句是用来拼装当前网页的相对路径的。 

    <base href="...">是用来表明当前页面的相对路径所使用的根路径的。 
    比如,页面内部有一个连接,完整的路径应该是 http://localhost:80/myblog/authen/login.do 
    其中http://
    server/是服务器的基本路径,myblog是当前应用程序的名字,那么,我的根路径应该是那么http://localhost:80/myblog/。 

    有了这个 <base ... >以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。 
    如果没有这个 <base...>,那么我页面的连链接就必须写全路径,否则服务器会找不到。 

    request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http” 
    request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost" 
    request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80, 
    request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的myblog 
    这四个拼装起来,就是当前应用的跟路径了

     

    最近在自学struts,被里面struts中action 的path与form表单的action属性弄迷糊了。

    struts-config.xml 文件中,action元素中的path属性表示的是浏览器地址栏中相对于应用程序根目录的请求路径,与form 中提交表单以后有谁处理的action属性指定的根路径一致。(只是一致,千万不要以为是绝对相等~)

    例如:form表单的提交处理请求是classesAdd.do,其在ie地址栏中的路径如下所示,

    http://localhost:9000/Struts_study/classesMan/classesAdd.do

    红色部分表示的根路径,所以,action中的classesAdd.do请求的完整路径是classesMan/classesAdd.do

    <form name="form1" action="classesAdd.do" method=post>

    所以 struts 中的action 的path路径是指/classesMan/classesAdd。

    大家可以看着浏览器的地址栏加以配置。祝大家晚上睡觉快乐。

     

    补充一下吧,form中的action的默认路径就是当前路径,

    而struts中的action 的path属性默认路径为根路径,所以要加上所在的文件夹得路径。

     

    使用方法如:

    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>404</title>
    <meta http-equiv="refresh" content="60;url=index.jsp">
    <!-- content="600,即600秒后返回主页,可根据需要修改或者删除这段代码 -->
    <link href="css/error.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <!-- 代码 开始 -->
    <div id="container"><img class="png" src="images/404.png" /> <img class="png msg" src="images/404_msg.png" />
      <p><a href="index.jsp"><img class="png" src="images/404_to_index.png" /></a> </p>
    </div>
    <div id="cloud" class="png"></div>
    <!-- 代码 结束 -->
    
     
    </body>
    </html>

     

  • 相关阅读:
    特征归一化
    什么是端到端(end2end)学习?
    RSA加密原理及其证明
    python脚本中__all__变量的用法
    洛谷 1108 低价购买
    洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
    洛谷 1365 WJMZBMR打osu! / Easy
    洛谷 2759 奇怪的函数
    洛谷 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    牛客网NOIP赛前集训营 提高组 第5场 T2 旅游
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7498522.html
Copyright © 2011-2022 走看看