案例:用户登录
用户登录案例需求
编写login.html登录页面
username & password 两个输入框
使用Druid数据库连接池技术,操作mysql,day14数据库中user表
使用JdbcTemplate技术封装JDBC
登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎您
登录失败跳转到FailServlet展示:登陆失败!用户名或密码错误
分析
开发步骤
创建项目
创建JavaEE项目,导入 login.html 页面,druid.properties 配置文件,导入依赖 jar 包
login.html
Login用户名
密码
druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db3
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
maxIdle=8
minIdle=3
创建数据库环境
CREATE DATABASE day14;
USE day14;
CREATE TABLE USER (
id INT PRIMARY KEY AUTO_INCREMENT, -- 编号
username VARCHAR(32) NOT NULL UNIQUE, -- 用户名
PASSWORD VARCHAR(32) NOT NULL -- 密码
);
DROP DATABASE IF EXISTS day14;
SELECT * FROM USER;
INSERT INTO USER(id,username,PASSWORD) VALUES(NULL, 'superbaby','123');
创建包 com.my.domain,创建类User
package com.my.domain;
/**
* 用户的实体类
*/
public class User {
private int id;
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建包 com.my.dao,创建类UsesrDao,提供login方法
package com.my.dao;
import com.my.domain.User;
import com.my.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* 操作数据库中 User表的类
*/
public class UserDao {
// 声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 登录方法
*
* @param loginUser 只有用户名和密码
* @return user包含用户全部数据,没有查询到,返回 null
*/
public User login(User loginUser) {
User user = null;
try {
// 1.编写sql
String sql = "select * from User where username = ? and password = ?";
// 2.调用query方法
user = template.queryForObject(sql,
new BeanPropertyRowMapper(User.class),
loginUser.getUsername(), loginUser.getPassword());
return user;
} catch (DataAccessException e) {
e.printStackTrace(); // 后期会记录在日志
return null;
}
}
}
编写 com.my.web.servlet.LoginServlet类
package com.my.web.servlet;
import com.my.dao.UserDao;
import com.my.domain.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.设置编码,防止中文乱码
req.setCharacterEncoding("utf-8");
// 2.获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
// 3.封装User对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
// 4.调用UserDao的login方法
UserDao dao = new UserDao();
User user = dao.login(loginUser);
// 5.判断user
if (null == user) {
// 登录失败
// 转发
req.getRequestDispatcher("/failServlet").forward(req, resp);
} else {
// 登录成功
// 存储数据
req.setAttribute("user", user);
// 转发
req.getRequestDispatcher("/successServlet").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
login.html中的form表单的action路径的写法
虚拟路径+Servlet资源路径
BeanUtils工具类,简化数据封装
用于封装JavaBean的
JavaBean:标准的Java类
要求:
类必须被public修饰
必须提供空参的构造器
一对getter和setter方法
成员变量必须使用private修饰
概念:
成员变量:
属性:setter和getter方法截取后的产物
例如:getUsername() --> Username --> username
方法
setProperties():
getProperties():
populate(Object obj,Map map):将map集合的键值对信息,封装到对应的JavaBean对象中,把键当作属性名称,把值当作JavaBean对应的属性的值
案例:文件下载
文件下载需求:
页面显示超链接
点击超链接后弹出下载提示框
完成图片文件下载
分析:
超链接指向的资源如果能被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。不满足需求
任何资源的下载都必须弹出下载提示框
使用响应头设置资源的打开方式:
content-disposition:attachment;filename=xxx
步骤:
定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
定义Servlet
获取文件名称(获取request参数)
使用字节输入流加载文件进内存
指定response的响应头:content-disposition:attachment;filename=xxx
将数据写出到response输出流
问题:
中文文件问题
解决思路:
获取客户端使用的浏览器版本信息
根据不同的版本信息,设置filename的编码方式不同
代码实现
download.html
DownloadServlet.java
package com.my.web.download;
import com.my.web.utils.DownLoadUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 附件下载案例
*/
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取请求参数,文件名称
String filename = request.getParameter("filename");
// 2.使用字节输入流加载文件进内存
// 2.1 获取ServletContext对象
ServletContext servletContext = this.getServletContext();
// 2.2 获取文件服务器真实路径
String realPath = servletContext.getRealPath("/img/" + filename);
// 2.3 使用字节流关联
FileInputStream fis = new FileInputStream(realPath);
// 3. 设置response的响应头
String mimeType = servletContext.getMimeType(filename); // 获取文件扩展名对应的mime类型
// 3.1 设置响应头的类型:content-type
response.setHeader("content-type", mimeType);
// 3.2 设置响应头的打开方式:content-disposition
// 解决中文文件名问题
// 1.获取user-agent请求头
String agent = request.getHeader("user-agent");
// 2.使用工具类方法编码文件名即可
filename = DownLoadUtils.getFileName(agent, filename);
response.setHeader("content-disposition", "attachment;filename=" + filename);
/*
// 简单的形式,设置编码,是在获取流之前设置
response.setContentType("text/html;charset=utf-8");
*/
// 4. 将输入流的数据写出到输出流中
ServletOutputStream sos = response.getOutputStream();
byte[] buff = new byte[1024 * 6]; // 缓冲数组
int len = 0; // 读取的有效个数
while ((len = fis.read(buff)) != -1) {
sos.write(buff, 0, len);
}
fis.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}