路径问题:
A: 客户端路径:那个斜杠("/")表示服务器的根(tomcat的webapps目录)
重定向
html中的form的action的值
html中的a标签的href的值
link标签的href的值
img中的src的值
javascript中的src的值
B:服务器端路径:那个斜杠("/")表示的是当前web应用的根
请求转发
web.xml文件中的servelt-mapping中的url-pattern的斜杠
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/oa/user/login</url-pattern>
//web.xml文件中的servelt-mapping中的url-pattern的斜杠- </servlet-mapping>
<%@ page language="java" import="java.util.*,java.text.SimpleDateFormat"
pageEncoding="UTF-8" isELIgnored="true" errorPage="/demo2.jsp" autoFlush="true" buffer="8kb"%>
Demo-Login案例
package zz.itcast.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8"); //响应去除乱码
String name=request.getParameter("name"); //根据请求参数获得相对应的属性值,常用于获取form表单数据
System.out.println(name);
//request.getRequestDispatcher("/page/info.jsp").forward(request, response); //请求转发
System.out.println(request.getContextPath()); //获得工程名或者(tomcat的webapps目录)
response.sendRedirect(request.getContextPath()+"/page/info.jsp"); //重定向
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
- }
Demo-jsp案例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/my.css">
// link标签的href的值<script src="${pageContext.request.contextPath }/js/my.js"></script>
//javascript中的src的值</head>
<body>
${pageContext.request.contextPath }
<form action="${pageContext.request.contextPath }/oa/user/login">
//html中的form的action的值<input name="name"/>
<input type="submit"/>
</form>
<a href="${pageContext.request.contextPath }/oa/user/login?name=tom">点我</a> //超链接
<img src="${pageContext.request.contextPath }/image/zp.png"/> //
img中的src的值</body>
</html>