解决中文乱码
//中文乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); 设置后端给前端发送的数据格式类型
response.setContentType("text/json;charset=utf-8");//响应json格式的数据
response.setContentType("text/html;charset=utf-8");//响应字符串格式的数据 获取前端传的参数值
String searchname = request.getParameter("searchname"); 给前端响应数据
response.getWriter().write(""); 了解jdbc操作以及使用jdbc工具类,首先要引入java操作mysql数据库的jar包
常用的基本sql语句
select * from 表 //查询 insert into 表(字段,...) values(值,...) //添加 delete from 表 where id=1 //删除 update 表 set 字段=值,... where id = 1 //修改 select * from 表 limit 起始值,数量 //限制查询 select * from 表 where name like "%关键字%" //模糊查询 select * from 表 where 1=1 and name like "%关键字%" //万能公式、多条件综合查询 select * from 表 order by 字段 desc|asc //排序 desc降序,asc升序 select * from 表 where id in() //范围查询
WEB-INF目录的有限访问,不能通知前端跳转来访问其中的资源
后端跳转的方式:
请求转发
request.getRequestDispatcher("页面地址").forward(request, response);
重定性
response.sendRedirect("页面地址") 过滤器(在servlet处理之前,先拦截请求,起到过滤作用)
//URI是统一资源定位标志符 URL是以路径的方式来实现资源定位的一种方式
//获取URI
String requestURI = ((HttpServletRequest)request).getRequestURI();
System.out.println(requestURI);
if(requestURI.contains("login") || requestURI.contains("resource")) {
chain.doFilter(request, response);//放行
}else {
//获取的登录状态
Object username = ((HttpServletRequest)request).getSession().getAttribute("username");
if(username!=null) {//登录
chain.doFilter(request, response);//放行
}else {//没有登录
//request.getRequestDispatcher("login.html").forward(request, response);
((HttpServletResponse)response).sendRedirect("login.html");
}
} 服务端使用session来存取共享数据
request.getSession().getAttribute("名称");//从session中取数据
request.getSession().setAttribute("名称","值");//往session中存数据
request.getSession().removeAttribute("名称");//从session中删除数据 利用java中的if分支语句来实现动态sql语句,从而到达用户综合查询
分页的后端实现原理: limit (page-1)*pagesize,pagesize,考虑前端传递来的数据都是字符串,要进行算术运算,需要强转成数字,比如Integer.parseInt("8")把字符串8转化成int类型的8
多表查询场景实现原理
select * from A表,B表,... where 条件
select * from A表 inner join B表 on 条件
inner join 内连接
left join 左连接
right join 右连接



