• Ajax模拟视频点赞功能


    前台

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: xx
    4. Date: 2023/9/4
    5. Time: 10:00
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. <script src="js/jquery-3.7.0.js">script>
    13. <script>
    14. // 1.原始方式
    15. /*function like(){
    16. window.location.href = "likeServlet"
    17. }*/
    18. // 2.原生JS实现异步
    19. function like(){
    20. //获取XmlHttpRequest对象
    21. var xhr = new XMLHttpRequest();
    22. //设置请求
    23. xhr.open("get","likeServlet");
    24. //发送请求
    25. xhr.send();
    26. xhr.onreadystatechange = function (){
    27. //获取状态码
    28. if ( xhr.readyState== 4){
    29. var msg = xhr.responseText;
    30. document.getElementById("num").innerText = msg;
    31. }
    32. }
    33. }
    34. // 3.第一次封装
    35. /*$(function () {
    36. //点击事件
    37. $("#btn").click(function () {
    38. $.ajax({
    39. url:"likeServlet",
    40. data:null,
    41. type:"get",
    42. async:true,
    43. dataType:"text",
    44. success:function (msg) {
    45. $("#num").text(msg);
    46. }
    47. });
    48. });
    49. });*/
    50. // 4.第二次封装
    51. /*$(function (){
    52. $("#btn").click(function (){
    53. $.get("likeServlet",null,function (date) {
    54. $("#num").text(date)
    55. },"text");
    56. });
    57. })*/
    58. script>
    59. head>
    60. <body>
    61. <video src="" controls>video>
    62. <input type="button" id="btn" onclick="like()" value="点赞">input>当前点赞量为:
    63. <span style="color: red" id = "num">
    64. ${count}
    65. span>
    66. body>
    67. html>

    servlet

    1. import javax.servlet.ServletContext;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.annotation.WebServlet;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.io.PrintWriter;
    9. @WebServlet("/likeServlet")
    10. public class LikeServlet extends HttpServlet {
    11. @Override
    12. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    13. //将获取的点赞了存储到ServletContext
    14. ServletContext servletContext = getServletContext();
    15. //获取当前点赞量
    16. Integer count =(Integer) servletContext.getAttribute("count");
    17. //如果是第一个点赞
    18. if (count == null){
    19. count = 1;
    20. } else {
    21. count++;
    22. }
    23. servletContext.setAttribute("count",count);
    24. /*原生js*/
    25. /*resp.sendRedirect("video.jsp");*/
    26. /*ajax*/
    27. PrintWriter writer = resp.getWriter();
    28. writer.print(count);
    29. writer.close();
    30. }
    31. }

  • 相关阅读:
    解决每次重启ganache虚拟环境,十个账号秘钥都会改变问题
    TypeScript 学习笔记(四):装饰器与高级编程技巧
    面试素材-结构化
    SpringBoot原理篇(2)—自定义starter
    东芝Z750的画质真实吗?适合看纪录片吗?
    Spring In Action 5 学习笔记 chapter1
    Yolov5 batch 推理
    一起来学Kotlin:概念:17. Kotlin Extension Function / Method (扩展函数)
    我的创作纪念日
    路由套接字
  • 原文地址:https://blog.csdn.net/weixin_62971115/article/details/132675798