day17-Servlet06
Servlet0615.HttpServletResponse
15.1HttpServletResponse介绍
[*]每次HTTP请求,Tomcat都会创建一个HttpServletResponse对象传递给Servlet程序使用
[*]HttpServletRequest表示请求过来的信息,HttpServletResponse表示所有响应的信息,如果需要设置返回给客户端的信息,通过HttpServletResponse对象来设置即可。
[*]HttpServletResponse类图
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113172438469.pnghttps://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113172538284.png https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113172610605.png
[*]向客户端返回数据方法:
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113173334168.png
[*]字节流getOutputStreamWriter(); 常用于下载(处理二进制数据)
[*]字符流getWriter(); 常用于回传字符串
[*]两个流同时只能使用一个,使用了字节流,就不能使用字符流,反之亦然,否则就会报错。
例子
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113173637791.png15.2返回数据时注意事项和细节
[*]处理中文乱码问题-方案1(推荐)
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113173929212.pngresponse.setContentType("text/html;charset=utf-8");
[*]处理中文乱码问题-方案2
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113173842325.png//1.设置服务器使用utf-8
response.setCharacterEncoding("utf-8");
//2.设置浏览器端是utf-8,而且类型是text/html
response.setHeader("Content-Type","text/html;charset=utf-8");
15.3请求重定向
15.3.1请求重定向介绍
请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/%E8%AF%B7%E6%B1%82%E9%87%8D%E5%AE%9A%E5%90%91%E7%A4%BA%E6%84%8F%E5%9B%BE.png请求重定向的地址栏会改变
15.3.2请求重定向应用实例
需求:演示请求重定向的使用,当访问DownServlet下载文件,重定向到DownServletNew下载文件
DownServlet:
package com.li.servlet.response;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = {"/downServlet"})
public class DownServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//完成自己的业务..
//请求重定向->downServletNew
//1.sendRedirect本质 会返回一个 302的状态码 和一个Location:/servlet_demo/downServletNew
//2.因此302和location是浏览器解析的,而不是服务器
//3.浏览器会将/servlet_demo/downServletNew 解析成
//=>http://localhost:8080/servlet_demo/downServletNew
//浏览器不能知道服务器上的web应用名称,只能将当前所在页面的主机名和端口拼上来,
// 所以重定向需要把/web应用名也写上来
//而请求转发是在服务器端的,可以获取到web应用名称,所以请求转发直接写/资源名即可
response.sendRedirect("/servlet_demo/downServletNew");
}
}DownServletNew:
package com.li.servlet.response;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = {"/downServletNew"})
public class DownServletNew extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DownServletNew-doPost()被调用..");
response.setContentType("application/x-tar;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("ok");
writer.flush();
writer.close();
}
}down.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载文件</title>
</head>
<body>
<h1>下载文件</h1>
<a target="_blank" href="http://localhost:8080/servlet_demo/downServlet">天龙八部</a>
</body>
</html>https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/ds.gif15.3.3请求重定向注意事项和细节
[*]最佳应用场景:网站迁移,比如原域名是www.abc.com,迁移到www.xyz.com,但是百度抓取的还是原网址
[*]浏览器地址会发生变化,本质上是两次http请求
[*]不能共享Request域中的数据,本质是两次http请求,因此会产生两个httpServletRequest对象
[*]不能重定向到/WEB-INF下的资源
[*]可以重定向到Web工程之外的资源,比如到 www.baidu.com
[*]重定向有两种方式,推荐使用第一种
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113192234822.pnghttps://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113192753508.png
[*]动态获取到application context(即Tomcat中配置的web应用项目名称)
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113193127441.png//动态获取到application context
String contextPath = getServletContext().getContextPath();
//contextPath =>/servlet_demo
response.sendRedirect(contextPath+"/downServletNew");
15.3.4练习
https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113194157745.pnghttps://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/image-20221113194219288.png
[*]编写一个MyPayServlet,能够接收到提交的数据
[*]编写一个简单的html页面pay.html
[*]如果支付金额大于100,则重定向到payok.html,否则重定向到原来的pay.ok
WebUtils:
package com.li.servlet.response.homework;
//编写一个String-->int的方法,并处理可能的异常
public class WebUtils {
public static int parseString(String str) {
int num = 0;
try {
//try-catch快捷键:ctrl+alt+t
num = Integer.parseInt(str);
} catch (NumberFormatException e) {
//这个异常不会throw给Tomcat
System.out.println("输入的str格式不正确...");
//如果输入的格式不正确,num的值还是0
}
return num;
}
}MyPayServlet:
package com.li.servlet.response.homework;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(urlPatterns = {"/myPayServlet"})
public class MyPayServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到金额
String moneyCount = request.getParameter("moneyCount");
//转成int
//处理一下异常
int i = WebUtils.parseString(moneyCount);
if (i > 100) {
//重定向到payok.html
response.sendRedirect(getServletContext().getContextPath() + "/payok.html");
} else {
//重定向到pay.html
response.sendRedirect(getServletContext().getContextPath() + "/pay.html");
}
}
}pay.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>支付页面</title>
</head>
<body>
<h1>支付页面</h1>
<form action="/servlet_demo/myPayServlet" method="post">
用户编号:<input type="text" name="userId"/><br/>
支付金额:<input type="text" name="moneyCount"/><br/>
<input type="submit" value="点击支付">
</form>
</body>
</html>payok.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>支付成功</title>
</head>
<body>
<h1>恭喜你,支付成功~</h1>
</body>
</html>https://liyuelian.oss-cn-shenzhen.aliyuncs.com/imgs/%E5%8A%A8%E7%94%BB%20(1).gif
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]