The connection and difference between ajax, js, jsp, servlet

Brief introduction

As I am working on the project, I have a deeper understanding of ajax, jsp, js and servlet, which seems more obvious than the vague concepts before.

  1. About AJAX: When I first learned ajax technology, it might be because I was a bit stupid. I thought it was a fixed mode. I typed fixed codes and performed fixed operations, but I didn't know how to use it, so the teacher just finished speaking. I don't know exactly where its magic is. In the process of working on the project, I slowly realized that AJAX has many practicalities. It has a fixed mode, which can pass the parameters in html/jsp to the servlet, and then call the servlet to obtain a series of method locks. The value is used as a callback function of AJAX, and then returned to html/jsp. Secondly, ajax can also refresh part of the page, this time the fuzzy search in the project is due to ajax;
  2. About JS: AJAX is generally written in JS, so I have more views on js. It can be seen as a piece of code embedded in JSP, in other words, all the content written in JSP can be written in JSP, so the code looks much clearer, so in this project according to ID lookup, paging lookup involves functions and AJAX parts, I extracted them and put them in the JS section;
  3. About JSP: The full name of JSP is java servlet page. It can be seen from the full name that it integrates part of the functions of servlet and html, in fact, it combines static pages and dynamic pages;
  4. For user servlets: I have two understandings of servlets: first, it can be used to write dynamic pages; second, it can connect to the database, obtain data through the MVC three-tier architecture, and then pass the data to the web page through the response. Get data through code in the web page.

The difference between ajax and servlet jump scenarios

1. Servlet page jump

In servlet, generally jump calls doGet or doPost method

1. Forward (forwarding) method

Jumping code: request.getRequestDispatcher(" ").forward(request, response);

Features:

  1. The path of the page is a relative path;
  2. The browser address bar will not change after the jump;
  3. The forward method can only jump to the page in this web application (no new page will be generated)

Use forwarding to jump to the page. Three methods can be used to pass values: request.getParameter(" "), request.getsession, request.setAttribute(" ") in the url

2. The redirect method

Jump code: response.sendRedirect(" ");

Features:

  1. The path of the page is a relative path;
  2. sendRedirect can redirect the page to any page, not necessarily limited to this web application; (for example: you can redirect to response.sendRedirect("http://www.4399.com");
  3. The browser address bar changes after the jump ;

Use forwarding to jump to the page, and the value can only be parameter or session

2. Ajax jump (JSP)

$.ajax({ url: "" //Enter the page you want to jump to type: "POST" // Choose your request method data: "" //Request the type of processing data to be sent async: false,// Synchronization: It means that the following js program will only be performed when there is a return value. success:function(msg){ // Logical code after successful writing }  } }








Guess you like

Origin blog.csdn.net/weixin_46687295/article/details/107287794