1.业务逻辑图如下:
2.源代码demo:
2.1.View层:Jsp实现
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
海之家
<%=session.getAttribute("session")%>
2.2.web.xml
需要配置Servlet的部分代码:
MainController
com.lhb.controller.MainController
MainController
/MainController
index.jsp
RegisterController
com.lhb.controller.RegisterController
RegisterController
/RegisterController
package com.lhb.controller;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lhb.dao.MainInfoDao;
import com.lhb.dto.MainInfoDto;
public class MainController extends HttpServlet {
public MainController() {
super();
}
public void destroy() {
super.destroy();
}
/*
* public void doGet(HttpServletRequest request, HttpServletResponse
* response) throws ServletException, IOException { System.out.println(2);
* doPost(request, response); }
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
String username = request.getParameter("username");
if (method.equals("main")) {
MainInfoDao midao = new MainInfoDao();
ArrayList arr = midao.getThePass(username);
request.setAttribute("array", arr);
getServletContext().getRequestDispatcher("/main.jsp").forward(
request, response);
} else {
String pass = request.getParameter("pass");
MainInfoDao mino = new MainInfoDao();
boolean blean = mino.isRgiht(username, pass);
if (blean) {
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("index.jsp");
}
}
// System.out.println(username + "##" + pass);
}
public void init() throws ServletException {
// Put your code here
}
}
附加另一个Controller层的业务逻辑:
package com.lhb.controller;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lhb.dao.MainInfoDao;
import com.lhb.dto.MainInfoDto;
public class MainController extends HttpServlet {
public MainController() {
super();
}
public void destroy() {
super.destroy();
}
/*
* public void doGet(HttpServletRequest request, HttpServletResponse
* response) throws ServletException, IOException { System.out.println(2);
* doPost(request, response); }
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
String username = request.getParameter("username");
if (method.equals("main")) {
MainInfoDao midao = new MainInfoDao();
ArrayList arr = midao.getThePass(username);
request.setAttribute("array", arr);
getServletContext().getRequestDispatcher("/main.jsp").forward(
request, response);
} else {
String pass = request.getParameter("pass");
MainInfoDao mino = new MainInfoDao();
boolean blean = mino.isRgiht(username, pass);
if (blean) {
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("index.jsp");
}
}
// System.out.println(username + "##" + pass);
}
public void init() throws ServletException {
// Put your code here
}
}
3.模型Model
业务模型:
package com.lhb.dto;
public class MainInfoDto {
public String username;
public String pass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
package com.lhb.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.lhb.dto.MainInfoDto;
public class MainInfoDao {
private Connection conn;
private Statement stat;
/* 数据库链接 */
public void getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
// "jdbc:mysql://114.215.102.190:3306/test", "root", "root");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 关闭数据库 */
public void close() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
/* 注册数据到数据库 */
public boolean registerDao(MainInfoDto reg) {
getConnection();
boolean flag = false;
try {
String sql = "insert into user(username,password)values('"
+ reg.getUsername() + "','" + reg.getPass() + "')";
stat = (Statement) conn.createStatement();
/*
* if (stat.execute(sql)) {插入只能使用update,或者 使用下面的PreparedStatement
*/
if (stat.executeUpdate(sql) > 0) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return flag;
}
/* 验证用户登录 */
public boolean isRgiht(String username, String password) {
boolean flag = false;
getConnection();
try {
String sql = "select password from user where username='"
+ username + "'";
stat = conn.createStatement();
ResultSet rset = stat.executeQuery(sql);
while (rset.next()) {
String pass = rset.getString("password");
System.out.println(pass.toString());
if (password.equals(pass)) {
flag = true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return flag;
}
/* 查询用户密码 */
public ArrayList getThePass(String username) {
getConnection();
ArrayList al = new ArrayList();
;
try {
String sql = "select password from user where username='"
+ username + "'";//条件查询
String sql3 = "update user set password='$$$$$$$$' where username='"
+ username + "'";//修改数据库
String sql2 = "select password from user where username like '"+"%"+username+"%"+"'";
stat = conn.createStatement(); //模糊匹配
/* 查询模糊匹配的 */
ResultSet res2 = stat.executeQuery(sql2);
while (res2.next()) {
System.out.println(res2.getString("password"));
}
/* 精确查找 */
ResultSet res1 = stat.executeQuery(sql);
MainInfoDto mid = new MainInfoDto();
while (res1.next()) {
mid.setPass(res1.getString("password"));
al.add(mid);
}
if (stat.executeUpdate(sql3) > 0) {
System.out.println("修改成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return al;
}
}