Jsp与Servlet 重定向与转发探究(一)

4 篇文章 0 订阅
3 篇文章 0 订阅

探究方式有二:

  1. jsp页面
  2. servlet实例

目录:

a. 探究过程及源代码
b. 最后总结


**

a.探究过程及源代码

**


1.jsp探究

原理方法:
创建4张jsp页面,index.jsp、f1.jsp、f2.jsp、end.jsp。

index.jsp:首页链入f1.jsp、f2.jsp。

f1.jsp:jsp转发页面。

f2.jsp:jsp重定向页面。

end.jsp:转发、重定向的最终页面。

源代码:
index.jsp 代码如下(写在了一起):

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    This is my JSP page. <br>
    <a href="f1.jsp" >request.jsp转发</a><br/>
    <a href="f2.jsp" >response.jsp重定向</a><br/>
    <a href="serreq" >request.servlet转发</a><br/>
    <a href="serresp" >response.servlet重定向</a><br/>
  </body>
</html>

f1.jsp 代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'f1.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <%request.getRequestDispatcher("end.jsp").forward(request, response); %>
  </body>
</html>

f2.jsp 代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'f2.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <%response.sendRedirect("end.jsp"); %>
  </body>
</html>

end.jsp 代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'end.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    This is my end JSP page. <br>
  </body>
</html>

首先运行项目。之后在页面单击,你将会看到效果。

重定向后的URL:
重定向后的页面

请求转发后的URL:
转发后的页面

比较结果:请求转发的URL是不变的。重定向的URL是改变的。


2.servlet探究

原理:通过对index.jsp中的超链接单击,测试servlet的请求转发与重定向。

源代码:

ServletRequest.java:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletResponse extends HttpServlet {


    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.sendRedirect(request.getContextPath()+"/end.jsp");
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }


    public void init() throws ServletException {
    }

}

ServletResponse.java:



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletRequest extends HttpServlet {

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.getRequestDispatcher("/end.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

    public void init() throws ServletException {
    }

}

web.xml配置(插入代码在之间):

  <servlet>
    <servlet-name>serresp</servlet-name>
    <servlet-class>servletForR.ServletResponse</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>serreq</servlet-name>
    <servlet-class>servletForR.ServletRequest</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>serresp</servlet-name>
    <url-pattern>/serresp</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>serreq</servlet-name>
    <url-pattern>/serreq</url-pattern>
  </servlet-mapping>

servlet请求转发结果:
servlet请求转发结果

servlet重定向结果:
servlet重定向结果

比较:servlet的请求转发URL不变,重定向URL改变。

b.最后总结

请求转发的URL是不改变的,重定向后的URL是改变的。

可是? servlet中的转发与重定向的URl是否可以随意?

请看第二篇。URL的探究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值