- 设计数据库employee,manager表,字段:user_id (主键,自增1,int类型),user_name, password
- 创建数据源:employee_dsn
- 制作登录页面login.jsp
登录页面 login.jsp代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>www.xin126.cn原创:JSP制作登录页面:login.jsp</title> <script language="javascript"> <!-- function check() { //验证用户名字段是否为空 username= document.form1.username.value; if (username == "") { alert("请输入用户名"); document.form1.username.focus(); return false; } else{ //验证密码字段是否为空 password = document.form1.password.value; if (password == "") { alert("请输入登陆密码"); document.form1.password.focus(); return false; } } } --> </script> </head> <body> <form name="form1" method="post" action="login_check.jsp"> 用户名:<input name="username" type="text" > 密码: <input name="password" type="text" > <input type="submit" name="Submit" value="登录" onClick="return check()"> <input type="reset" name="Submit2" value="重填"> </form> </body> </html>
提交后登录验证页面:login_check.jsp核心代码如下:
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%> <% String username=request.getParameter("username"); String password=request.getParameter("password"); Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException e) { out.print(e); } try { con=DriverManager.getConnection("jdbc:odbc:employee_dsn","sa","2005"); sql=con.createStatement(); rs=sql.executeQuery("select * from manager where user_name='"+username+"' and password='"+password+"'"); if(rs.next()) { session.setAttribute("login","true"); response.sendRedirect("admin.jsp"); }else{ %> <script language="javascript"> <!-- alert("用户名或密码错误!"); window.location="login.jsp"; --> </script> <% } con.close(); } catch(SQLException e) { out.print(e); } %>
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/8998.html