图片
JSP:
Servlet:
package com.syh.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.syh.pojo.User; import com.syh.service.UserService; public class LoginServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取从jsp页面传过来的数据 String username = request.getParameter("username"); String password = request.getParameter("password"); //获取service对象,用来处理逻辑事务 UserService service = new UserService(); User user = new User(); user = service.findSimpleUser(username); if(password != null && password != "" && password.equals(user.getPassword())){ HttpSession session = request.getSession(); session.setAttribute("user", user); request.getRequestDispatcher("/success.jsp").forward(request, response); }else{ request.getRequestDispatcher("/error.jsp").forward(request, response); } } }
Service:
package com.syh.service; import com.syh.dao.UserDao; import com.syh.pojo.User; public class UserService { UserDao dao = new UserDao(); public User findSimpleUser(String username) { return dao.findSimpleUser(username); } }
dao:
package com.syh.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.syh.pojo.User; import com.syh.utils.DataSourseUtil; public class UserDao { private DataSourseUtil util = null; public User findSimpleUser(String username) { String sql = "select * from user where username = ?"; Map map = new HashMap(); List params = new ArrayList(); params.add(username); util = new DataSourseUtil(); map = util.findSample(sql, params); if(map != null){ User user = new User(); user.setUsername((String)map.get("username")); user.setPassword((String)map.get("password")); return user; } return null; } }
Util:
package com.syh.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataSourseUtil {
private static String name = "root";
private static String password = "123456";
private static String url = "jdbc:mysql://localhost:3306/test";
private final static String Driver = "com.mysql.jdbc.Driver";
private static Connection coon = null;
private PreparedStatement statement = null;
private ResultSet result = null;
static{
try {
Class.forName(Driver);
coon = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
}
//增删改
public boolean addDeleteModify(String sql , List
params){
boolean flag = false;
int rows = -1;//表示受影响的行数
try{
this.statement = coon.prepareStatement(sql);
int index = 1;
int len = params.size();
if (params != null && !params.isEmpty()){
for(int i = 0 ; i < len ; i++){
statement.setObject(index++, params.get(i));
}
}
rows = statement.executeUpdate();
flag = rows > 0 ? true : false;
}catch(Exception ex){
ex.printStackTrace();
}
return flag;
}
//查找 单一查找
public Map findSample(String sql , List params){
Map map = new HashMap();
try{
this.statement = coon.prepareStatement(sql);
int index = 1;
int len = params.size();
if (params != null && !params.isEmpty()){
for(int i = 0 ; i < len ; i++){
statement.setObject(index++, params.get(i));
}
}
result = statement.executeQuery();
ResultSetMetaData rsmd = result.getMetaData();//获取列名
int count = rsmd.getColumnCount();
System.out.println(result.getRow());
while(result.next()){
for(int i = 0 ; i < count ; i++ ){
String key = rsmd.getColumnName(i + 1);//获取第i个列名
Object value = result.getObject(key);//通过列名来获取对应的值
map.put(key, value);
}
}
return map;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
public List> findMany(String sql , List params){
//Map 存储的没行数据
List> list = new ArrayList>();
try{
this.statement = coon.prepareStatement(sql);
int index = 1;
int len = params.size();
if (params != null && !params.isEmpty()){
for(int i = 0 ; i < len ; i++){
statement.setObject(index++, params.get(i));
}
}
result = statement.executeQuery();
ResultSetMetaData rsmd = result.getMetaData();//获取列名
int count = rsmd.getColumnCount();//共有多少列
while(result.next()){
Map map = new HashMap();
for(int i = 0 ; i < count ; i++ ){
String key = rsmd.getColumnName(i + 1);//获取第i个列名
Object value = result.getObject(key);//通过列名来获取对应的值
map.put(key, value);
}
list.add(map);
}
return list;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
public void closeConnection(){
if(coon != null){
try {
coon.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(result != null){
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
web.xml:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> index.jsp LoginServlet com.syh.servlet.LoginServlet LoginServlet /loginServlet