The basic content about Servlet has been shared with you before. Refer to J2EE to learn from cainiao to the seven servlets of laruence. Now we can learn from it in DRP, a profound understanding of the role of Servlet in hub transit, control logic (more like a controller in MVC), especially similar to the general processing program in asp.net in the background when BS projects are implemented: the data obtained by the front-end html contains the data ---> General processing program --> passed through the general processing program to the database for query --- 1> General processing program ---> interface, which is displayed after rendering by the browser.
Servlet is a server-side applet. In BS architecture, it is used to process the response of client requests. It features a single instance and runs in multiple threads. servlet runs in containers, its life cycle is managed by tomcat, And the thread is insecure. When using it, you must note that the member variable is used with caution (unsafe ).
Servlet is on the server side (I am using Tomcat)
Front-end Html
<Html>
Servlet:
Import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class LoginServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request. getParameter ("username"); String password = request. getParameter ("password"); System. out. println ("username =" + username); System. out. println ("pas Sword = "+ password); response. setContentType (" text/html; charset = GB18030 "); // response. getWriter (). println (" Login Success !!! "); // Response. getWriter (). println (" Logon successful !!! "); Response. getWriter (). println ("
Configure servlet through xml
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet><servlet-name>MyServlet</servlet-name><servlet-class>LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>MyServlet</servlet-name><!--<url-pattern>/loginServlet</url-pattern>????????--><url-pattern>/exam/loginServlet</url-pattern></servlet-mapping></web-app>
Enter http: // 127.0.0.1: 8080/test_servlet/login.html in the address bar to read the logon page, as shown in figure
Tomcat received
Logon successful:
Entire implementation process
Entire Process:
After clicking submit in html, locate the web page according to the relative path http: // 127.0.0.1: 8080/test_servlet/loginServlet corresponding to the action. loginServlet in <servlet-mapping> <url-pattern> in the xml file. Find the <servlet-name> MyServlet in <servlet> for loginServlet according to <servlet-name>, generate a LoginServlet instance, call the doPost method of LoginServlet, and display the result in the browser.
Deep understanding with UML diagrams:
Summary
Servlet processing logic makes the structure clear and easy to write code and maintain, and the system is more flexible. We basically implement the principle of extended development and closed modification.
I will discuss it in depth after getting started with MVC and look forward ......