本章博客的知识点:
1、字节输出流和字符输出流的共存问题
2、转发时,两个servlet都输出信息的问题
详细知识,见OneServlet和TwoServlet源代码中的注释
转发:传参,访问顺序(doGet还是doPost)
3、重定向:传参,访问顺序(doGet还是doPost)
4、请求包含:传参,访问顺序(doGet还是doPost)
有些演示,不怎么好分段用博客写处理,如果想加深理解的,最好自己取写一遍。
1、字节输出流和字符输出流的共存问题
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <!-- 1、字节输出流和字符输出流的共存问题---不能共存(跟get或post方式没关系)。即 :在同一个servlet响应中,不能同时采两种输出流。 另外:如果要用字节流输出中文,用write(byte[])方法 --> <a href="servlet/OutServlet">字节输出流和字符输出流的共存问题</a> <form action="servlet/OutServlet" method="post"> <input type="text" name="name" /><br /> <input type="submit"value="提交" /> </form> </body> </html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <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> <servlet> <servlet-name>OutServlet</servlet-name> <servlet-class>cn.hncu.servlet.OutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OutServlet</servlet-name> <url-pattern>/servlet/OutServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
OutServlet.java:
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class OutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); //1、用字节输出流向客户端写信息 ServletOutputStream out = response.getOutputStream();//获取字节输出流 out.print("HELLO--doGet");//全部写英文是没有问题的 //out.print("你好");//500错误。出异常,因为内部是用iso8859-1读取,已经写死了,因此中文不行 out.write("你好".getBytes("utf-8"));//如果中文要用字节流输出,用write(byte[])方法,而且最好给编码方式 //2、同时用字符输出流向客户端写信息-也是出现500错误。 PrintWriter out2 = response.getWriter(); out2.print("hello,你好"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); //1、用字节输出流向客户端写信息 ServletOutputStream out = response.getOutputStream();//获取字节输出流 out.print("HELLO--doGet");//全部写英文是没有问题的 //out.print("你好");//500错误。出异常,因为内部是用iso8859-1读取,已经写死了,因此中文不行 out.write("你好".getBytes("utf-8"));//如果中文要用字节流输出,用write(byte[])方法,而且最好给编码方式 //2、同时用字符输出流向客户端写信息-也是出现500错误。 PrintWriter out2 = response.getWriter(); out2.print("hello,你好"); } }
小总结:
大家自己写的时候,注意知道把哪里注释了,同时写了response.getOutputStream();//获取字节输出流和response.getWriter();-是不能共存的,也就是说,只能写其中一个!!!
2、转发时,两个servlet都输出信息的问题、传参,访问顺序(doGet还是doPost)
详细知识,见OneServlet和TwoServlet源代码中的注释
index.jsp:
<a href="servlet/OneServlet">转发时,两个servlet都输出信息的问题、传参问题、访问顺序问题</a><br /> <form action="servlet/OneServlet" method="post"> <input type="text" name="name" /><br /> <input type="submit"value="提交" /> </form>
web.xml:
<servlet> <servlet-name>OneServlet</servlet-name> <servlet-class>cn.hncu.servlet.OneServlet</servlet-class> </servlet> <servlet> <servlet-name>TwoServlet</servlet-name> <servlet-class>cn.hncu.servlet.TwoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OneServlet</servlet-name> <url-pattern>/servlet/OneServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>TwoServlet</servlet-name> <url-pattern>/servlet/TwoServlet</url-pattern> </servlet-mapping>
OneServlet.java:
package cn.hncu.servlet; 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 OneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("你好...OneServlet..."); //out.flush(); //Tomcat对于同response,只输出一次(把缓存中的内容刷出去)。刷了(close也一样),流就关闭了,下面的转发就无法进行了,因为此时response已经提交了(整个转发链只会响应一次,即提交了) //传参---设置属性(只要key不同,随便存几个)---放入request对象中 request.setAttribute("age", "23-OneServlet"); RequestDispatcher rd = request.getRequestDispatcher("/servlet/TwoServlet"); rd.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("你好...OneServlet..."); //out.flush(); //Tomcat对于同response,只输出一次(把缓存中的内容刷出去)。刷了(close也一样),流就关闭了,下面的转发就无法进行了,因为此时response已经提交了(整个转发链只会响应一次,即提交了) //传参---设置属性(只要key不同,随便存几个)---放入request对象中 request.setAttribute("age", "23-OneServlet"); RequestDispatcher rd = request.getRequestDispatcher("/servlet/TwoServlet"); rd.forward(request, response); } }
TwoServlet.java:
package cn.hncu.servlet; 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 TwoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//如果前一个转发过来的Servlet已经设了,可以不用再写 PrintWriter out = response.getWriter(); out.println("get...<br/>"); out.print("周末愉快。。。<br/>"); //第二个servlet会先把缓存中之前的内容情空,然后再把当前servlet的输出内容写入缓存,刷出去。 String name = (String) request.getAttribute("age"); out.print("name:"+name+"<br/>"); out.flush();//写在这里是没有什么影响的 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//如果前一个转发过来的Servlet已经设了,可以不用再写 PrintWriter out = response.getWriter(); out.println("post...<br/>"); out.print("周末愉快。。。<br/>"); //第二个servlet会先把缓存中之前的内容情空,然后再把当前servlet的输出内容写入缓存,刷出去。 String name = (String) request.getAttribute("age"); out.print("name:"+name+"<br/>"); out.flush();//写在这里是没有什么影响的 } }
小总结:
转发时,两个servlet都输出信息的问题
详细知识,见OneServlet和TwoServlet源代码中的注释
传参,访问顺序(doGet还是doPost) —转发是共享同一个request和同一个response对象的
1)第一个是doGet,第二个走的也是doGet
2)第一个是doPost,第二个走的也是doPost
(因为request传过来的参数一样,访问方式也是一样的)
3)传参:可以通过request.setAttribute()设置,通过request.getAttribute()获取 —doGet或doPost都一样