如果项目中路径如:http://localhost:8080/RenRenDemo/login 就要注意了 ,这里项目路径含有二级目录RenRenDemo,使用相对路径就要注意了 ,login 是servlet的 url-parttem ,
1. 在 jsp页面中 你可能这样写 action里面是使用的相对地址
<form method="post" action="/login">
<label for="username">用户名:</label><input id="username" type="text" name="username"/><br/>
<label for="password">密码:</label><input id="password" type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
你会发现点击 页面会跳到这个地址:http://localhost:8080/login 这完全不是我们想要的 ,解决这个办法是 加上绝对路径
<form method="post" action="${pageContext.request.contextPath}/login">
这样页面就会跳转到我们正确的地址
2. 在servlet中 同样存在这个问题
response.sendRedirect("/profile");
当使用redirect 方式跳转时同样出现该问题 页面会跳转到 :http://localhost:8080/login 解决方法
response.sendRedirect(request.getContextPath()+"/profile");
不过用forward 跳转方式不存在该问题
RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/login.jsp");
welcomeDispatcher.forward(request, response);
3. 如果项目不存在二级目录 或者更多的 目录 则可以使用相对路径