javaweb02_HttpServletResponse
程序员文章站
2022-05-08 16:02:16
...
javaweb02_HttpServletResponse
HttpServletResponse
文件下载示例:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//文件全路径
String realPath = "D:\\eclipseworkspace\\javawebdemo\\src\\main\\resources\\头像.jpg";
//通过字符串截取获得文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
//Content-Disposition: attachment 表示消息体应该被下载到本地,filename的值指定下载后的文件名(中文需要经URLEncoder类进行编码)
resp.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));
//获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
//创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//获取输出流对象
ServletOutputStream out = resp.getOutputStream();
//使用输出流将缓冲区数据输出到客户端
while ((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
out.close();
in.close();
}
生成验证码示例:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//让浏览器3秒刷新一次
resp.setHeader("refresh", "3");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
//得到画笔
Graphics2D graphics = (Graphics2D) image.getGraphics();
//设置图片背景色
graphics.setColor(Color.white);
graphics.fillRect(0, 0, 80, 20);
//给图片写数据
graphics.setColor(Color.BLUE);
graphics.setFont(new Font(null, Font.BOLD, 20));
graphics.drawString(makeNum(), 0, 20);
//告诉浏览器这个请求用图片打开
resp.setContentType("image/jpeg");
//不让浏览器缓存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("Pragma", "no-cache");
//将图片写给浏览器
ImageIO.write(image,"jpg",resp.getOutputStream());
}
/**
* 生成7位随机数并转换为字符串
*/
private String makeNum() {
Random random = new Random();
String num = random.nextInt(9999999)+"";
StringBuffer sb = new StringBuffer();
for(int i=0;i<7-num.length();i++) {
sb.append("0");
}
return num = sb.toString()+num;
}
HttpServletResponse实现请求重定向(重点):
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("进入AServlet");
resp.sendRedirect("/javawebdemo/b");
}
请求url:http://localhost:8080/javawebdemo/a
结果页面重定向至:http://localhost:8080/javawebdemo/b
地址栏url发生了改变!
重定向还可以写成以下形式:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("进入AServlet");
resp.setHeader("Location", "/javawebdemo/b");
resp.setStatus(302);
}
重定向与请求转发的区别:
请求转发的url不会改变
重定向的url会发生改变
重定向的应用:登录逻辑
添加依赖:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
登录页面:
<html>
<body>
<h2>Login</h2>
<%-- ${pageContext.request.contextPath} 代表当前的项目路径 --%>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"/>
密码:<input type="password" name="password"/>
<input type="submit"/>
</form>
</body>
</html>
LoginServlet:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Post进入LoginServlet");
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
resp.sendRedirect("/javawebdemo/success.jsp");
}
success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
上一篇: php 使用GD库为页面增加水印
下一篇: 一个数据表中作恶的事