news

实体bean-Admin.java
package www.csdn.net.ajax.domain;

import java.io.Serializable;

public class Admin implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private String pass;
    public Admin() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Admin(Integer id, String name, String pass) {
        super();
        this.id = id;
        this.name = name;
        this.pass = pass;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    @Override
    public String toString() {
        return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";
    }
}

BaseDao.java

package www.csdn.net.ajax.dao;

import java.util.List;

public interface BaseDao<T, PK> {

    /**
     * 插入实体
     *
     * @param entity
     * @throws Exception
     */
    void insert(T entity) throws Exception;

    /**
     * 更新实体
     *
     * @param entity
     * @throws Exception
     */
    void update(T entity) throws Exception;

    /**
     * 删除实体
     *
     * @param entity
     * @throws Exception
     */
    void delete(T entity) throws Exception;

    /**
     * 根据id删除实体
     *
     * @param id
     * @throws Exception
     */
    void deleteById(PK id) throws Exception;

    /**
     * 查询所有
     *
     * @return
     * @throws Exception
     */
    List<T> findAll() throws Exception;

    /**
     * 根据id查询实体对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    T findById(PK id) throws Exception;
    
    /**
     * 批量删除的操作
     * @param ids
     */
    void deletes(PK ids[])throws Exception;
    
    /**
     * 查询当前页信息
     * @param nowpage
     * @return
     */
    List<T> findNowPageInfo(int nowpage)throws Exception;
    
    Integer getCountPage()throws Exception;

}
AdminDao.java

package www.csdn.net.news.dao;

import www.csdn.net.news.domain.Admin;

public interface AdminDao extends BaseDao<Admin, Integer>{

    /**
     * 用户登录验证(根据name,pass)
     * @param name
     * @param pass
     * @return
     */
    Admin checkLogin(String name,String pass) throws Exception;
}

DBConn.java

