学习视频来自于:秦疆(遇见狂神说)Bilibili地址
他的自学网站:kuangstudy
我们缺乏的不是知识,而是学而不厌的态度
web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest
对象,代表响应的一个HttpServletResponse;
负责向浏览器发送数据的方法
servletOutputstream getOutputstream() throws IOException;
Printwriter getwriter() throws IOException;
负责向浏览器发送响应头的方法
void setCharacterEncoding(String var1);
void setCharacterEncoding("utf-8");
void setContentLength(int var1);
void setContentLengthLong(long var1);
void setContentType(String var1);
void setContentType("text/html;charset=UTF-8");
void setDateHeader(String varl,long var2);
void addDateHeader(String var1,long var2);
void setHeader(String var1,String var2);
void addHeader(String var1,String var2);
void setIntHeader(String var1,int var2);
void addIntHeader(String varl,int var2);
步骤
// 指定浏览器字符集编码
response.setContentType("text/html;charset=UTF-8");
//1. 要获取下载文件的路径(路径中空格被替换成%20)
String path = this.getServletContext().getClassLoader().getResource("别错过(DJ阿卓版) - 程佳佳.flac").getPath();
//MIME格式解码器(将%20变成空格)
path = URLDecoder.decode(path, "UTF-8");
File file = new File(path);
System.out.println("文件路径:" + file);
//2. 获取下载文件名称,去掉中间包含的空格(MIME格式编码器会将空格变成+)
String name = path.substring(path.lastIndexOf("/") + 1).replace(" ", "");
System.out.println("文件名:" + name);
//3. 设置想办法让浏览器能够支持下载我们需要的东西
// MIME格式编码器解决浏览器中文乱码
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
//4. 获取下载文件的输入流
FileInputStream fis = new FileInputStream(file);
//5. 创建缓冲区
int len = 0;
byte[] bufer = new byte[1024];
//6. 获取OutputStream对象
ServletOutputStream out = response.getOutputStream();
//7. 将FileOutputStream流写入到bufer缓冲区
while ((len = fis.read(bufer)) > 0) {
//8. 使用OutputStream将缓冲区中的数据输出到客户端!
out.write(bufer, 0, len);
}
//9.释放资源
fis.close();
out.close();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//如何让浏览器3秒自动刷新一次;
response.setHeader("refresh","3");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D g = (Graphics2D) image.getGraphics(); //笔
//设置图片的背景颜色
g.setColor(Color.white);
g.fillRect(0,0,80,20);
//给图片写数据
g.setColor(Color.BLUE);
g.setFont(new Font(null,Font.BOLD,20));
g.drawString(makeNum(),0,20);
//告诉浏览器,这个请求用图片的方式打开
response.setContentType("image/jpeg");
//网站存在缓存,不让浏览器缓存
response.setDateHeader("expires",-1);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
//把图片写给浏览器
ImageIO.write(image,"jpg", response.getOutputStream());
}
//生成随机数
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");
}
num = sb.toString() + num;
return num;
}
两次请求,浏览器地址栏改变,
void sendRedirect(String var1) throws IOException;
用户登录jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Login" method="post">
<table border="1" width="300">
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
用户登录java
ServletContext context = this.getServletContext();
User user = null;
LoginService loginService = new LoginService(); // 创建Service层
String username = req.getParameter("username"); // 获取用户名
String password = req.getParameter("password"); // 获取密码
user = loginService.LoginService(username,password); // 查询数据库
if(user.getUsername() != null && user.getPassword() != null) {
System.out.println(user);
resp.sendRedirect(context.getContextPath() + "/success.jsp"); // 重定向到登录成功页
}else{
resp.sendRedirect(context.getContextPath()); // 失败重新输入
}