登陆功能
UsersDao.java
package com.wpf.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.wpf.model.Users;
import com.wpf.util.DBHelper;
public class UsersDao {
public Users selectUserByUsernameAndUserpwd(String username,String userpwd){
//在这里初始化时设置为null 因为我们不知道能否查询出内容, 如果查询出来了,对象才会初始化,否则就返回null用来做判断
Users user = null;
//1.获取连接
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
//2.准备查询的sql语句
String selSql = "select * from users where username = ? and userpwd = ?";
//3.预编译语句
try {
ps = con.prepareStatement(selSql);
//4.设置查询的参数值
ps.setString(1, username);
ps.setString(2, userpwd);
//5.执行查询语句 并拿去结果集
rs = ps.executeQuery();
//6.取出查询内容
if(rs.next()){//用户名是不能重复的,所以能查出来的只有一个内容
//如果查出来了,把数据放到 容器中
user = new Users();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setUserpwd(rs.getString("userpwd"));
user.setQm(rs.getString("qm"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBHelper.close(con, ps, rs);
}
return user;
}
public ArrayList<Users> selectUsersAll(){
//创建用于存放的集合
ArrayList<Users> list = new ArrayList<Users>();
//1.获取连接
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
//2.准备sql
String selSql = "select * from users";
//3.预编译语句
try {
ps = con.prepareStatement(selSql);
//4.如果有参数,设置参数
//5.执行查询语句 并拿去结果集
rs = ps.executeQuery();
//6.遍历结果集内容 通常会把查询出来的数据存放 到 Java内存中,用容器保存
while(rs.next()){
//每有一行新的数据,我们就创建一个新的Users对象用于存放该数据内容
Users user = new Users();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setUserpwd(rs.getString("userpwd"));
user.setQm(rs.getString("qm"));
//数据填充完毕后,放到集合容器中
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//7.关闭
DBHelper.close(con, ps, rs);
}
return list;
}
public int insertUsers(Users users){
int count = 0;
//1.获取连接
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
//2.准备sql语句
String insetSql = "insert into users values(?,?,?)";
try {
//3.预编译语句
ps = con.prepareStatement(insetSql);
//4.设置插入的内容
ps.setString(1, users.getUsername());
ps.setString(2, users.getUserpwd());
ps.setString(3, users.getQm());
//5.执行语句
count = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//6.关闭连接
DBHelper.close(con, ps, null);
}
return count;
}
//用来测试上面写的方法是否正确
public static void main(String[] args) {
// Users user = new Users();
// //设置数据
// user.setUsername("小韩");
// user.setUserpwd("123");
// user.setQm("This is my way!");
//
UsersDao usersDao = new UsersDao();
//ArrayList<Users> userlist = usersDao.selectUsersAll();
//System.out.println(userlist);
Users user = usersDao.selectUserByUsernameAndUserpwd("小可爱", "ewq");
System.out.println(user);
// int count = usersDao.insertUsers(user);
// System.out.println(count);
}
}
前台页面 也就是前台用户输入及信息获取的内容 index.jsp 和 LoginServlet
index.jsp
<%@ page language="java" import="java.util.*" 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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="<%=basePath%>/servlet/LoginServlet" method="post">
用户名:<input type="text" name="userName"> <br/>
密码:<input type="password" name="userPwd"> <br/>
<input type="submit" value="提交"> <a href="<%=basePath%>/register.jsp">注册</a><br/>
</form>
</body>
</html>
对应的LoginServlet ,,我们登陆提交的数据是在POST中,所以处理的请求是 doPost方法中
LoginServlet.java
package com.wpf.serlvet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wpf.dao.UsersDao;
import com.wpf.model.Users;
public class LoginServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取前段传过来的值
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
//设置返回 中文时乱码解决
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
//判断
if("1".equals(userName) && "1".equals(userPwd)){
out.print("登录成功!");
}else{
out.print("<font color='red'>登录失败!</font>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取前段传过来的值
//转化中文乱码
request.setCharacterEncoding("utf-8");
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
UsersDao dao = new UsersDao();
Users user = dao.selectUserByUsernameAndUserpwd(userName, userPwd);
//响应的内容
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(user!=null){
//数据库中可以查询到该登陆信息
out.print("登陆成功,欢迎<font color='red'>"+user.getUsername()+"</font>使用 嘻嘻嘻系统 ! 用户宣言为:"+user.getQm());
}else{
out.print("登陆失败,暗号对不上, 不是自己人!");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}