利用EL和basePath拼接请求URI

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaouncle/article/details/80470106

我们有多种方式在前台jsp页面拼写请求URL,这里记录两种常用的方法,以备日后查看。

1、EL表达式拼接请求URI

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
	index.jsp
	<%-- 利用EL表达式指定URI --%>
	<form action="${pageContext.request.contextPath }/test/login.action" method="post">
		姓名:<input type="text" name="userName"/><br>
		年龄:<input type="text" name="age"/><br>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

2、basePath拼接请求URI

注意:<form>中的action必须是“test/login.action”,不能是“/test/login.action”

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- 利用basePath指定URI --%>
<base href="<%=basePath%>">
<title>index</title>
</head>
<body>
	<form action="test/login.action" method="post">
		姓名:<input type="text" name="userName"/><br>
		年龄:<input type="text" name="age"/><br>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>


猜你喜欢

转载自blog.csdn.net/xiaouncle/article/details/80470106