package www.csdn.net.news.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBConn {
	private static Connection conn;

	private DBConn() {

	}

	public static Connection getConn() {
		try {
			if (conn == null) {
				// 创建集合对象
				Properties properties = new Properties();
				// 装载
				properties.load(DBConn.class.getClassLoader()
						.getResourceAsStream("db.properties"));
				// 加载驱动程序
				Class.forName(properties.getProperty("driverClassName"));
				// 获取连接对象
				conn = DriverManager.getConnection(
						properties.getProperty("url"),
						properties.getProperty("user"),
						properties.getProperty("pass"));
				conn.setAutoCommit(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void update(String sql, Object params[]) {
		PreparedStatement pstmt = null;

		try {
			pstmt = getConn().prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			pstmt.executeUpdate();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();

		} finally {
			release(null, pstmt);
		}
	}

	public static void release(ResultSet rs, PreparedStatement pstmt) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

AdminDaoImpl.java

package www.csdn.net.news.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import www.csdn.net.news.dao.AdminDao;
import www.csdn.net.news.domain.Admin;
import www.csdn.net.news.util.DBConn;

public class AdminDaoImpl implements AdminDao {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    @Override
    public void insert(Admin entity) throws Exception {
        // 定义sql
        String sql = "insert into admin(name,pass) values(?,?)";
        DBConn.update(sql, new Object[] {entity.getName(), entity.getPass()});
    }

    @Override
    public void update(Admin entity) throws Exception {
        // 定义sql语句
        String sql = "update admin set name=?,pass=? where id=?";
        DBConn.update(sql, new Object[] { entity.getName(), entity.getId(),
                entity.getPass() });
    }

    @Override
    public void delete(Admin entity) throws Exception {
        // 定义sql语句
        String sql = "delete from admin where id=?";
        DBConn.update(sql, new Object[] { entity.getId() });
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        // 定义sql
        String sql = "delete from admin where id=?";
        DBConn.update(sql, new Object[] { id });
    }

    @Override
    public List<Admin> findAll() throws Exception {
        List<Admin> entities = new ArrayList<Admin>();
        // 定义sql语句
        String sql = "select id,name,pass from admin";
        conn = DBConn.getConn();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            Admin entity = new Admin();
            entity.setId(rs.getInt("id"));
            entity.setName(rs.getString("name"));
            entity.setPass(rs.getString("pass"));
            entities.add(entity);

        }
        return entities;
    }

    @Override
    public Admin findById(Integer id) throws Exception {
        Admin entity = null;
        // 定义sql语句
        String sql = "select id,name,pass from admin";
        conn = DBConn.getConn();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            entity = new Admin();
            entity.setId(rs.getInt("id"));
            entity.setName(rs.getString("name"));
            entity.setPass(rs.getString("pass"));

        }
        return entity;

    }

    @Override
    public Admin checkLogin(String name, String pass) throws Exception {

        Admin entity = null;
        // 定义sql语句
        String sql = "select id,name,pass from admin where name=? and pass=?";
        conn = DBConn.getConn();
        pstmt = conn.prepareStatement(sql);
        int index = 1;
        pstmt.setString(index++, name);
        pstmt.setString(index++, pass);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            entity = new Admin();
            entity.setId(rs.getInt("id"));
            entity.setName(rs.getString("name"));
            entity.setPass(rs.getString("pass"));

        }
        return entity;
    }

    @Override
    public void deletes(Integer[] ids) throws Exception {
        conn = DBConn.getConn();
        try {
            for (int i = 0; i < ids.length; i++) {
                String sql = "delete from admin where id=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, ids[i]);
                pstmt.executeUpdate();
            }
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            conn.rollback();
        }
    }
    final int PAGESIZE=10;
    @Override
    public List<Admin> findNowPageInfo(int nowpage) throws Exception{
        List<Admin> entities = new ArrayList<Admin>();
        // 定义sql语句
        String sql = "select id,name,pass from admin limit ?,?";
        conn = DBConn.getConn();
        pstmt = conn.prepareStatement(sql);
        int index=1;
        pstmt.setInt(index++, (nowpage-1) * PAGESIZE);
        pstmt.setInt(index++, PAGESIZE);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            Admin entity = new Admin();
            entity.setId(rs.getInt("id"));
            entity.setName(rs.getString("name"));
            entity.setPass(rs.getString("pass"));
            entities.add(entity);

        }
        return entities;
    }

    @Override
    public Integer getCountPage() throws Exception {
        
        // 定义sql语句
        String sql = "select count(*) as c from admin";
        conn = DBConn.getConn();
        pstmt = conn.prepareStatement(sql);
        
        rs = pstmt.executeQuery();
        if(rs.next()) {
            int countsize=rs.getInt("c");
            return countsize%PAGESIZE==0?countsize/PAGESIZE:countsize/PAGESIZE+1;
        }
        return null;
    }

}



DefaultServlet.java


package www.csdn.net.news.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DefaultServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//转发
		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

AdminSerlet.java

package www.csdn.net.news.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.csdn.net.news.dao.AdminDao;
import www.csdn.net.news.domain.Admin;
import www.csdn.net.news.impl.AdminDaoImpl;

public class AdminServlet extends HttpServlet {
	// 声明dao操作对象
	private AdminDao adminDao = new AdminDaoImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码问题
		request.setCharacterEncoding("UTF-8");
		// 获取操作的标识符
		String oper = request.getParameter("oper");
		// 判断是否是登录操作
		if ("login".equals(oper)) {
			login(request, response);
		}

		if ("list".equals(oper)) {
			list(request, response);
		}
		if ("findById".equals(oper)) {
			findById(request, response);
		}
		if ("update".equals(oper)) {
			update(request, response);
		}
		if ("add".equals(oper)) {
			add(request, response);
		}
		if ("deletes".equals(oper)) {
			deletes(request, response);
		}
		if ("deleteById".equals(oper)) {
			deleteById(request, response);
		}
	}
	public void deleteById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String id=request.getParameter("id");
			
			try{
				adminDao.deleteById(Integer.parseInt(id));
			}catch(Exception e){
				request.setAttribute("msg","用户操作失败");
				e.printStackTrace();
			}
			
			// 转发给查询所有的操作
			request.getRequestDispatcher("./adminList.do?oper=list").forward(
					request, response);

	}
	public void deletes(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ids = request.getParameter("ids");
		System.out.println(ids + "----");
		String arrs[] = ids.split(",");
		Integer idarrs[] = new Integer[arrs.length];
		try {
			for (int i = 0; i < arrs.length; i++) {
				String arr = arrs[i];
				idarrs[i] = Integer.parseInt(arr);
			}
			adminDao.deletes(idarrs);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 转发给查询所有的操作
		request.getRequestDispatcher("./adminList.do?oper=list").forward(
				request, response);

	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取更新数据
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");

		Admin entity = new Admin();
		try {
			// 查询实体
			entity.setName(name);
			// 更新属性值
			entity.setPass(pass);
			// 更新
			adminDao.update(entity);
			// 转发给查询所有的操作
			request.getRequestDispatcher("./adminList.do?oper=list").forward(
					request, response);
		} catch (Exception e) {
			request.setAttribute("msg", "添加用户失败");
			// 转发到添加的页面
			request.getRequestDispatcher("./manager/admin/insertAdmin.jsp")
					.forward(request, response);
			e.printStackTrace();
		}

	}

	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取更新数据
		String id = request.getParameter("id");
		String name = request.getParameter("name");

		System.out.println("id:::" + id + "::::name" + name);

		Admin entity = null;
		try {
			// 查询实体
			entity = adminDao.findById(Integer.parseInt(id));
			// 更新属性值
			entity.setName(name);
			// 更新
			adminDao.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 转发给查询所有的操作
		request.getRequestDispatcher("./adminList.do?oper=list").forward(
				request, response);

	}

	public void findById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Admin entity = null;
		try {
			entity = adminDao.findById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("entity", entity);
		request.getRequestDispatcher("./manager/admin/updateAdmin.jsp")
				.forward(request, response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    String npage= request.getParameter("nowpage");
		int nowpage=1;
		int countPage=0;
		if(npage!=null){
			nowpage=Integer.parseInt(npage);
		}
		// 查询所有
		List<Admin> entities = null;
		try {
			entities = adminDao.findNowPageInfo(nowpage);
			 countPage=adminDao.getCountPage();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("entities", entities);
		request.setAttribute("nowpage", nowpage);
		request.setAttribute("countpage", countPage);
		request.getRequestDispatcher("/manager/admin/adminList.jsp").forward(
				request, response);
	}

	// 用户登录操作
	public void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取用户登录操作的信息
		String name = getValue(request, "name");
		String pass = getValue(request, "pass");
		System.out.println(name + "==" + pass);
		// 调用业务方法
		// dao对象去调用响应的操作
		Admin admin = null;
		try {
			admin = adminDao.checkLogin(name, pass);
		} catch (Exception e) {

			e.printStackTrace();
		}
		// 判断
		if (admin != null) {
			// 把admin对象储存
			request.getSession().setAttribute("name", admin.getName());
			// 转发成功页面
			request.getRequestDispatcher("/manager/main.jsp").forward(request,
					response);
		} else {
			request.setAttribute("msg", "用户名或者密码错误,请重新登录");
			// 转发登录界面
			request.getRequestDispatcher("WEB-INF/login.jsp").forward(request,
					response);
			;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * 获取参数指定的值
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public String getValue(HttpServletRequest request, String name) {
		return request.getParameter(name);
	}
}

WebRoot/manager

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>
  </head>
  
  <body>
    路径:<%=basePath%><a href="./default.do">登录界面</a>
  </body>
</html>
WebRoot/WEB-INF

login.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 'login.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>
	<div style="text-align: center;">
		<form action="./adminLogin.do" method="post">
			用户名:<input type="text" name="name" /></br> 密码:<input type="password"
				name="pass" /></br> <input type="reset" value="重置" />
			           <input
				type="submit" value="登录" /> <input type="hidden" name="oper"
				value="login" />
		</form>
	</div>
	<div>
		<%
			String msg = (String) request.getAttribute("msg");
			if (msg != null) {
		%>
		<%=msg%>
		<%
			} else {
		%>
		<h5>没有提示操作!</h5>
		<%
			}
		%>
	</div>
</body>
</html>

WebRoot/manager

main.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 'main.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>
<frameset border="1px" rows=10%,*,10%>
	<frame noresize="noresize" src="./manager/top.jsp">
	<frameset cols="10%,*,">
		<frame noresize="noresize" src="./manager/left.jsp">
		<frame noresize="noresize" name="m" src="./manager/rigth.jsp">
	</frameset>
	<frame noresize="noresize" src="./manager/botton.jsp">
</frameset>
</html>

left.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 'left.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 bgcolor="lavender">
  
     <div align="center">
        <a href="./adminList.do?oper=list" target="m">管理员管理</a><br/><br/><br/>
        <a href="#">新闻类型管理</a><br/><br/><br/>
        <a href="#">新闻管理</a>
       
     </div>
   
  </body>
</html>

AdminServlet.java

package www.csdn.net.news.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.csdn.net.news.dao.AdminDao;
import www.csdn.net.news.domain.Admin;
import www.csdn.net.news.impl.AdminDaoImpl;

public class AdminServlet extends HttpServlet {
	// 声明dao操作对象
	private AdminDao adminDao = new AdminDaoImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码问题
		request.setCharacterEncoding("UTF-8");
		// 获取操作的标识符
		String oper = request.getParameter("oper");
		// 判断是否是登录操作
		if ("login".equals(oper)) {
			login(request, response);
		}

		if ("list".equals(oper)) {
			list(request, response);
		}
		if ("findById".equals(oper)) {
			findById(request, response);
		}
		if ("update".equals(oper)) {
			update(request, response);
		}
		if ("add".equals(oper)) {
			add(request, response);
		}
		if ("deletes".equals(oper)) {
			deletes(request, response);
		}
		if ("deleteById".equals(oper)) {
			deleteById(request, response);
		}
	}
	public void deleteById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String id=request.getParameter("id");
			
			try{
				adminDao.deleteById(Integer.parseInt(id));
			}catch(Exception e){
				request.setAttribute("msg","用户操作失败");
				e.printStackTrace();
			}
			
			// 转发给查询所有的操作
			request.getRequestDispatcher("./adminList.do?oper=list").forward(
					request, response);

	}
	public void deletes(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ids = request.getParameter("ids");
		System.out.println(ids + "----");
		String arrs[] = ids.split(",");
		Integer idarrs[] = new Integer[arrs.length];
		try {
			for (int i = 0; i < arrs.length; i++) {
				String arr = arrs[i];
				idarrs[i] = Integer.parseInt(arr);
			}
			adminDao.deletes(idarrs);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 转发给查询所有的操作
		request.getRequestDispatcher("./adminList.do?oper=list").forward(
				request, response);

	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取更新数据
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");

		Admin entity = new Admin();
		try {
			// 查询实体
			entity.setName(name);
			// 更新属性值
			entity.setPass(pass);
			// 更新
			adminDao.update(entity);
			// 转发给查询所有的操作
			request.getRequestDispatcher("./adminList.do?oper=list").forward(
					request, response);
		} catch (Exception e) {
			request.setAttribute("msg", "添加用户失败");
			// 转发到添加的页面
			request.getRequestDispatcher("./manager/admin/insertAdmin.jsp")
					.forward(request, response);
			e.printStackTrace();
		}

	}

	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取更新数据
		String id = request.getParameter("id");
		String name = request.getParameter("name");

		System.out.println("id:::" + id + "::::name" + name);

		Admin entity = null;
		try {
			// 查询实体
			entity = adminDao.findById(Integer.parseInt(id));
			// 更新属性值
			entity.setName(name);
			// 更新
			adminDao.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 转发给查询所有的操作
		request.getRequestDispatcher("./adminList.do?oper=list").forward(
				request, response);

	}

	public void findById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Admin entity = null;
		try {
			entity = adminDao.findById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("entity", entity);
		request.getRequestDispatcher("./manager/admin/updateAdmin.jsp")
				.forward(request, response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    String npage= request.getParameter("nowpage");
		int nowpage=1;
		int countPage=0;
		if(npage!=null){
			nowpage=Integer.parseInt(npage);
		}
		// 查询所有
		List<Admin> entities = null;
		try {
			entities = adminDao.findNowPageInfo(nowpage);
			 countPage=adminDao.getCountPage();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("entities", entities);
		request.setAttribute("nowpage", nowpage);
		request.setAttribute("countpage", countPage);
		request.getRequestDispatcher("/manager/admin/adminList.jsp").forward(
				request, response);
	}

	// 用户登录操作
	public void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取用户登录操作的信息
		String name = getValue(request, "name");
		String pass = getValue(request, "pass");
		System.out.println(name + "==" + pass);
		// 调用业务方法
		// dao对象去调用响应的操作
		Admin admin = null;
		try {
			admin = adminDao.checkLogin(name, pass);
		} catch (Exception e) {

			e.printStackTrace();
		}
		// 判断
		if (admin != null) {
			// 把admin对象储存
			request.getSession().setAttribute("name", admin.getName());
			// 转发成功页面
			request.getRequestDispatcher("/manager/main.jsp").forward(request,
					response);
		} else {
			request.setAttribute("msg", "用户名或者密码错误,请重新登录");
			// 转发登录界面
			request.getRequestDispatcher("WEB-INF/login.jsp").forward(request,
					response);
			;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * 获取参数指定的值
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public String getValue(HttpServletRequest request, String name) {
		return request.getParameter(name);
	}
}

WebRoot/manager/admin

adminList.jsp

<%@ page language="java"
	import="java.util.*,www.csdn.net.news.domain.Admin"
	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 'adminList.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">
	-->
<script type="text/javascript">
	//当窗体加载完毕后触发函数
	window.onload = function() {

		var cbk = document.getElementById("cbk");
		//触发函数
		cbk.onclick = clickCheckbox;

	}
	//全选或全取消
	function clickCheckbox() {
		var cbks = document.getElementsByName("cbks");
		if (this.checked) {
			for (var i = 0; i < cbks.length; i++) {
				var cbktemp = cbks[i];
				if (!cbktemp.checked) {
					cbktemp.checked = true;
				}
			}
		} else {
			for (var i = 0; i < cbks.length; i++) {
				var cbktemp = cbks[i];
				if (cbktemp.checked) {
					cbktemp.checked = false;
				}
			}
		}

	}
	function deletes() {
		var cbks = document.getElementsByName("cbks");
		var temp = "";
		for (var i = 0; i < cbks.length; i++) {
			var cbktemp = cbks[i];
			if (cbktemp.checked) {
				temp += cbktemp.value + ",";
			}
		}
		//发送请求到web器
		window.location.href = "./deletesAdmin.do?ids=" + temp
				+ "&oper=deletes";
	}
</script>
</head>

<body>
	<div align="center">
		<div>
			<a href="./manager/admin/insertAdmin.jsp" target="m">添加</a> <a
				href="javascript:deletes()">删除所选项</a>
		</div>

		<div>
			<%
				String msg = (String) request.getAttribute("msg");
				if (msg != null) {
			%>
			<%=msg%>
			<%
				}
			%>
		</div>

		<div>
			<%
				List<Admin> entities = (List<Admin>) request
						.getAttribute("entities");
			%>
			<table border="1px" cellpadding="0" cellspacing="0">
				<thead>
					<th><input id="cbk" type="checkbox"></th>
					<th>序号</th>
					<th>姓名</th>
					<th>密码</th>
					<th>操作</th>
				</thead>
				<tbody>
					<%
						for (Admin entity : entities) {
					%>
					<tr>
						<td><input name="cbks" type="checkbox"
							value="<%=entity.getId()%>" /></td>
						<td><%=entity.getId()%></td>
						<td><%=entity.getName()%></td>
						<td><%=entity.getPass()%></td>
						<td><a
							href="./findByIdAdmin.do?oper=findById&id=<%=entity.getId()%>"
							target="m">编辑</a> | <a
							href="./deleteAdmin.do?oper=deleteById&id=<%=entity.getId()%>">删除</a></td>
					</tr>
					<%
						}
					%>
					<tr>
						<td colspan="5" align="center"><a
							href="./adminList.do?oper=list&nowpage=1">首页</a> <a
							href="./adminList.do?oper=list&nowpage=<%=(Integer) request.getAttribute("nowpage") - 1 <= 1 ? 1
					: (Integer) request.getAttribute("nowpage") - 1%>">上一页</a>
							<a
							href="./adminList.do?oper=list&nowpage=<%=(Integer) request.getAttribute("nowpage") + 1 >= (Integer) request
					.getAttribute("countpage") ? (Integer) request
					.getAttribute("countpage") : (Integer) request
					.getAttribute("nowpage") + 1%>">下一页</a>
							<a
							href="./adminList.do?oper=list&nowpage=<%=(Integer) request.getAttribute("countpage")%>">末页</a>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</body>
</html>

insertAdmin.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 'insertAdmin.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>
	<div align="center">
		<div>
			<%
				String msg = (String) request.getAttribute("msg");
				if (msg != null) {
			%>
			<%=msg%>
			<%
				}
			%>
		</div>
		<h3>用户加添界面</h3>
		<form action="./addAdmin.do" method="post">
			<table>
				<tr>
					<td>名称:</td>
					<td><input type="text" name="name"></td>
					<td><input type="button" value="检查用户"></td>
				</tr>
				<tr>
					<td>密码:</td>
					<td colspan="2"><input type="password" name="pass"></td>
				</tr>
				<tr>
					<td colspan="3" align="center"><input type="reset" value="重置" />
						<input type="submit" value="添加" /> <input type="hidden"
						value="add" name="oper"></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

updateAdmin.jsp

<%@ page language="java"
	import="java.util.*,www.csdn.net.news.domain.Admin"
	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 'updateAdmin.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>
	<div align="center">
		<%
			Admin entity = (Admin) request.getAttribute("entity");
		%>
		<form action="./updateAdmin.do" method="post">
			序号:<input type="text" disabled="disabled"
				value="<%=entity.getId()%>" /><br /> 用户名:<input type="text"
				name="name" value="<%=entity.getName()%>" /><br /> <input
				type="hidden" name="oper" value="update" /> <input type="hidden"
				name="id" value="<%=entity.getId()%>" /><br /> <input
				type="submit" value="更新" />
		</form>

	</div>
</body>
</html>

src/db.properties

url=jdbc\:mysql\://localhost\:3306/news?useUnicode\=true&characterEncoding\=UTF-8
user=root
pass=root
driverClassName=com.mysql.jdbc.Driver


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值