Use servlet to make simple login verification, response download file and page jump

Source: Internet
Author: User
Tags html form


Make a login verification:

the development of the servlet has been introduced before, and most commonly used methods in HttpServletRequest and HttpServletResponse. Now we can make a simple login verification through these knowledge points, this login verification needs to connect the database, because the user name and password are stored in the database.


Tools and techniques to use:

Tomcat, C3P0, JDBC, Servlet, MySQL, HTML, CSS


Approximate idea:

First in the MySQL database to create a library and user data table, in the table first store a user and password (after all, no registration function).

Using HTML and CSS to write a login page, the user's password is submitted to the servlet on the server through the form.

Write a class that can get the database connection object from the C3P0 connection pool, and then write a servlet class that obtains the form data through the HttpServletRequest object in the Servlet class, and the user password needs to be validated (two authentication on the server). After verifying the connection to the MySQL database for querying, after querying the data, the HttpServletResponse object responds to the login success, otherwise the response login fails.


Implementation code example:

MySQL database:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office7709487c18ed4b8499778b09b5fbbbcb/4674 "alt=" 4674 "/>


The data in the table:

650) this.width=650; "style=" width:492px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office6291c934256f4964bd0c8bb01fa248fa/4675 "alt=" 4675 "/>

The password was encrypted using the password () function.


Page code:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office876fa79321d74ca1a2c06056e6251d7d/4676 "alt=" 4676 "/>

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office0fd7c899162842b1826135c02b2077fe/4677 "alt=" 4677 "/>

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officef66edcd131df43deaa03b4bff10e2f4f/4678 "alt=" 4678 "/>


Before you write a servlet, you need to download two jar packages, one c3p0 a JDBC package, and then put the two packages in the Lib directory in Web-inf (be careful not to put it wrong):

650) this.width=650; "style=" width:332px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office8d433d6ed49e4aa5a42bd52b04d4c749/4679 "alt=" 4679 "/>


Then write a class that can get the object from the connection pool:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office74b2e20814f641db92a40847b088c52b/4680 "alt=" 4680 "/>

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officed3ca9a3fede641a0818ef868b46dbd04/4681 "alt=" 4681 "/>



Servlet Code:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office71efda024381451da94327c7f881b440/4682 "alt=" 4682 "/>

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officeec5e4d786b4a4681aa4eda40272c3b79/4683 "alt=" 4683 "/>


Operating effect:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office5049317d285842f3bd222236f12f0e99/4685 "alt=" 4685 "/>


Enter your user name and password:

650) this.width=650; "style=" width:444px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office707ee350744e46f1ab46fe41a7c236c0/4686 "alt=" 4686 "/>


Login success:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officee7574f9ce55044d49c610c8cfe919706/4687 "alt=" 4687 "/>


Login failed:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office4a7c9f16792b4fcd9236dd964abef39b/4688 "alt=" 4688 "/>


From the implementation of the above code, I can see in the HTML form code, the declaration of the required required property as a form of validation, and then in the JS code to verify the validation, in addition to the service side of the Servlet class and code validation once, This verifies the legitimacy of the three data.

Some people may have doubts, why in the front-end to verify the legitimacy of the data, but also in the service to verify more than once, this is because his meow Web source can be changed, I can put the page code in the required attribute removed, and on some browsers can also disable JS interpreter (manual funny). If you don't believe it, you'll find out by looking at it:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officefa1d679d886444aa9d4c19d7b52ba033/4689 "alt=" 4689 "/>


Then I can skip the HTML form validation:

650) this.width=650; "style=" width:520px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office15f383ac697f4bc395048369493faab2/4690 "alt=" 4690 "/>

Because I also wrote a validation in JS, so the account password is shown empty, but the validation in HTML is skipped.


Then I put JS in the browser settings to disable:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officeea753c4a251640b6b30f58c7a7120774/4691 "alt=" 4691 "/>


That's all that's left of the service. The layer verifies that:

650) this.width=650; "style=" width:476px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office239421546258408abedd4c7278538120/4692 "alt=" 4692 "/>

Therefore, the service-side verification is essential, if not on the server to write more than one layer of verification is doing things.





Simple file download via response:

In fact, even if not through response can also allow the browser to download files, just need to download the files in the WebContent directory can be:

650) this.width=650; "style=" width:323px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office7f6a271e9a264e8ba491e42956dcd8b0/4693 "alt=" 4693 "/>


The file name can then be downloaded via the browser:

650) this.width=650; "style=" width:483px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office7878de2375614076b3928b2a06163497/4694 "alt=" 4694 "/>

650) this.width=650; "style=" width:295px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officef13e75ea5caa4e06a6e60acda46d0ddf/4695 "alt=" 4695 "/>

This is because Tomcat can automatically respond to the corresponding file type to the browser, and the browser will automatically download the file after it has found that it cannot be opened directly.

But in the actual development do not do so, because it is not safe, if it is not important files or can provide a public download file is good to say, but the important data files or customer information is such hotlinking download, will cause the customer's privacy disclosure, Therefore, the download of this type of file must be verified by the server before it can be downloaded.



For example, we can do a simple experiment, combined with the example of the login verification above, the login is successful before starting to download the file, in the servlet through the HttpServletResponse object we can respond to a browser to download the file type, and then through the i/ The O stream outputs the local file file to the browser for download.

code example:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office0274a571b3c0427aa0790d8bed47618a/4696 "alt=" 4696 "/>

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office3020954e5a134947aa13f1cff5da2bc1/4697 "alt=" 4697 "/>

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office4729f6a59d1446e0839a1f766d62c569/4698 "alt=" 4698 "/>


Login success:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officeb15bd9903ee34007855ad86e4e1e28ee/4699 "alt=" 4699 "/>


Login failed:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office7ac28ecda90e48a69337a5fa33c6ec79/4700 "alt=" 4700 "/>

That's it, we,




Page Jump:

In the request and response object each have a method, can implement the function of requesting jump, this jump is divided into redirect jump, and internal forwarding jump,:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office9aaef633a32a44bda9b091c69b4ab13f/4701 "alt=" 4701 "/>

You can see the difference between redirection and internal forwarding, the redirect is to access Aservlet, and then Aservlet tells the browser to visit Bservlet, so the browser accesses the servlet two times.

The internal forwarding browser only needs to access the servlet once, because Aservlet will forward the request internally to Bservlet, so the HttpServletRequest and HttpServletResponse objects are still the same, and redirection is the reverse , two objects were generated because the servlet was accessed two times.

REDIRECT Jump code example:

REDIRECT jumps require the use of the HttpServletResponse object to invoke the Sendredirect method, which needs to pass a parameter that passes the Web access name of the servlet to jump.

Aservlet:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office0a317549246a409e902604d2aea5b6b0/4702 "alt=" 4702 "/>


Bservlet:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office8cba3d8ea0ff464e8b0e93356a892185/4703 "alt=" 4703 "/>


When the browser accesses the Aservlet, it jumps to the Bservlet:

650) this.width=650; "style=" width:402px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officee3f0635b4a2e48ad9bedee6530868185/4704 "alt=" 4704 "/>


Console Print Results:

650) this.width=650; "style=" width:184px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office6449c4ffe308409d86130e673fe9c155/4705 "alt=" 4705 "/>

From the print order you can see that the aservlet is accessed first and then accessed by Bservlet.


Redirection can be forwarded at the time of the URL to add some parameters, code example:

Aservlet:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officeb6777098251b47e78a4388d27b415e5b/4706 "alt=" 4706 "/>


Bservlet:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officef75f5af2614b47e7827348cda246795e/4707 "alt=" 4707 "/>


A few more parameters will be found after the browser accesses the Aservlet jump:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office00a26d369389445cb302558fd7e4b5e2/4708 "alt=" 4708 "/>


Console Print Results:

650) this.width=650; "style=" width:220px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office69bf60b08a4c47159e1f095ca2402a9e/4709 "alt=" 4709 "/>



Internal Forwarding code example:

Internal forwarding requires the use of the HttpServletRequest object to invoke the Getrequestdispatcher method, which also needs to pass a parameter that passes the Web access name of the servlet to jump. But this method returns a RequestDispatcher object, You will then need to call the forward method through this object to pass the HttpServletRequest and HttpServletResponse objects along to the target servlet, code example:

Aservlet:

650) this.width=650; "style=" width:552px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office81480ba0189e4d5e99c8f46b7a8ebbfc/4710 "alt=" 4710 "/>


Bservlet:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officec7ff0c29a085410889a54a34f903047e/4711 "alt=" 4711 "/>


When the browser accesses Aservlet, the URL does not appear to jump to the Bservlet, but receives a response from the Bservlet, which is a different point between the internal forwarding and the redirect jump:

650) this.width=650; "style=" width:353px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officec7ff46b66c324716ba7e16d63b328818/4712 "alt=" 4712 "/>


Console Print Results:

650) this.width=650; "style=" width:185px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office5643ffb78d984d37b85666816369f47b/4713 "alt=" 4713 "/>

From the print order you can see that the aservlet is accessed first and then accessed by Bservlet.


Because internal forwarding passes both the request and the response object to the target servlet, we can add an attribute to the request data at the time of forwarding:

Aservlet:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officedcc4a307121d487ba9ead857ab618c1d/4714 "alt=" 4714 "/>


Bservlet:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office197ebd480c49498b89d046e67b2f88bd/4715 "alt=" 4715 "/>


Browser Access results:

650) this.width=650; "style=" width:330px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officefbbb0ce98dc747079e09ecb626490c00/4716 "alt=" 4716 "/>


Console Print Results:

650) this.width=650; "style=" width:240px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/office3c29f79a948f43f4b70fee84eb58950c/4717 "alt=" 4717 "/>


The above example just demonstrates a simple jump, in addition to jumping to their own projects under the page, you can also redirect to other people's pages, such as Baidu, Google and other code examples:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officecfdb6168beb742feac8b6fd384a93eec/4718 "alt=" 4718 "/>


Operation Result:

650) this.width=650; "style=" width:553px; "src=" https://note.youdao.com/yws/public/resource/ 63cb50ee186ab78e34e2aada9d4a4193/xmlnote/officef54b7fbddc3846f68fc212fe938c9b49/4719 "alt=" 4719 "/>


Note: Only redirect type of jump can jump other people's pages, internal forwarding can only jump to their own projects under the page, and the implementation of the redirected Sendredirect method can only be called in one servlet once, if the call two times will be an error.




This article is from the "zero" blog, make sure to keep this source http://zero01.blog.51cto.com/12831981/1981476

Use servlet to make simple login verification, response download file and page jump

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.