Ajax模拟视频点赞功能

前台

<%--
  Created by IntelliJ IDEA.
  User: xx
  Date: 2023/9/4
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
    



当前点赞量为:

    ${count}



servlet

import javax.servlet.ServletContext;
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;
import java.io.PrintWriter;

@WebServlet("/likeServlet")
public class LikeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将获取的点赞了存储到ServletContext
        ServletContext servletContext = getServletContext();
        //获取当前点赞量
        Integer count =(Integer) servletContext.getAttribute("count");
        //如果是第一个点赞
        if (count == null){
            count = 1;
        } else {
            count++;
        }
        servletContext.setAttribute("count",count);
        /*原生js*/
        /*resp.sendRedirect("video.jsp");*/
        /*ajax*/
        PrintWriter writer = resp.getWriter();
        writer.print(count);
        writer.close();
    }
}

你可能感兴趣的:(web前端基础,ajax,音视频,okhttp,javascript,servlet,java,前端)