Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Ajax to verify the existence of user name

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Ajax to verify the existence of a user name", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Ajax to verify the existence of a user name.

Jsp page

I introduced bootstrap and jQuery.

Name

Register.jsp page

/ / after the page is loaded, function fun () {/ / bind the user name to the blur event $("# studentName") .blur (function () {/ / get the value of the studentName text box var studentName = $("# studentName") .blur () / / send ajax request / / the data format {"userExsit": true, "msg": "this user name is too popular to expect from the server Please change a "} / {" userExsit ": false," msg ":" this user name already exists "} $.get (" CheckNameServlet ", {" studentName ": studentName}, function (data) {/ / determine whether the value of the userExsit key is true var span = $(" # s_studentName ") If (data.isExist) {/ / user does not exist span.css ("color", "red"); span.html (data.msg);} else {/ / user exists, which can be prompted or not given to span.html (");}}," json ");}; fun ()

The CheckNameServlet page under the background student file

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / set encoding format response.setContentType ("text/html;charset=UTF-8"); / / get the value of the front-end page String name = request.getParameter ("studentName") / / data format {"isExsit": true, "msg": "this user name is too popular. Replace it with"} / {"userExsit": false, "msg": "this user name already exists"} / / verify whether the user name try {boolean isExist = StudentService.isExist (name); System.out.println ("isExist" + isExist); Map map = new HashMap () / / Notification page, whether there is if (isExist) {map.put ("isExist", true); map.put ("msg", "this user name is too popular, please change one");} else {map.put ("isExist", false); / / map.put ("msg", "user name available") } / / before converting map to json, import the package ~ / / convert map to json and pass it to the client ObjectMapper mapper = new ObjectMapper (); mapper.writeValue (response.getWriter (), map);} catch (SQLException E1) {/ / TODO Auto-generated catch block e1.printStackTrace ();}}

The implementation of JDBCDemo's method (I didn't implement the interface, I wrote it directly)

Public static boolean checkName (String name) throws SQLException {boolean flag = false; String sql = "select * from student_table where student_name=?"; PreparedStatement statement = connection.prepareStatement (sql); statement.setString (1, name); ResultSet set = statement.executeQuery (); if the user name I entered matches the user name already in the database table if (set.next ()) {flag = true;} return flag }} at this point, I believe you have a deeper understanding of "how to verify the existence of a user name with Ajax". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report