1.过滤器Filter
按照过滤规则筛选出想要的资源
很多地方都需要判断是否登录,对每个资源进行判断,非常麻烦,可以使用过滤器在访问这些资源前进行判断。
案例:
package com.ghx.filter;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {//初始化 只会执行一次@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("初始化");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request=(HttpServletRequest)servletRequest;HttpServletResponse response=(HttpServletResponse)servletResponse;String path = request.getRequestURI();//放行登录注册及其servletif("/login.jsp".equals(path) || "/register.jsp".equals(path) ||"/UserServlet".equals(path)|| path.startsWith("/img")){//放行filterChain.doFilter(request,servletResponse);}else {//登录放行HttpSession session = request.getSession();Object user = session.getAttribute("userinfo");if(user!=null){filterChain.doFilter(request,servletResponse);}else {//不登录返回登录页面response.sendRedirect("/login.jsp");}}}
//销毁方法@Overridepublic void destroy() {System.out.println("关闭");}
}
2.ajax异步请求
ajax响应的不是地址或网页,而是一个数据,在哪里发送请求数据就响应到哪里
允许浏览器与服务器通信而无需刷新当前页面的技术都叫ajax
===========使用jquery
1.引入jquery的插件
<script src="/js/jquery-3.7.1.min.js"></script>
2.设计点击事件
3.服务器响应的数据类型
1.文本类型
2.xml类型 不建议使用
3.json类型
4.json
JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。JSON
是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任何特殊的API或工具包。
JSON的规则很简单:对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’对”之间使用“,”(逗号)分隔。
json对象:
{k1:v1,k2:v2,k3:v3....}json对象集合:
[
{k:v,k:v},
{k:v,k:v},
{k:v,k:v}
]定义json对象 key必须用"" value可以是任意类型
<script>//定义json对象 key必须用"" value可以是任意类型var p={"name":"张三","age":19,"sex":"男","flag":true};alert(p.name);var ps=[{"name":"张三1","age":19,"sex":"男","flag":true},{"name":"张三2","age":19,"sex":"男","flag":true},{"name":"张三3","age":19,"sex":"男","flag":true}]for(var i=0;i<ps.length;i++){alert(ps[i].name);}</script>
异步请求方式:
$.ajax({
url: "服务器路径",
data: {携带的数据},
success: function(){回调函数},
dataType: 服务器响应的数据类型,
type: 请求方式,
async: 是否异步
})
$.get(url,data,callback,type);
$.post(url,data,callback,type);
案例
<body onload="loadBook()"><table><tr><th>书籍id</th><th>书籍名</th><th>价格</th><th>出版社</th><th>操作</th></tr><tbody id="tbodyBook"></tbody></table></body>
<script src="/js/jquery-3.7.1.min.js"></script><script>function loadBook(){$.post("/BookServlet",{"method":"getAll"},function (result){let elementById = document.getElementById("tbodyBook");var str="";for (var i=0;i<result.length;i++){//alert(result[i].name);str+="<tr>";str+="<td>"+result[i].id+"</td>";str+="<td>"+result[i].name+"</td>";str+="<td>"+result[i].price+"</td>";str+="<td>"+result[i].publisher+"</td>";str+="<td><a onclick='deleteBook("+result[i].id+") '>删除</a>" +"<a href='/BookServlet?method=getById&id="+result[i].id+"' >修改</a></td>";str+="</tr>";}elementById.innerHTML=str;},"json")}function deleteBook(id) {if(confirm("确定要删除该条信息吗?")){$.get("/BookServlet",{"method":"delete","id":id},function (result){if(result==1){alert("删除成功");loadBook();}else {alert("删除失败")}});}}</script>
private void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{int id = Integer.parseInt(request.getParameter("id"));int i = bookDao.deleteBook(id);PrintWriter out = response.getWriter();out.print(i);out.flush();out.close();}