javaWeb请求的转发

请求的转发:
什么是请求的转发?
请求的转发是指:服务器收到请求后,从一次资源跳转到另一个资源的操作叫请求转发

 

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hyong~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值