通过html访问servlet
如果HTML和servlet访问路径相同
比如:
访问HTML的路径是:
http://localhost:8080/requestresponse/1.html
访问servlet的路径是:(已在XML中配置servlet的访问路径为hello)
http://localhost:8080/requestresponse/hello
那么在HTML中的a标签中就可以直接写:
<a href=./hello>点击访问</a>
或者:
<a href =hello> 点击访问servlet</a>
HTML中的代码:
<!DOCTYPE html>
<html>
<head>
<title>1.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h1>webroot下的1.html访问相对路径的servlet</h1>
<!--
1. servlet的路径:
http://localhost:8080/requestresponse/hello
1.html的路径
http://localhost:8080/requestresponse/1.html
2.
所以这里用.代表上级一样的目录
./hello
./1.html
-->
<a href="./hello">get in !</a><br/>
<!-- 或者这种方式:目录层级相同 直接访问 不加/ -->
<a href="hello">get in !</a><br/>
</body>
</html>
如果HTML的路径是在webroot文件下的dir1文件夹中而不是在根目录
那么利用HTML访问servlet的时候,就要想上退一层目录
HTML中的代码:
<body>
< <h1>webroot下的1.html访问相对路径的servlet</h1>
<!--
1. servlet的路径:
http://localhost:8080/requestresponse/hello
1.html的路径
http://localhost:8080/requestresponse/dir1/1.html
2.
所以这里用..想上退一层目录
./hello
./dir1/1.html
-->
<a href="../hello">get in !</a><br/>
</body>