js中获取 <%=request.getContextPath()%>

本文介绍如何在JavaScript中获取项目的basePath和contextPath,包括通过window.location对象解析路径的方法,并提供两种不同环境下的实现方案。
在jsp中获取context path或者basePath 是很容易的。在javascript中也可以获取,并且有的时候也很有必要。

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

在单独的javascript中不能使用<%= basePath%>类似这样的java  script所以,只能用javascript来获取此context path.
可以用下面的代码来实现。

var localObj = window.location;

var contextPath = localObj.pathname.split("/")[1];

var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;

var server_context=basePath;

两种实现的方法用在不同的环境中。





${pageContext.request.contextPath},是获取当前根目录,${ctx}为${pageContext.request.contextPath}的简写版,
<c:setvar="ctx"value="${pageContext.request.contextPath}"/>


<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8” %> <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <!-- <link rel="stylesheet" href="<%=request.getContextPath() %>/css2/index2.css" type="text/css"></link> --> <link rel="stylesheet" href="<%=request.getContextPath() %>/css/theme.blue.css" type="text/css"></link> <body> <form action="<%=request.getContextPath() %>/g?m=getList" method="post"> <table id="myTable" class="tablesorter-blue"> <thead> <tr> <th>邮箱:<input type="text" name="yname" id="name" onblur="you()"><span id="MyName"></span></th> </tr> <tr> <th>密码:<input type="text" name="ymima" id="ymima" onblur="mi()"><span id="MyMiMa"></span></th> </tr> <tr> <th><input type="button" value="登陆" onclick="deng()"></th> </tr> </thead> <tbody> </tbody> </table> </form> </body> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/random.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.tablesorter.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/My97DatePicker/WdatePicker.js"></script> <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script> <script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript"> function mi(){ let name=$("#ymima").val() if (name!=null&&name.trim().length){ let y=/^[A-Za-z]{6,10}$/ if (y.test(name)){ $("#MyMiMa").html("<font color='lime'>√</font>") }else { $("#MyMiMa").html("<font color='#dc143c'>密码必须为6-10位字母和数字组合</font>") } }else { $("#MyMiMa").html("<font color='#dc143c'>密码不可为空</font>") } } function you(){ let name=$("#name").val() if (name!=null&&name.trim().length){ let y=/^[A-Za-z\d]+@[A-Za-z\d]+\.(com)$/ if (y.test(name)){ $("#MyName").html("<font color='lime'>√</font>") }else { $("#MyName").html("<font color='#dc143c'>请输入有效的邮箱地址</font>") } }else { $("#MyName").html("<font color='#dc143c'>邮箱不可为空</font>") } } function deng(){ $.post("<%=request.getContextPath()%>/my?my=deng", $("#myTable").serialize() ) } </script> </html> 这是全部前端你看看对么?
最新发布
12-24
<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%> <%@ page import=“javaBean.Student”%> <% 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 'StudentList.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> <h2 align="center">学生管理系统</h2> <table align="center" border="1px"> <tr> <td>学号</td> <td>姓名</td> <td>年龄</td> <td align="center">操作</td> </tr> <% List slist= (ArrayList)request.getAttribute("sList"); //获取servlet传过来的学生数据 for(int i=0;i<slist.size();i++){ Student s = (Student)slist.get(i); %> <tr> <td><%=s.getSid()%></td> <td><%=s.getSname()%></td> <td><%=s.getSage()%></td> <td><a href="Student.jsp">添加<a href="Update.jsp">修改<a href="Delete.jsp">删除</td> </tr> <% } %> </table> </body> </html>现在得在这个view层写一个点击删除弹出一个弹窗,这个该怎么写,给点思路-这个没怎么学,我记得是一个confirm的关键字
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值