Configuring the Welcome page

  • Add the following code to web.xml to automatically find the request path in this list when accessing the project root path

     <welcome-file-list>
        <welcome-file>HomeServlet</welcome-file>
      </welcome-file-list>
    Copy the code

Synchronous and asynchronous requests

  • Synchronous request: the content returned includes the page and data, the entire page content refresh use

    Requests are made through the browser address bar, hyperlinks, and forms

  • Asynchronous request: only data is returned, and partial page content is refreshed

    Make asynchronous requests through Ajax

  • Backend separation: The server does not need to consider whether the request was made by the browser or the client app, but only returns data. For back-end Java programmers only need to develop a set of business code, to achieve the separation of the front and back end must require the browser to send requests must be asynchronous requests, so that all functions of the website are static pages through Ajax to send asynchronous requests to achieve

JSON

  • JSON is a lightweight data encapsulation format (Data Interchange Format) similar to XML.

    <books> <book> <name> <author> <price>5</price> </book> <book> <name> <author> <price>5</price> </book> </books>Copy the code

    Format json: [{” name “:” Java based “, “author” : “old master”, “price” : “5”}, {” name “:” Java based “, “author” : “old master”, “price” : “5”}]

  • Why use JSON?

    Between the client and the server might be sending more complex data content, this data must be saved to a string, then save the data in a string, must, in accordance with a particular format, or detrimental to the client access to the inside of the data, the highest use of JSON format is by far the most popular a kind of data exchange format.

  • How to convert complex data in the server to JSON?

    All kinds of data in the server are generally encapsulated in an entity, which can be easily converted into A JSON string through the existing three-party framework

The relationship between asynchronous AJax requests and JSON

  • AJax is a technology for sending asynchronous requests in front-end development. When sending asynchronous requests, the server generally returns only data without pages to the client. Therefore, when the returned data is complex, it needs to use JSON string to encapsulate the data into JSON string, which is convenient for the client to process.

Implement more loading steps

  1. Add more buttons to home.html and add click events to the buttons, in which an asynchronous ajax request is made to the LoadMoreServlet to pass the current number of images
  2. Create a LoadMoreServlet, get the ProductDao argument and call the laodMore(count) method
  3. Convert the queried list into a JSON string and pass it to the client
  4. Get the data where the Ajax request is made and display it on the page

Page views +1 step

  1. Before the DetailServlet queries the details of the work by ID, let the viewCount of the work +1 and call the addViewCount(ID) method in the DAO
  2. Implement addViewCount in the DAO

Step of thumbs-up function

1. In the detail. HTML page, add click event to the "Like" button with jQuery, send Ajax request in the event, send request to LikeServlet, and pass the ID of the work on the current page 2. Create LikeServlet and leave doGet method to get ID, create ProductDao and call addLikeCount(ID) method, then call findById(ID) method again to get article information, and finally return the number of likes in article information to the client. 3. In the detail.html page where the request is made, the number of likes returned by the server is obtained in the SUCCESS method, and the number of likes in the page is modified by jQuery codeCopy the code

The Filter Filter

  • What is a filter:

    Filters work somewhat like servlets, requiring one or more urls to be configured, When accessing the specified URL,Tomcat will access the Filter first, and then access the corresponding Servlet after the Filter is allowed. It can write the repeated code in multiple servlets to the Filter to improve development efficiency.

  • How to Use filters

    1. Create a Filter and write the code that needs to be repeated in the Servlet before it is written in the doFilter method of the Filter class. The doFilter method is used to control whether access to the Servlet is allowed to continue

    2. The url configured to handle in the web.xml file is a Servlet3.0 url that needs to be intercepted in annotations

      //@WebFilter(urlPatterns = {“/ShowSendServlet”,”/DelServlet”})

  • Several Settings of url-pattern

    1. Exact match: /ShowSendServlet, /DelServlet
    2. JPG *.png *.html can realize the image anti-theft chain
    3. Path match: /images/*
    4. All matches: /*