关于response的疑问
关于该作业的疑问:
CheckNumServlet(检查servlet) 和 ShowServlet(显示servlet)共享同一个request,这个没有疑问,那关于response呢,也是共享同一个吗?
如果是,那么为啥以下CheckNumServlet 中的 this is check page! 没有输出
如果不是,那么ShowServlet输出的中文,我发现在CheckNumServlet 中response设置setContentType对其有效,在ShowServlet设置字符集反而还是乱码是啥原因?
创建表单: sum.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Request-Struc/direct/check">
<input type="text" name="num" placeholder="请输入100以内的数字"/>
<input type="submit" value="求累加和"/>
</form>
</body>
</html>
创建处理servlet: CheckNumServlet
package com.imooc.servlet.direct;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckNumServlet
*/
@WebServlet("/direct/check")
public class CheckNumServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckNumServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("this is check page!");
String num=request.getParameter("num");
request.setAttribute("num", num);
request.getRequestDispatcher("/direct/show").forward(request, response);;
}
}
创建显示servlet: ShowServlet
package com.imooc.servlet.direct;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ShowServlet
*/
@WebServlet("/direct/show")
public class ShowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String num=(String)request.getAttribute("num");
// response.setContentType("text/html;charset=utf-8");
response.getWriter().println("this is sum page!<br/>");
int n=Integer.parseInt(num);
int sum=0;
//求累加和
for(int i=1;i<=n;i++) {
sum+=i;
}
response.getWriter().println(n+"的累加和为:"+sum);
}
}
正在回答
同学你好,
1、response是同一个,因为在CheckNumServlet中请求转发传入了response。
2、请求转发前后CheckNumServlet写入的数据应该是发送给客户端了,只不过由于请求被转发了,客户端的所有显示都是由转发后的servlet生成的,转发前的servlet产生的客户端输出也会被转发后的servlet的客户端输出覆盖,表面上看起来转发前的servlet没有输出。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星