正在回答
2回答
如下是w3c官网对<base>标签的解释,所以当你设置了base标签之后,实际上会改变url中的路径,设置了<base href="<%=basePath%>">这行代码是为页面上的所有链接规定默认地址或默认目标,
譬如下面的代码,设置了<base
href="<%=basePath%>">之后,原本是需要通过http://localhost8080/项目名称/LoginServlet才能访问的servlet,修改之后就可以直接使用LoginServlet进行访问了,这就是<base>标签的作用,为所有页面的链接规定默认的目标,如果我的建议解决了你的问题,请采纳,祝学习愉快~
<%@ page language="java" 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>课程后台管理系统</title> </head> <a href="LoginServlet">aaaa</a> </html>
冲哥_
2018-10-22 19:33:40
<%@ page language="java" contentType="text/html; charset=UTF-8" 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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <meta charset="utf-8"> <title>播放列表查看</title> <script type="text/javascript" src="resources/js/jquery-1.4.2.js"> </script> </head> <body> <table> <tr> <td > <input type="button" value="流行歌曲" class="a" name="song1"> </td> <td> <input type="button" value="经典歌曲" class="b" name="song2"> </td> <td> <input type="button" value="摇滚歌曲" class="c" name="song3"> </td> </tr> </table> <span id="aa">11</span><br><span id="bb">22</span><br><span id="cc">33</span><br> </body> <script> $(".a").click(function() { //单击按钮触发Ajax事件 $.ajax({ url:"<%=basePath%>/ClickServlet", type:"post", data : { name : $("input[name=song1]").val(), }, dataType:"json", success:function(result){ $("#aa").text("a"); $("#bb").text("b"); $("#cc").text("c"); } }); }); $(".b").click(function(){ //单击按钮触发Ajax事件 $.ajax({ url:"<%=basePath%>/ClickServlet", type:"post", data : { name : $("input[name=song2]").val(), }, dataType:"json", success:function(result){ $(".#aa").text("f"); $("#bb").text("e"); $("#cc").text("d"); } }); }); $(".c").click(function(){ //单击按钮触发Ajax事件 $.ajax({ url:"<%=basePath%>/ClickServlet", type:"post", data : { name : $("input[name=song3]").val(), }, dataType:"json", success:function(result){ $("#aa").text("g"); $("#bb").text("h"); $("#cc").text("i"); } }); }); </script> </html>
package com.imooc.servler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; @WebServlet("/ClickServlet") public class ClickServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); System.out.println(name); JSONObject jsonObject = new JSONObject(); response.getOutputStream().write(jsonObject.toString().getBytes("utf-8")); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
l老师,上面的代码中如果我把 <base href="<%=basePath%>"> 单击之后就会有任何变化,这是什么原因呢?
这题是Ajax入门的2-3自由编程
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星