Servlet + JavaBean + JSP create Java Web registration and login function

The MVC structure diagram implemented by java web is as follows, in which the controller part is implemented by servlet, the model part is implemented by JavaBean, and most views are implemented by JSP pages.

The working principle of JSP + JavaBean two-tier structure should be familiar and easy to understand. However, it must be clear that the user sends a web page request through the browser. After the request reaches the server, find the corresponding web page on the server, If this is the first request (there is no need to explain the implementation for the second time). For JSP, it is necessary to generate a servlet, then execute the servlet through the servlet engine, embed the result of calling JavaBean in the page and return it to the user's browser. The essence of the three-tier structure of JSP + JavaBean + Servlet is that there is an additional controller: servlet to distribute the requests of the client browser. If the servlet that plays the role of controller is used It is understood that preprocessing the client's request will be very helpful to understand the servlet. Via web The XML configuration file can find the corresponding relationship between user requests and specific servlets. Each servlet has a specific servlet object corresponding to it. Therefore, a servlet object inherited from httpservlet is used to process user requests.

As shown above, it is extracted from the web A configuration servlet of XML. The first part is mainly used to configure the servlet to be associated with a specific servlet object. The second part is mainly used to configure which servlet handles the request and the servlet name. The processing request is associated with the specific servlet processing object. For example, the / newsmain request sent by the client browser is processed by the MS1 servlet, The corresponding Serlet object news can be found through MS1 Firstaction, i.e. / newsmain-ms1-news Firstaction, which is the meaning of the configuration file. Now I understand that the user / newsmain request will be sent to news The object of firstaction class is processed, so if you want to understand the program, you need to understand the function of firstaction. For example, the following is an implementation of firstaction.

Through this implementation, you can see that when the server receives a client request to execute news Searchnewstitle (DB), and then pass the return value through session Put setAttribute into session, and then use resp Sendredirect (target) is transferred indirectly to newsmain JSP, so in newsmain JSP through session The getattribute function can get the corresponding value stored in the session. Looking back, it is easy to see that the working principle of JSP + JavaBean is different from that of JSP + JavaBean + Servlet. The two-tier structure must put the preprocessing in JSP, such as news Searchnewstitle (DB), a three-tier structure, first preprocesses in the servlet, and then returns the processing result to JSP through session, making JSP pay more attention to the display of the interface.

Login registration module requirements 1 registration 1.1 user's registration form (user name, password, email, nickname, verification code) 1.2 submit registration: to do (user name, password, mailbox, nickname and verification code). 1.2.1 the user name, password, mailbox and nickname are completed in the client browser through JS. 1.2.2 the verification code is to be completed in the server program. 2. If the verification of the registration form passes, judge the business logic. 2.1 if the user already exists, tell the user the error message Rest. 2.2 if the mailbox already exists, tell the user the error message. 2.3 if none exists Then proceed to step 3. 3. Save the user's information to the database 4 Registration is successful, jump to login page 5 Login 5.1 send the user's login information to the background for verification 5.2 if the verification is successful, jump to the home page 5.3 if the jump fails, jump to the login page and prompt error information.

Project directory structure the source code of the project is divided into four package files, which are used to access models, views, controllers and tool classes respectively. The specific files are as follows:

For the view, we define three JSP pages, as follows:

Define view login JSP page

index. JSP page

regist. JSP page

Define user model:

Useroperation model

Checkcode model

Define controller loginservlet class

Registservlet class

Checkimageservlet class

Define tool class dbutil class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>