javaweb利用ajax使登录窗口不跳转页面实现对账号密码的判断
和上一篇判断用户名是否被占用不跳转页面类似!利用ajax实现跳转,要导入jquery文件库!具体代码我会贴出来,注释在里面!!可以观摩一手!(代码我也留下链接,如果失效,评论补发,代码可能导入也无法使用!!你们要自己配置一下路径,或者自己建文件复制粘贴吧!)
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>在此处插入标题</title> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-3.3.1.js"></script> <script type="text/javascript"> function tiJiao(){ var username = $("#username").val();//获取登录的名字 var password = $("#password").val();//获取登陆的密码 if(username == null || username.length == 0 || password == null || password.length == 0 ){ alert("填写不完整");//判断是不是账号密码为空! return false;} var url="${pageContext.request.contextPath}/login";//这个地址是你要判断用户是否存在的后台 var args={"username":username,"password":password,"time":new Date()};//这个参数是把编辑框里的内容传过去给后台了,这个参数是你自己随便写的相当于map类键值对从这里可以传值过去,你在后台用request.getParameter(“”键“”)就可以得到里面的值愿意传多少都行!
$.post(url,args,function(data){$("#message").html(data);}); } </script> </head> <body> <div align="center"> 账号:<input type="text" id="username" name="username" style="width:200px; height:25px;" ><label id="message"></label></div><br> <div align="center">密码:<input type="password" id="password" name="password" style="width:200px; height:25px;"></div> <div align="center"><input type="button" value="登陆" onclick=" tiJiao()" style="width:70px; height:30px;" /></div> </body> </html>
servelt下的login.java(哈哈可能不太规范!)
package com.servelt; import java.io.IOException; import java.sql.SQLException; 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 com.stdy.Util.PdLogin; /** * Servlet implementation class login */ @WebServlet(asyncSupported = true, urlPatterns = { "/login" }) public class login extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); PdLogin login =new PdLogin(); String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("用户名"+username+password); try { String result=null; String temp=login.checkLogin(username, password); if(temp.equals("1")) { //result="<font color='red'>登录成功</font>"; //response.setContentType("text/html");//在这里是传回的文本格式为html格式 //response.getWriter().print(result);//将提示信息传回前端jsp页面 //其实上面几行写不写吧!!可以设置一个session什么的 //在这里可以直接跳转你想要登录的界面!! System.out.println("登录成功!"); } else { result="<font color='red'>账号或者密码错误</font>"; System.out.println(result); response.setContentType("text/html");//在这里是传回的文本格式为html格式 response.getWriter().print(result);//将提示信息传回前端jsp页面 } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
这个是数据库链接文件DButil.java
package com.stdy.Util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DButil { public static Connection getConnection() { try { //1 加载驱动 Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/test"; Connection connection = null; try { //2 创建链接对象connection connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("sql链接出现异常"); e.printStackTrace(); } return connection; } //关闭资源的方法 public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
这是判断是不是登录成功的java文件
PdLogin.java
package com.stdy.Util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class PdLogin { public String checkLogin(String username,String password) throws SQLException {String flag="0"; Connection connection = DButil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("select * from t_user where username =?"); preparedStatement.setString(1, username); ResultSet resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { if((resultSet.getString("password").equals(password))) {flag="1";//管理员 } } preparedStatement.close(); connection.close(); resultSet.close(); return flag; } }
数据库为mysql 数据库名称为test 下面就有一个表t_user 内容为 username和password 都为文本型 截图:
运行截图:
下载链接:链接: https://pan.baidu.com/s/1qZB8Fwg 密码: s4hq