Servlet请求与转发详解

Servlet请求与转发在实际应用中很普遍,二者有一定的区别,接下来,我们用一个例子来说明二者的区别是什么。

我们说一下例子的思路:
首先在web工程的index.jsp页面写一个form表单,可以提交到servlet页面,然后我们获取表单中的值,将其加入request的属性中去,然后进行转发,转发到一个叫Text01.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+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting pagetitle>
    <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">
    
  head>

  <body>
    <form method="post" action="Text01">
    请输入一段话:
    <input type="text" name="text">
    <input type="submit" name="anniu">
    form>
  body>
html>

Text01.java

package Text01;

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 Text01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(req,resp);//调用doPost方法
}
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    req.setCharacterEncoding("UTF-8");//设置请求的字符集类型
    resp.setContentType("text/html;charset=utf-8");//设置响应的字符集类型
    PrintWriter out=resp.getWriter();//设置输出流
    String text = req.getParameter("text");//获取index.jsp表单中的名字叫text的值
    req.setAttribute("key", text);//将text的值添加到request的属性中去,属性名为key,值为text的值
    req.getRequestDispatcher("/Text01.jsp").forward(req, resp);//转发到Text01.jsp
    }
}

Text01.jsp

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


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'Text01.jsp' starting pagetitle>

    <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">
    

  head>

  <body>
    欢迎来到新页面:
    这是你在index.jsp页面输入的一段话:
    <%
    out.println(request.getAttribute("key"));

     %>
  body>
html>

web.xml


<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>display-name> 
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  <servlet>
  <servlet-name>HelloWorldservlet-name>
  <servlet-class>Text01.Text01servlet-class>
  servlet>
  <servlet-mapping>
  <servlet-name>HelloWorldservlet-name>
  <url-pattern>/Text01url-pattern>
  servlet-mapping>
web-app>

转发的特点是:1.转发后的路径还是原来的,比如你从index.jsp转发到了Text01.jsp,但是你会发现,路径还是index.jsp
2.转发会把请求与响应一块携带着,你可以从转发后的页面进行访问到。

接下来就是跳转啦,我把名字叫Text01的servlet进行了下修改,代码如下:

package Text01;

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 Text01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(req,resp);//调用doPost方法
}
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    req.setCharacterEncoding("UTF-8");//设置请求的字符集类型
    resp.setContentType("text/html;charset=utf-8");//设置响应的字符集类型
    PrintWriter out=resp.getWriter();//设置输出流
    String text = req.getParameter("text");//获取index.jsp表单中的名字叫text的值
    req.setAttribute("key", text);//将text的值添加到request的属性中去,属性名为key,值为text的值
    resp.sendRedirect("/Text/Text01.jsp");//进行跳转
    }
}

这次,我们会发现,跳转后的页面的输入内容那里会显示一个null.

我们来说一下跳转的特点吧:
1.跳转不携带请求与响应
2.跳转后的路径,会显示新的路径,比如你从index.jsp跳转到Text01.jsp,路径会显示Text01.jsp
3.跳转代码的路径要从localhost:8080开始记起

通俗的来讲,假如你需要办点事,你拿着礼物去请甲方办事,甲方办不了,但把你的礼物收下了,替你去乙方把事情给办好了,这就是转发。

而如果甲方收下了礼物,发现自己办不了,就告诉你要去乙方办事。而你去乙方,还得再送一次礼给乙方,这就是跳转。

你可能感兴趣的:(JSP技术帖)