安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据 2.制作jsp登录页面 index.jsp 提交到ok.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在 3.如果存在,把用户名保存,跳转到yes.jsp
数据库
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <body> <form action="ok.jsp" method=post> 用户名: <input type="text" name="uname" /> <br> 密 码 : <input type="text" name="upwd" /><br> 验证码: <input type="text" name="yzm" /> 11+30=?<br> <input type="submit" name="dl" value="登录" /> <br> </form> <% %> <script> //弹窗提示 alert('输入密码或用户名错误'); </script> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="java.sql.*"%> <html> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("uname"); String pwd = request.getParameter("upwd"); String yz = request.getParameter("yzm"); //request.getRequestDispatcher("index.jsp").forward(request,response); PreparedStatement pre = null; Connection con = null; Statement sql; ResultSet rs; request.setCharacterEncoding("utf-8"); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { out.print("<h1>加载错误" + e); } String user = "root"; String password = "root"; con = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/mysql", user, password); try { sql = con.createStatement(); String SQLL = "select * from sjk where uname=? and upwd=?"; pre = con.prepareStatement(SQLL); pre.setString(1, name); pre.setString(2, pwd); rs = pre.executeQuery(); if (rs.next()) { String na = rs.getString(2); session.setAttribute("uname", na); response.sendRedirect("yes.jsp"); } else { response.sendRedirect("index.jsp"); } } catch (SQLException e) { out.print("<h1>查询错误" + e); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <body> 欢迎 <%=session.getAttribute("uname")%> 来到你的世界! </body> </html>