jsp页面用DBHelper实现简单的登陆验证

首先我们需要写一个简单的登陆页面login.jsp,然后用from表单提交给index.jsp页面。在index.jsp页面通过DBHelper连接数据库判断账号和密码,如果密码正确则显示登陆成功。

下面是登陆页面代码
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登陆页面</title>

  </head>
  
  <body>

//提交用户名密码到index.jsp <form action="index.jsp" name="form1" method="post" > <div > <a>用户:</a><br> <input class="yh" type="text" value="" name="username" /><br> <a>密码:</a><br> <input class="ma" type="password" value="" name="password" /><br> <input class="dl1" type="submit" value="登陆" /> </div> </form> </body> </html>

效果图

下面是index.jsp 的代码

//注册DBHelper,不然会报错
<%
@ page language="java" import="java.util.*,DBHelper.*,java.sql.*" pageEncoding="UTF-8"%> <% %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>验证页面</title> </head> <body> <% String SQL="SELECT * FROM user WHERE username=? AND password=?"; String uid=request.getParameter("uid"); String pwd=request.getParameter("pwd"); Object[] params=new Object[]{uid,pwd}; ResultSet rs=DBHelper.getResultSet(SQL,params); if(rs.next()) out.print("登录成功"); else out.print("登录失败"); rs.close(); %> </body> </html>

 

然后将DBHelper包copy到src文件夹下

将DBHelper代码里的用户名和密码换成你的mysql数据库的。
然后就可以运行了。
我的运行结果


  

posted @ 2016-03-15 14:00  小小星火  阅读(753)  评论(0编辑  收藏  举报