请求的转发:
什么是请求的转发?
请求的转发是指:服务器收到请求后,从一次资源跳转到另一个资源的操作叫请求转发
Servlet程序1:public class Servlet1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求的参数(办理的材料)查看 System.out.println(request.getParameter("username")); //给材料盖一个章,并传递到counter2(柜台二)怎么走 request.setAttribute("key1","counter1"); // 问路 counter2(柜台2)怎么走 //请求转发必须药以 / 斜杆开头,斜杠表示是:http://ip:port/工程名/ 映射到idea代码的web目录//getRequestDispatcher().forward(request,response) 要跳转到哪里 调用这个方法 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/counter2"); //counter2(柜台2) requestDispatcher.forward(request,response); } }
servlet程序2:
public class Servlet2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("username")); if(request.getAttribute("key1")==null){ System.out.println("请先去counter1办理证件"); }else{ System.out.println("办理成功"); } }
base标签:
可以设置当前页面中所有相对路径工作时,参照哪个路径来进行跳转
public class Servlet3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("经过了C"); request.getRequestDispatcher("/a/b/c.html").forward(request,response); } }
具体主页:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/JavaWeb1_war_exploded/Test" method="post">
User:<input value="username" placeholder="name" type="text" name="name"><br/>
Password:<input value="password" type="password" name="password"><br/>
Hobby:<input type="checkbox" checked="checked" name="hobby" value="c++">c++
<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="JS">javaScript
<input type="checkbox" name="hobby" value="C#">C#<br/>
<input type="submit" value="提交">
</form>
<a href="a/b/c.html">正常去C</a>
<!-- base标签:可以设置当前页面中所有相对路径工作时,参照哪个路径来进行跳转-->
<a href="http://localhost:8080/JavaWeb1_war_exploded/servlet3">发送请求去C</a>
</body>
</html>
副页面:放在a/b的文件夹中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- base标签:可以设置当前页面中所有相对路径工作时,参照哪个路径来进行跳转-->
<base href="http://localhost:8080/JavaWeb1_war_exploded/a/b/">
</head>
<body>
<a href="../../HttpTest.html">去主页</a>
</body>
</html>
<base href="http://localhost:8080/JavaWeb1_war_exploded/a/b/">
如果不在c.html放上面的代码
则通过发送请求的方式去c 路径为:
http://localhost:8080/JavaWeb1_war_exploded/servlet3
http://localhost:8080/JavaWeb1_war_exploded/servlet3/HttpTest.html
返回上2级后变为:http://localhost:8080/index.html
则会报错
这就是base标签的作用
具体的xml文件:
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>Jump.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/counter1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>Jump.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/counter2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet3</servlet-name>
<servlet-class>Jump.Servlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet3</servlet-name>
<url-pattern>/servlet3</url-pattern>
</servlet-mapping>