一个登陆页面(login,jsp)一个成功登陆页面(success.jsp)
请问:在JSP里怎么实现登陆验证(连接数据库)?
请问:在JSP里怎么实现登陆验证(连接数据库)?
解决方案 »
- 想实现这个超链接list.action?id=3&name=zhangsan,对id后的参数加密
- 网站开发架构书
- 上海:寻兼职(JAVA相关-项目、程序维护、二次开发等
- <html:multibox>标签是什么东西,<html:checkbox>又是什么东西?有什么区别?
- 请帮忙检查一下这段代码
- tomcat threadpool is full 是什么error 呀?
- SSH项目复杂查询和调用存储过程的问题。
- 难道可用JSP+SQL Sever?
- SOS---How to replace ' to ''? (Thanks a lot!)----SOS
- Tomcat安装报错,出错提示如下,但是可以运行JSP,但不能关掉DOS窗口,帮忙近来看看?
- 错误求教~~~大家来看看
- 难解的问题:上传下载前不弹出对话框(谁解决送谁分)
不过一般都不是这么写的,起码也要写在一个BEANS里面
然后若在数据库中搜索数据,若有则跳转到success.jsp中.
给你个例子吧!
<form method="POST" name="form9" action="success.jsp">
<p class="bod">>>用户名:<br>
<input name="username" type="text" class="denglu" value="" size="17" maxlength="17" tabindex="1">
<br>
>>密码:<br>
<input name="password" type="password" class="denglu" value="" size="17" maxlength="17" tabindex="2">
<br>
<form>success.jsp<%!String UserName,PassWord,sql;%>
<%
UserName=request.getParameter("username");
PassWord=request.getParameter("password"); sql="Select * From 用户表 Where 用户名='"+UserName+"' And 用户密码='"+PassWord+"'";
rs=stmt.executeQuery(sql);
rs.last();
if (rs.getRow()>0){
//to do
}
我用的是连接池进行的数据库操作哈!如果你用的是JDBC的话,把我注释掉的部分拿来用就可以了哈!当提交表单的时候就提交到这个页面进行登陆的验证:
tring strUserName = request.getParameter("txtUserName");
String strPassword = request.getParameter("txtPassword");
//创建数据库联接
ConnectionPool connPool = DB.getConnPool();
Connection con = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
//Connection con = null;
try {
//Class.forName("com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new Driver());
//String strUrl = "jdbc:mysql://127.0.0.1:3306/UserLogin?useUnicode=true&characterEncoding=GBK";
//String strUser = "root";
//String strPwd = "";
//System.out.println("1");
//con = DriverManager.getConnection(strUrl,strUser,strPwd);
//System.out.println("2");
//int nextMessageid = 0;
//String relative_path = null;
String sql = "select * from userinfo where username = '"
+ strUserName + "' and userpwd = '" + strPassword + "'";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
request.getSession(true).setAttribute("UserName",
strUserName);
response.sendRedirect("sucess.jsp");
} else {
request.getSession(true).setAttribute("UserName",strUserName);
response.sendRedirect("fail.jsp");
//out.println("对不起,登陆失败!");
}
} catch (SQLException sqlExc) { sqlExc.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
connPool.freeConnection(con);
} %>