基于java新闻发布管理系统
新闻发布系统是基于java和mysql开发,B/S架构的系统,系统主要分类用户和管理员两个角色,其中用户主要功能是查看注册登录系统,查看新闻资讯信息,对新闻进行点评,搜索信息,修改个人信息,管理员则是对新闻栏目,新闻信息,注册用户等信息进行管理。本系统结构简单,jsp开发,适合作为java毕业设计和java课程设计来参考和学习。
一.技术环境
JDK版本:1.8
IDE工具:eclipse
数据库: mysql 5.6
编程语言: Java
tomcat: 8.0
详细技术:HTML+CSS+JS+JSP+JAVA+Servlet+MYSQL
二.项目文件(项目获取请看文末官网)
三.系统功能
四.代码示例
package servlet.admin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.Admin;
import bean.PageBean;
import bean.User;
import dao.AdminDao;
import dao.OrderDao;
import dao.UserDao;
import dao.impl.AdminDaoImpl;
import dao.impl.OrderDaoImpl;
import dao.impl.UserDaoImpl;
import net.sf.json.JSONObject;
/**
* Servlet implementation class UserManageServlet
*/
@WebServlet("/jsp/admin/UserManageServlet")
public class UserManageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String USERLIST_PATH="userManage/userList.jsp";//用户列表页面地址
private static final String USERADD_PATH="userManage/userAdd.jsp";//用户增加页面地址
private static final String USEREDIT_PATH="userManage/userEdit.jsp";//用户修改页面地址
private static final String USERDETAIL_PATH="userManage/userDetail.jsp";//用户修改页面地址
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter("action");
if(action==null) {
action="list";
}
switch(action){
case "list":
userList(request,response);
break;
case "add":
userAdd(request,response);
break;
case "update":
userUpdate(request,response);
break;
case "edit":
userEdit(request,response);
break;
case "del":
userDel(request,response);
break;
case "batDel":
userBatDel(request,response);
break;
case "find":
adminFind(request,response);
break;
case "detail":
datail(request,response);
break;
case "seach":
seachUser(request,response);
}
}
private void seachUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao ud=new UserDaoImpl();
int curPage=1;
String page=request.getParameter("page");
if(page!=null){
curPage=Integer.parseInt(page);
}
//获取xml中设置的每页显示大小参数
int maxSize=Integer.parseInt(request.getServletContext().getInitParameter("maxPageSize"));
String username = request.getParameter("username");
PageBean pageBean=null;
if(username != null && username != "") {
pageBean=new PageBean(curPage,maxSize,ud.cakeReadCount(username));
request.setAttribute("userList", ud.userList(pageBean,username));
}else {
pageBean=new PageBean(curPage,maxSize,ud.cakeReadCount());
request.setAttribute("userList", ud.userList(pageBean));
}
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher(USERLIST_PATH).forward(request, response);
}
private void datail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
UserDao ud=new UserDaoImpl();
request.setAttribute("userInfo",ud.findUser(Integer.valueOf(id)));//这里回去是User对象
request.getRequestDispatcher(USERDETAIL_PATH).forward(request, response);
}
private void userBatDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ids=request.getParameter("ids");
UserDao ud=new UserDaoImpl();
if(ud.batDelUser(ids)) {
request.setAttribute("userMessage", "用户已批量删除");
}else {
request.setAttribute("userMessage", "用户批量删除失败");
}
//用户删除成功失败都跳转到用户列表页面
userList(request, response);//通过servlet中listUser跳到用户列表
}
private void userDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=Integer.parseInt(request.getParameter("id"));
UserDao ud=new UserDaoImpl();
if(ud.delUser(id)) {
request.setAttribute("userMessage", "用户已删除");
}else {
request.setAttribute("userMessage", "用户删除失败");
}
//用户删除成功失败都跳转到用户列表页面
userList(request, response);//通过servlet中listUser跳到用户列表
}
private void userUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user=new User(
Integer.parseInt(request.getParameter("userId")),
request.getParameter("passWord"),
request.getParameter("name"),
request.getParameter("sex"),
Integer.parseInt(request.getParameter("age")),
request.getParameter("tell"),
request.getParameter("address"),
request.getParameter("enabled"));
UserDao ud=new UserDaoImpl();
if(ud.userUpdate(user)) {
request.setAttribute("userMessage", "用户更新成功");
userList(request, response);//通过servlet中listUser跳到用户列表
}else {
//更新失败跳转到修改页面
request.setAttribute("userMessage", "用户更新失败");
request.setAttribute("userInfo", ud.findUser(Integer.valueOf(user.getUserId())));//这里回去是Admin对象
request.getRequestDispatcher(USEREDIT_PATH).forward(request, response);
}
}
private void userEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
UserDao ud=new UserDaoImpl();
request.setAttribute("userInfo",ud.findUser(Integer.valueOf(id)));//这里回去是User对象
request.getRequestDispatcher(USEREDIT_PATH).forward(request, response);
}
private void adminFind(HttpServletRequest request, HttpServletResponse response) throws IOException {
String userName=request.getParameter("param");
UserDao ud=new UserDaoImpl();
JSONObject json=new JSONObject();
if(ud.findUser(userName)){
json.put("info", "用户名已存在");
json.put("status", "n");
}else{
json.put("info", "用户名可以使用");
json.put("status", "y");
}
response.getWriter().write(json.toString());
}
private void userAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao ad=new UserDaoImpl();
User user=new User(
request.getParameter("userName"),
request.getParameter("passWord"),
request.getParameter("name"),
request.getParameter("sex"),
Integer.parseInt(request.getParameter("age")),
request.getParameter("tell"),
request.getParameter("address"));
user.setEnabled("y");//默认添加的用户启用
//添加之前判断用户名是否在库中存在
if(new AdminDaoImpl().findUser(user.getUserName())){
request.setAttribute("userMessage", "用户添加失败!用户名已存在");
request.getRequestDispatcher(USERADD_PATH).forward(request, response);
}else{
//执行dao层添加操作
if(ad.userAdd(user)){
request.setAttribute("userMessage", "用户添加成功!");
userList(request, response);//通过servlet中listUser跳到用户列表
}else{
request.setAttribute("userMessage", "用户添加失败!");
request.getRequestDispatcher(USERADD_PATH).forward(request, response);
}
}
}
private void userList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao ud=new UserDaoImpl();
int curPage=1;
String page=request.getParameter("page");
if(page!=null){
curPage=Integer.parseInt(page);
}
//获取xml中设置的每页显示大小参数
int maxSize=Integer.parseInt(request.getServletContext().getInitParameter("maxPageSize"));
PageBean pageBean=new PageBean(curPage,maxSize,ud.cakeReadCount());
request.setAttribute("userList", ud.userList(pageBean));
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher(USERLIST_PATH).forward(request, response);
}
}
五.项目截图