### Ajax ```js $.ajax({ url: type: data: dataType: success: error: complete: }) $.ajax({ url:"/ajax.do", type:"POST", data:{ id:1, name:"zhangsan" }, dataType:"text", success:function(data){ alert(data); } }); ``` - 動態驗證用戶名是否存在。 ```jsp <%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-23 Time: 21:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ var flag = false; $("#name").blur(function(){ var name = $(this).val(); $.ajax({ url:"/login.do", type:"POST", data:"name="+name, dataType:"text", success:function (data) { if(data == "true"){ flag = true; $("#mess").html("<font color='green'>用戶名存在</font>"); } if(data == "false"){ flag = false; $("#mess").html("<font color='red'>用戶不存在!</font>"); } } }); }); $("#btn").click(function(){ if(flag){ $("form").submit(); } }); }); </script> </head> <body> <form action=""> 用戶名:<input type="text" name="name" id="name"/><span id="mess"></span><br/> 密碼:<input type="password" name="password" id="password"/><br/> <input id="btn" type="button" value="登陸"/> </form> </body> </html> ``` ```java package com.southwind.controller; import com.southwind.util.JDBCTools; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @WebServlet("/login.do") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); Connection connection = JDBCTools.getConn(); String sql = "select * from t_user where username = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String flag = "false"; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,name); resultSet = preparedStatement.executeQuery(); if(resultSet.next()){ flag = "true"; } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,resultSet); } resp.getWriter().write(flag); } } ``` - select 框三級聯動。
index.jspjavascript
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-23 Time: 20:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ $.ajax({ url:"/ajax.do", type:"POST", dataType:"JSON", success:function(data){ alert(data[0].id+"-"+data[0].name+"-"+data[0].age); } }); }); </script> </head> <body> <h1 id="id"></h1> <h1 id="name"></h1> <h1 id="age"></h1> </body> </html>
login.jsphtml
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-23 Time: 21:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ var flag = false; $("#name").blur(function(){ var name = $(this).val(); $.ajax({ url:"/login.do", type:"POST", data:"name="+name, dataType:"text", success:function (data) { if(data == "true"){ flag = true; $("#mess").html("<font color='green'>用戶名存在</font>"); } if(data == "false"){ flag = false; $("#mess").html("<font color='red'>用戶不存在!</font>"); } } }); }); $("#btn").click(function(){ if(flag){ $("form").submit(); } }); }); </script> </head> <body> <form action=""> 用戶名:<input type="text" name="name" id="name"/><span id="mess"></span><br/> 密碼:<input type="password" name="password" id="password"/><br/> <input id="btn" type="button" value="登陸"/> </form> </body> </html>
web.xmljava
/lib/...mysql
c3p0-config.xmljquery
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="testc3p0"> <!-- 指定鏈接數據源的基本屬性 --> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property> <!-- 若數據庫中鏈接數不足時, 一次向數據庫服務器申請多少個鏈接 --> <property name="acquireIncrement">5</property> <!-- 初始化數據庫鏈接池時鏈接的數量 --> <property name="initialPoolSize">5</property> <!-- 數據庫鏈接池中的最小的數據庫鏈接數 --> <property name="minPoolSize">5</property> <!-- 數據庫鏈接池中的最大的數據庫鏈接數 --> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
AjaxServlet.javaweb
package com.southwind.controller; import com.southwind.entity.User; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @WebServlet("/ajax.do") public class AjaxServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); // List<User> list = new ArrayList<>(); User user = new User(); user.setId(1); user.setName("張三"); user.setAge(22); // list.add(user); // user = new User(); // user.setId(2); // user.setName("李四"); // user.setAge(22); // list.add(user); // user = new User(); // user.setId(3); // user.setName("王五"); // user.setAge(22); // list.add(user); // JSONObject jsonObject = JSONObject.fromObject(list); JSONArray jsonArray = JSONArray.fromObject(user); resp.getWriter().write(jsonArray.toString()); } }
LoginServlet.javaajax
package com.southwind.controller; import com.southwind.util.JDBCTools; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @WebServlet("/login.do") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); Connection connection = JDBCTools.getConn(); String sql = "select * from t_user where username = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String flag = "false"; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,name); resultSet = preparedStatement.executeQuery(); if(resultSet.next()){ flag = "true"; } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,resultSet); } resp.getWriter().write(flag); } }
entity.javasql
package com.southwind.entity; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
JDBCTools.java數據庫
package com.southwind.util; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTools { private static DataSource dataSource; static { dataSource = new ComboPooledDataSource("testc3p0"); } public static Connection getConn(){ Connection connection = null; try { connection = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void release(Connection connection, Statement statement, ResultSet resultSet){ try { if(connection!=null){ connection.close(); } if(statement!=null){ statement.close(); } if(resultSet!=null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } }