Servlet跳转之转向(Forward)
Servlet跳转之转向(Forward)
Servlet之间可以相互跳转,从一个Servlet跳转到另一个Servlet。利用Serlvet跳转可以很容易地把一个大的任务按模块分成较小的任务。比如我们用一个Servlet接收用户提交的数据,然后跳转到另一个Servlet中根据提交的数据读取数据库并进行业务处理,然后跳到另一个Servlet把处理结果显示出来。Servlet的跳转可以实现程序模块化。
现在的MVC(Model-View-Control)框架中都使用了Servlet跳转。MVC框架把程序分成三个独立的模块:业务处理模块(Model)、视图模块(View)控制模块(Control)。其中Model负责处理业务,View负责显示数据,Control负责控制。在Struts框架中这三部分分别为3个Servlet,程序在3个Servlet之间跳转。
转向(Forward)
转向(Forward)是通过RequestDispatcher对象的forward(HttpServletRequestrequest, HttpServletResponse response)方法来实现的。RequestDispatcher可以通过HttpServletRequest的getRequestDispatcher()方法获得。例如,下面的代码跳转到另一个Servlet:
request.getRequestDispatcher("/success.jsp").forward(request,response);
Forward是MVC框架中常用的一种技术。Forward不仅可以跳转到本应用的另一个Servlet,JSP页面,也可以跳转到另外一个文件,甚至WEB-INF文件夹下的文件。其中跳转到Servlet与JSP页面是最常见的。框架中常使用一个Servlet来集中处理请求然后跳转到响应的Servlet,或者在Servlet中处理业务逻辑,然后跳转到JSP页面中显示处理结果。下面我们来看一个例子,这个例子是ForwardServlet中根据地址栏传入的destination参数不同而跳转到不同的目的地。如果destination参数值为file,则跳转到文件/WEB-INF/web.xml;如果为servlet,则跳转到firstServlet中;如果为jsp则跳转到jsp页面forward.jsp中(也就是原网页)。
ForwardServlet.java
package cn.hanxd.servlet;
import java.io.IOException;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ForwardServlet
*/
public class ForwardServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ForwardServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("gb2312");
String destination = request.getParameter("destination");
//跳转到/WEB-INF/web.xml
if("file".equals(destination)){
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/web.xml");
requestDispatcher.forward(request, response);
}else if("jsp".equals(destination)){ //跳转到 /forward.jsp
request.setAttribute("date", new Date());
//传递一个Date对象给JSP页面
RequestDispatcher dispatcher = request.getRequestDispatcher("/forward.jsp");
dispatcher.forward(request, response);
}else if("servlet".equals(destination)){ //跳转到另一个servlet
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/FirstServlet");
requestDispatcher.forward(request, response);
}else {
response.setCharacterEncoding("gb2312");
response.getWriter().println("缺少参数。用法:" + request.getRequestURL() + "?destination=jsp或者file或者servlet");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("gb2312");
doGet(request, response);
}
}
FirstServlet.java
package cn.hanxd.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServlet
*/
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.log("执行doGet方法!");
response.getWriter().append("FirstServlet");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@Override
protected long getLastModified(HttpServletRequest req) {
// TODO Auto-generated method stub
return super.getLastModified(req);
}
}
forward.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:directive.page import="java.util.Date" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Forward 跳转</title>
</head>
<body>
<div align="center"><br/><filedset style=width:90%><legend>Forward 跳转</legend><br/>
<div style='line'>
<div align='center'>
<input type='button' onclick='location="<%=request.getContextPath() %>/ForwardServlet?destination=servlet";' value='跳转到Servlet' class=button>
<input type='button' onclick='location="<%=request.getContextPath() %>/ForwardServlet?destination=file";' value='跳转到web.xml' class=button"'>
<input type='button' onclick='location="<%=request.getContextPath() %>/ForwardServlet?destination=jsp";' value='跳转到JSP' class=button"'>
</div>
</div>
</body>
</html>
点击各个按钮后,注意页面的变化和地址栏中的变化。
当使用forward形式跳转Servlet时,地址栏会显示跳转前的Servlet访问地址。因为该跳转是在服务器端实现的,客户端浏览器并不知道该如何跳转动作。也就是说forward跳转对客户端浏览器是透明的。
Forward是最长用的方式。在Struts等MVC框架中,都是用Servlet来处理用户请求,把结果通过request.setAttribute()放到request中,然后forward到JSP中显示。
注意:在forward之前尽量不要用out.print()语句向客户端输出结果,否则会抛出IllegalStateException。
注意:在forward之前尽量不要用out.print()语句向客户端输出结果,否则会抛出IllegalStateException。
上一篇: 人工智能涉及算法