通过servlet进行业务处理的两种方式
登录、注册、页面跳转都用到servlet,目前学习到两种方式
一、利用Dao类
前端传递数据到servlet后直接在servlet中调用dao中的方法进行与数据库的数据交互
servlet类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String phone = new String(request.getParameter("phone").getBytes("ISO-8859-1"), "UTF-8");
String mima = request.getParameter("mima");
MemberDao memberDao = new MemberDao();
String result = memberDao.login(phone, mima);
System.out.println("登录提交成功!");
}
MemnberDao类
public String login(String tel, String password) {
String sql = "select name from muser where password=? and tel=?";
try {
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, password);
pst.setString(2, tel);
ResultSet executeQuery = pst.executeQuery();
if (executeQuery.next()) {
String muser = executeQuery.getString("name");
return muser;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Conns.close(con);
}
return null;
}
Conns类是与数据库的连接类
二、直接利用servlet
所有的跳转全部到servlet中,根据参数tp的值来执行对应的方法
public class Myservlet extends HttpServlet {
UserService lg = new UserService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("业务方法被执行");
// 类型
String type = req.getParameter("tp");
System.out.println("业务方法类型:" + type);
if (type.equals("login")) {//前台登录
lg.login(req, resp);
} else if (type.equals("reg")) {//前台注册
lg.reg(req, resp);
}
}
}
UserService类就是普通的Java类
public class UserService {
Connection con = null;
public void login(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("登陆业务");
// 获取前台数据
String name = req.getParameter("username");
String pass = req.getParameter("password");
System.out.println("用户名" + name);
System.out.println("密码" + pass);
// 连接数据库'
// 1加载驱动程序
try {
con = new Dbutil().getConnection();
String sql = "select * from t_user where name ='" + name
+ "' and pass = '" + pass + "'";
PreparedStatement pst = con.prepareStatement(sql);
// 执行查询语句
ResultSet executeQuery = pst.executeQuery();
if (executeQuery.next()) {
System.out.println("用户id:" + executeQuery.getString("tel"));
System.out.println("登陆成功");
// 返回数据到前台ajax请求
User user = new User();
user.setTel(executeQuery.getString("tel"));
user.setName(name);
user.setPass(pass);
resp.setCharacterEncoding("UTF-8");// 设置页面编码格式
HttpSession s = req.getSession(true);
s.setAttribute("user", user);
req.getRequestDispatcher("index.jsp").forward(req, resp);
// resp.getWriter().write("ok");
// resp.sendRedirect("index.jsp");
} else {
System.out.println("登陆失败");
resp.getWriter().write("error");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void reg(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("注册业务。。。");
try{
String name = new String(req.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
String pass = req.getParameter("pwd");
String tel = req.getParameter("tel");
String mail = req.getParameter("mail");
String sql = "insert into t_user(name,pass,tel,mail) values(?,?,?,?)";
PreparedStatement pst = null;
con = new Dbutil().getConnection();
pst = con.prepareStatement(sql);
// 数据库编码有问题
pst.setString(1, name);//
pst.setString(2, pass);
pst.setString(3, tel);
pst.setString(4, mail);
int executeUpdate = pst.executeUpdate();
if (executeUpdate > 0) {
System.out.println("注册成功");
resp.sendRedirect("login.html");
} else {
System.out.println("注册失败");
resp.sendRedirect("regist.html");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.close(con);
}
}
}
个人认为后面这样方便些,不用建很多servlet,而且方便管理
但是第二种也可以用第一种的dao调用其中的方法