servlet:
目录结构:
/*
url-pattern定义匹配规则,取值说明:
精确匹配 /具体的名称 只有url路径是具体的名称的时候才会触发Servlet
后缀匹配 *.xxx 只要是以xxx结尾的就匹配触发Servlet
通配符匹配 /* 匹配所有请求,包含服务器的所有资源
通配符匹配 / 匹配所有请求,包含服务器的所有资源,不包括.jsp
*/
@WebServlet(name = "PathServlet",urlPatterns = "/pathservlet")
public class PathServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向(只能使用根路径,相对路径,绝对路径) 跳转到login.html
response.sendRedirect("login.html"); //相对路径 相对于pathservlet是同级目录
response.sendRedirect("/0902/login.html"); // 重定向属于客户端行为 最左边斜杠表示是主机地址 0902表示此项目的访问路径(下面截图有介绍)
response.sendRedirect("http://localhost:8080/0902/login.html"); //绝对路径
//转发(只能使用根路径,相对路径)
request.getRequestDispatcher("login.html").forward(request, response); //相对路径 相对于pathservlet是同级目录
request.getRequestDispatcher("/login.html").forward(request, response); //根路径 由于转发属于服务端行为,所以最左边斜杠表示项目访问路径
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
如何查看和配置项目访问路径:
html页面中路径:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--此为客户端行为所以action中的第一个斜杠表示服务器域名加端口号-->
<form action="/0902/loginservlet" method="post">
</form>
</body>
</html>