Request
Request继承体系
Request获取请求数据
获取请求数据
通用方式获取请求参数
@WebServlet("/req1")
public class req1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("get...");
// 获取所有参数的Map集合
Map<String, String[]> parameterMap = req.getParameterMap();
for (String s : parameterMap.keySet()) {
System.out.print(s+":");
String[] strings = parameterMap.get(s);
for (String string : strings) {
System.out.print(string+" ");
}
System.out.println();
}
// 根据key获取参数值。数组
System.out.println("----------------");
String[] hobbies = req.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
// 根据key获取单个参数值
System.out.println("-------------------");
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
请求参数中文乱码
@WebServlet("/req2")
public class req2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
username = new String(username.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
this.doGet(request, response);
}
}
Request请求转发
Response
Response设置响应数据功能介绍
Response完成重定向
目录问题
动态获取虚拟目录:
Response响应字符数据
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置解析方式,和字符集
//response.setHeader("content-type","text/html");
response.setContentType("text/html;charset=utf-8");
// 获取流
PrintWriter writer = response.getWriter();
writer.write("<h1>侯一鸣</h1>");
}
Response响应字节数据
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 读取文件
FileInputStream fis = new FileInputStream("d://桌面/1.png");
// 获取response的输出流
ServletOutputStream os = response.getOutputStream();
// 完成流的拷贝
// byte[] bytes = new byte[1024];
// int len = 0;
// while ((len = fis.read(bytes)) != -1){
// os.write(bytes,0,len);
// }
IOUtils.copy(fis,os);
fis.close();
}
案例
用户登录
编写LoginServlet方法
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
//调用maybatis查询
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.select(username, password);
sqlSession.close();
// 判断
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
if(user != null){
writer.write("登陆成功");
}else{
writer.write("登陆失败");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
用户注册
registerServlet
@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User u = mapper.selectByUsername(username);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
if (u == null){
mapper.add(user);
sqlSession.commit();// 增删改需要提交事务
sqlSession.close();
writer.write("注册成功");
}else{
writer.write("用户名已存在");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
SqlSessionFactory工具类抽取
package com.houyiming.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
// 静态代码块会随着类的加载自动执行,且只执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
可以用一行代码代替原来的三行
// 原来的三行
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 新的一行
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
= "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 新的一行
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();