1. 程式人生 > >Servlet中include方法和forward方法的區別

Servlet中include方法和forward方法的區別

/*login.jsp*/

<%@ page contentType="text/html;charset=GBK" language="java" pageEncoding="GBK"%>

<html>
<head>
<%
 request.setCharacterEncoding("GBK");
%>

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>

<body>

<form action="login.do" name="LoginForm" method="get">


使用者名稱:<input type="text" name="uname"><br>
密  碼:<input type="password" name="upwd"><br>
<input type="hidden" name="uid" value="432022199008081888">
<input type="submit" value="登陸">
<input type="reset" value="重置">
</form>
</body>
</html>

/*LoginServlet*/

package com.session;

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

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

public class LoginServlet extends HttpServlet {

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

  response.setContentType("text/html; charset=GBK");
  request.setCharacterEncoding("GBK");
  response.setCharacterEncoding("GBK");
  
  String uname = request.getParameter("uname");
  String upwd = request.getParameter("upwd");
  String uid = request.getParameter("uid");
  RequestDispatcher rd = request.getRequestDispatcher("forward.do");
 
  if ("".equals(uname) || "".equals(upwd)) {
   response.sendRedirect("bad.html");
  } else {
   rd.forward(request, response);
  }

 }

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

/*ForwardServlet*/

package com.session;

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

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

public class ForwardServlet extends HttpServlet {

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

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  
  request.setCharacterEncoding("GBK");  
  String uname = request.getParameter("uname");
  String upwd = request.getParameter("upwd");
  String uid = request.getParameter("uid");
  
  out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  
  out.println("uname: " + uname + "<br>");
  out.println("upwd: " + upwd + "<br>");
  out.println("uid: " + uid + "<br>");  
  RequestDispatcher rd = request.getRequestDispatcher("include.do");
  rd.include(request, response);

  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

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

}

/*IncludeServlet*/

package com.session;

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 IncludeServlet extends HttpServlet {

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

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is a forward servlet");
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

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

/*

其實Servlet的forward方法就是將請求繼續往下傳,因此在這個過程中,傳遞的引數一直都是可以訪問到的

include的方法其實是將另一個servlet中的輸出內容包含進來

*/