码迷,mamicode.com
首页 > 数据库 > 详细

用DBHelper在JSP页面中实现登录功能

时间:2016-06-29 12:52:14      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

首先我们需要写一个简单的登陆页面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 的代码:

<%@ 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数据库的。
然后就可以运行了。
我的运行结果

技术分享

用DBHelper在JSP页面中实现登录功能

标签:

原文地址:http://www.cnblogs.com/the-boy/p/5626283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!