javaweb实验:Java Web综合应用开发

javaweb实验:Java Web综合应用开发

码农世界 2024-05-28 前端 123 次浏览 0个评论

目录

  • 前言
  • 实验目的
  • 实验原理
  • 实验内容
  • 实验过程
    • 项目结构
    • 代码实现
    • Java代码
      • controller层
        • AddNewsServlet类
        • DeletrNewsServlet类
        • LoginServlet类
        • LogoutServlet类
        • QueryAllNewsServlet类
        • QueryNewsServlet类
        • RegisterServlet类
        • UpdateNewsServlet类
        • dao层
          • NewsDao类
          • UserDao类
          • daoimpl层
            • NewsDaoImpl类
            • UserDaoImpl类
            • entity层
              • News类
              • PageBean类
              • User类
              • service层
                • NewsService类
                • UserService类
                • serviceimpl层
                  • NewsServiceImpl类
                  • UserServiceImpl类
                  • util层
                  • jsp代码
                      • addNews
                      • displayNews
                      • login
                      • login_failure
                      • https://blog.csdn.net/m0_72471315/article/details/queryAllNews
                      • reg_failure
                      • reg_success
                      • register
                      • updateNews
                      • 配置web.xml
                      • 成果
                      • 分页查询
                      • 总结

                        前言

                        本实验的目的是让学生掌握Java Web开发的基本原理和方法,以及MVC设计模式的应用。MVC是一种将程序分为三个部分的设计模式,即模型(Model)、视图(View)和控制器(Controller)。模型负责封装数据和业务逻辑,视图负责展示用户界面,控制器负责接收用户请求并调用模型和视图进行处理。MVC模式可以提高程序的可维护性、可扩展性和可重用性,是一种常用的Web开发模式。

                        本实验要求学生使用JSP、Servlet和JavaBean技术,实现一个简单的在线图书管理系统。该系统可以实现用户注册、登录、浏览图书、借阅图书、归还图书等功能。学生需要按照MVC模式,将程序分为三个层次,即数据访问层、业务逻辑层和表现层。数据访问层使用Druid连接池和JDBC技术,对MySQL数据库进行操作。业务逻辑层使用JavaBean封装数据和业务方法。表现层使用JSP和HTML构建用户界面,并使用Servlet作为控制器,接收用户请求并调用业务逻辑层进行处理。

                        本实验报告将详细介绍本实验的设计思路、实现步骤、运行结果和遇到的问题。希望通过本实验,学生能够深入理解Java Web开发的原理和方法,以及MVC设计模式的优势和应用。

                        实验目的

                        理解MVC模式,并能够熟练使用MVC模式,综合运用JSP、JavaBean(DAO模式)和Servlet等相关技术,进行Web应用程序的开发。

                        实验原理

                        关于MVC模式

                        MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC设计模式被分成三个核心层:模型层、视图层、控制层。它们各自处理自己的任务。各层的任务如下:

                        模型层(Model):完成一个个独立的业务操作组件,一般都是以JavaBean的形式进行定义的。

                        控制层(Controller):此层由Servlet实现,负责所有的用户请求参数,判断请求参数是否合法,根据请求的类型调用JavaBean执行操作并将最终的处理结果交由显示层进行显示。

                        显示层(View):此层主要是负责接收Servlet传递的内容,并且调用JavaBean,将内容显示给用户。

                        如图1所示。使用MVC模式进行Web程序开发,是以Servlet为主体展开的,由Servlet接收所有的客户端请求,根据请求调用相应的JavaBean,并将所有的显示结果交给JSP完成。

                        实验内容

                        [贯穿项目] 使用MVC模式,综合运用JSP、JavaBean(DAO模式)和Servlet等相关技术,实现新闻管理系统。要求提供如下功能:用户登录,用户注册,新闻的增、查、改、删。

                        关于创建项目创建数据库等内容在此就不多赘述

                        实验过程

                        项目结构

                        代码实现

                        Java代码

                        controller层

                        AddNewsServlet类
                        import java.io.IOException;
                        import java.text.DateFormat;
                        import java.text.ParseException;
                        import java.text.SimpleDateFormat;
                        import java.util.Date;
                        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 nuc.ss.entity.News;
                        import nuc.ss.service.NewsService;
                        import nuc.ss.serviceimpl.NewsServiceImpl;
                        @WebServlet("/addNews")
                        public class AddNewsServlet extends HttpServlet {
                        	
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                            	// 1、解决编码问题
                            	req.setCharacterEncoding("utf-8");
                            	resp.setCharacterEncoding("utf-8");
                            	resp.setContentType("text/html;charset=utf-8");
                            	
                            	//2、接受前台数据,并将前台数据封装成一个News对象
                            	String title = req.getParameter("title");
                            	String author = req.getParameter("author");
                            	String content = req.getParameter("content");
                            	String date_str = req.getParameter("enterdate");
                            	String hot_str = req.getParameter("hot");
                            	// String-->Util.Date
                            	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                            	Date date;
                            	int hot;
                            	News news = null;
                        		try {
                        			date = df.parse(date_str);
                        			hot = Integer.parseInt(hot_str);			
                        	    	news = new News(0, title, author, content, date, hot);
                        		} catch (ParseException e) {
                        			e.printStackTrace();
                        		}
                        		
                        		// 3、调用service层,然后传入news
                        		NewsService ns = new NewsServiceImpl();
                        		
                        		int n = ns.addNews(news);
                        		
                        		// 4、处理结果
                            	if(n >0) {
                            		//插入成功
                            		resp.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/queryAllNews"); 
                            	}else {
                            		// 插入失败
                            		req.getRequestDispatcher("https://blog.csdn.net/m0_72471315/article/details/addNews.jsp").forward(req, resp);
                            		
                            	}
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// TODO Auto-generated method stub
                        		doGet(req, resp);
                        	}
                        }
                        
                        DeletrNewsServlet类
                        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 nuc.ss.service.NewsService;
                        import nuc.ss.serviceimpl.NewsServiceImpl;
                        @WebServlet("/deleteNews")
                        public class DeleteNewsServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                            	// 1、解决编码问题
                            	req.setCharacterEncoding("utf-8");
                            	//resp.setCharacterEncoding("utf-8");
                            	resp.setContentType("text/html;charset=utf-8");
                            	// 2、获取前台传过来的id
                            	int id = Integer.parseInt(req.getParameter("id"));
                            	// 3、调用Service层
                            	NewsService ns = new NewsServiceImpl();
                            	int n = ns.removeNews(id);
                            	// 4、处理结果
                            	if(n>0) {
                            		//删除成功,则直接刷新页面
                            		resp.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/queryAllNews");
                            	}else {
                            		resp.getWriter().println("删除失败!");
                            	}
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// TODO Auto-generated method stub
                        		doGet(req, resp);
                        	}
                        }
                        
                        LoginServlet类
                        import java.io.IOException;
                        import javax.servlet.ServletException;
                        import javax.servlet.annotation.WebServlet;
                        import javax.servlet.http.Cookie;
                        import javax.servlet.http.HttpServlet;
                        import javax.servlet.http.HttpServletRequest;
                        import javax.servlet.http.HttpServletResponse;
                        import nuc.ss.entity.User;
                        import nuc.ss.service.UserService;
                        import nuc.ss.serviceimpl.UserServiceImpl;
                        @WebServlet("/login")
                        public class LoginServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
                        		//获取前台数据
                        	   request.setCharacterEncoding("UTF-8");
                        	   String id = request.getParameter("id");
                        	   String password = request.getParameter("password");
                        	   String remenber = request.getParameter("remenber");
                        	   String name;
                        	   if((!(name=check(id,password)).equals(""))){  //登录成功,跳转到成功页
                        			Cookie cookie;
                        		    System.out.println("remenber=" + remenber);// on, null
                        	   		if(remenber != null) {
                        		    		cookie = new Cookie("Cookie2020", id+"#"+password+"#"+remenber);
                        		    		cookie.setMaxAge(45);
                        		    		response.addCookie(cookie);
                        		    } else {
                        		    		cookie = new Cookie("Cookie2020", id+"#"+password+"#"+remenber);
                        		    		cookie.setMaxAge(0);
                        		    		response.addCookie(cookie); 	
                        		    }
                        	   		
                        	   		request.getSession().setAttribute("username", name);
                        	   		request.getSession().setMaxInactiveInterval(30);// 30秒
                        			response.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/queryAllNews");
                        			
                        	   }else{              //登录失败,跳转到失败页
                        		   response.sendRedirect("login_failure.jsp");
                        	   }
                        	}
                        	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                        		doGet(request, response);
                        	}
                        	
                        	private String check(String userid, String userpwd){
                        		String uname = "";
                        		UserService us = new UserServiceImpl();
                        		User user = us.login(userid, userpwd);
                        		
                        		if(user != null){  //登录成功
                        			uname = user.getUname();
                        		}
                        		
                        		return uname;
                        	 }
                        }
                        
                        LogoutServlet类
                        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 javax.servlet.http.HttpSession;
                        @WebServlet("/https://blog.csdn.net/m0_72471315/article/details/logout")
                        public class LogoutServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                            	HttpSession session = request.getSession();
                            	session.removeAttribute("username");
                            	
                            	//跳转到https://blog.csdn.net/m0_72471315/article/details/login.jsp
                           		response.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/login.jsp");
                        	}
                        	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                        		doGet(request, response);
                        	}
                        }
                        
                        QueryAllNewsServlet类
                        import nuc.ss.entity.News;
                        import nuc.ss.entity.PageBean;
                        import nuc.ss.service.NewsService;
                        import nuc.ss.serviceimpl.NewsServiceImpl;
                        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 java.io.IOException;
                        @WebServlet("/https://blog.csdn.net/m0_72471315/article/details/queryAllNews")
                        public class QueryAllNewsServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// 1、调用service层,查询新闻
                        		NewsService ns = new NewsServiceImpl();
                        		List list = ns.https://blog.csdn.net/m0_72471315/article/details/queryAllNews();
                        		// 2、处理结果
                        		req.setAttribute("list", list);
                            	req.getRequestDispatcher("https://blog.csdn.net/m0_72471315/article/details/queryAllNews.jsp").forward(req, resp);
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		doGet(req, resp);
                        	}
                        }
                        
                        QueryNewsServlet类
                        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 nuc.ss.entity.News;
                        import nuc.ss.service.NewsService;
                        import nuc.ss.serviceimpl.NewsServiceImpl;
                        @WebServlet("/queryNews")
                        public class QueryNewsServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                            	// 1、解决编码问题
                            	req.setCharacterEncoding("utf-8");
                            	//resp.setCharacterEncoding("utf-8");
                            	resp.setContentType("text/html;charset=utf-8");
                            	
                            	//2、接受前台数据
                            	int id = Integer.parseInt(req.getParameter("id"));
                            	int flag = Integer.parseInt(req.getParameter("flag"));
                            	
                            	// 3、调用service层,查询指定id的新闻信息
                        		NewsService ns = new NewsServiceImpl();
                        		
                        		News news = ns.queryOneNews(id);
                        		
                        		// 4、处理结果
                        		if(news != null) {
                        			req.setAttribute("news", news);
                        			if(flag == 1) {
                        				req.getRequestDispatcher("updateNews.jsp").forward(req, resp);
                        			}else {
                        				req.getRequestDispatcher("displayNews.jsp").forward(req, resp);	
                        			}
                        		}
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// TODO Auto-generated method stub
                        		doGet(req, resp);
                        	}
                        }
                        
                        RegisterServlet类
                        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 nuc.ss.entity.User;
                        import nuc.ss.service.UserService;
                        import nuc.ss.serviceimpl.UserServiceImpl;
                        @WebServlet("/register")
                        public class RegisterServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		//设置编码格式
                        		req.setCharacterEncoding("UTF-8");
                        		//resp.setCharacterEncoding("utf-8");
                        		resp.setContentType("text/html;charset=utf-8");
                        		
                        		//获取前台数据
                        		String id = req.getParameter("uid");
                        		String name = req.getParameter("uname");
                        		String password = req.getParameter("upassword");
                        		String password2 = req.getParameter("upassword2");
                        		String sex = req.getParameter("usex");
                        		String email = req.getParameter("uemail");
                        		java.util.Date regdate = new java.util.Date();
                        		
                        		String uname = check(id,name,password,password2,sex,email,regdate);
                        		
                        		if(!( "".equals(uname) || "######".equals(uname)
                        				|| "******".equals(uname)|| "$$$$$$".equals(uname))){  //注册成功,跳转到成功页
                        			req.getSession().setAttribute("info", uname);
                        			resp.sendRedirect("reg_success.jsp");
                        			
                        		}else{              //注册失败,跳转到失败页
                        			String info = "";
                        			if("######".equals(uname)){
                        				info = "错误:登录ID已经存在!";
                        			}
                        			if("******".equals(uname)){
                        				info = "错误:两次输入密码不一致!";
                        			}
                        			if("$$$$$$".equals(uname)){
                        				info = "错误:所有信息都必须填写!";
                        			}		
                        			req.getSession().setAttribute("info", info);
                        			resp.sendRedirect("reg_failure.jsp");
                        		}
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// TODO Auto-generated method stub
                        		doGet(req, resp);
                        	}
                        	
                        	private String check(String userid, String username, 
                        		      String userpwd, String userpwd2,
                        		      String usersex, String useremail, 
                        		      java.util.Date userregdate){
                        	
                        		if("".equals(userid) || "".equals(username) || "".equals(userpwd) || "".equals(userpwd2)){
                        			return "$$$$$$";
                        		}
                        		
                        		if(!(userpwd.equals(userpwd2))){
                        			return "******";
                        		}
                        		
                        		User user = new User(userid,username,userpwd,usersex,useremail,userregdate);	
                        		
                        		UserService us = new UserServiceImpl();
                        		
                        		int n = us.register(user);
                        		
                        		if (n == -1) {
                        			return "######";
                        		}
                        		
                        		return username;
                        	} 
                        }
                        
                        UpdateNewsServlet类
                        import java.io.IOException;
                        import java.text.DateFormat;
                        import java.text.ParseException;
                        import java.text.SimpleDateFormat;
                        import java.util.Date;
                        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 nuc.ss.entity.News;
                        import nuc.ss.service.NewsService;
                        import nuc.ss.serviceimpl.NewsServiceImpl;
                        @WebServlet("/updateNews")
                        public class UpdateNewsServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                            	// 1、解决编码问题
                            	req.setCharacterEncoding("utf-8");
                            	resp.setCharacterEncoding("utf-8");
                            	resp.setContentType("text/html;charset=utf-8");
                            	
                            	//2、接受前台数据,并将前台数据封装成一个News对象
                            	int id = Integer.parseInt(req.getParameter("id"));    	
                            	String title = req.getParameter("title");
                            	String author = req.getParameter("author");
                            	String content = req.getParameter("content");
                            	String date_str = req.getParameter("enterdate");
                            	String hot_str = req.getParameter("hot");
                            	// String-->Util.Date
                            	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                            	Date date;
                            	int hot;
                            	News news = null;
                        		try {
                        			date = df.parse(date_str);
                        			hot = Integer.parseInt(hot_str);			
                        	    	news = new News(id, title, author, content, date, hot);
                        		} catch (ParseException e) {
                        			e.printStackTrace();
                        		}
                        		
                        		// 3、调用service层,然后传入news
                        		NewsService ns = new NewsServiceImpl();
                        		
                        		int n = ns.updateNews(news);
                        		
                        		// 4、处理结果
                            	if(n >0) {
                            		//修改成功
                            		resp.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/queryAllNews"); 
                            	}else {
                            		// 修改失败
                            		resp.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/queryAllNews");
                            	}
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// TODO Auto-generated method stub
                        		doGet(req, resp);
                        	}
                        }
                        

                        dao层

                        这里是dao的接口

                        NewsDao类
                        import nuc.ss.entity.News;
                        import java.util.List;
                        public interface NewsDao {
                        	static int getTotalSize() {
                        		return 0;
                        	}
                        	static List queryNewsByPage(int startIndex, int pageSize) {
                        		return null;
                        	}
                        	int insertOne(News news);
                        	List selectAll();
                        	int deleteOne(int id);
                        	News selectOne(int id);
                        	int updateOne(News news);
                        }
                        
                        UserDao类
                        import nuc.ss.entity.User;
                        public interface UserDao {
                        	int insertOne(User user);
                        	User findOne(String uid, String pwd);
                        	
                        	boolean findOne(String uid);
                        }
                        

                        daoimpl层

                        NewsDaoImpl类
                        import nuc.ss.dao.NewsDao;
                        import nuc.ss.entity.News;
                        import nuc.ss.entity.PageBean;
                        import nuc.ss.util.DBUtil;
                        import java.sql.Connection;
                        import java.sql.PreparedStatement;
                        import java.sql.ResultSet;
                        import java.sql.SQLException;
                        import java.util.ArrayList;
                        import java.util.List;
                        public class NewsDaoImpl implements NewsDao {
                        	@Override
                        	public int insertOne(News news) {
                        		String sql = "insert into t_news values(default,?,?,?,?,?)";
                        		Object[] objs = {news.getTitle(), news.getAuthor(), news.getContent(), news.getEnterdate(), news.getHot()};
                        		int n = DBUtil.excuteDML(sql, objs);
                        		return n;
                        	}
                        	@Override
                        	public List selectAll() {
                        		Connection conn = DBUtil.getConnection();
                        		String sql = "select * from t_news";
                        		PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
                        		ResultSet rs = null;
                        		List list = new ArrayList();
                        		try {
                        			rs = ps.executeQuery();
                        			while(rs.next()) {
                        				News news = new News(rs.getInt("id"), rs.getString("title"),
                        						rs.getString("author"), rs.getString("content"),
                        						rs.getDate("enterdate"), rs.getInt("hot"));
                        				list.add(news);
                        				
                        			}
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		} finally {
                        			DBUtil.closeAll(rs, ps, conn);
                        		}
                        		return list;
                        	}
                        	@Override
                        	public int deleteOne(int id) {
                        		String sql = "delete from t_news where id=?";
                        		Object[] objs = {id};
                        		int n = DBUtil.excuteDML(sql, objs);
                        		return n;
                        	}
                        	@Override
                        	public News selectOne(int id) {
                        		Connection conn = DBUtil.getConnection();
                        		String sql = "select * from t_news where id=?";
                        		PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
                        		try {
                        			ps.setInt(1, id);
                        		} catch (SQLException e1) {
                        			e1.printStackTrace();
                        		}
                        		ResultSet rs = null;
                        		News news = null;
                        		try {
                        			rs = ps.executeQuery();
                        			if(rs.next()) {
                        				news = new News(rs.getInt("id"), rs.getString("title"),
                        						rs.getString("author"), rs.getString("content"),
                        						rs.getDate("enterdate"), rs.getInt("hot"));
                        			}
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		} finally {
                        			DBUtil.closeAll(rs, ps, conn);
                        		}
                        		return news;		
                        	}
                        	@Override
                        	public int updateOne(News news) {
                        		String sql = "update t_news set title=?,author=?,content=?,enterdate=?,hot=? where id=?";
                        		Object[] objs = {news.getTitle(), news.getAuthor(), news.getContent(),
                        				         news.getEnterdate(), news.getHot(), news.getId()};
                        		int n = DBUtil.excuteDML(sql, objs);
                        		return n;
                        	}
                        	public PageBean queryNewsByPage(int currentPage, int pageSize) {
                        		// 创建一个PageBean对象
                        		PageBean pageBean = new PageBean();
                        		// 设置当前页数和每页记录数
                        		pageBean.setCurrentPage(currentPage);
                        		pageBean.setPageSize(pageSize);
                        		// 调用dao层的方法获取总记录数
                        		int totalSize = NewsDao.getTotalSize();
                        		// 设置总记录数
                        		pageBean.setTotalSize(totalSize);
                        		// 计算总页数
                        		int totalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize + 1);
                        		// 设置总页数
                        		pageBean.setTotalPage(totalPage);
                        		// 计算开始索引
                        		int startIndex = (currentPage - 1) * pageSize;
                        		// 调用dao层的方法获取当前页数据
                        		List list = NewsDao.queryNewsByPage(startIndex, pageSize);
                        		// 设置当前页数据
                        		pageBean.setList(list);
                        		// 返回PageBean对象
                        		return pageBean;
                        	}
                        }
                        
                        UserDaoImpl类
                        import java.sql.Connection;
                        import java.sql.PreparedStatement;
                        import java.sql.ResultSet;
                        import java.sql.SQLException;
                        import nuc.ss.dao.UserDao;
                        import nuc.ss.entity.User;
                        import nuc.ss.util.DBUtil;
                        public class UserDaoImpl implements UserDao{
                        	@Override
                        	public int insertOne(User user) {
                        		
                        		String sql = "insert into t_user values (?,?,?,?,?,?)";
                        		
                        		Object[] objs = {user.getUid(), user.getUname(), user.getUpassword(), user.getUsex(), user.getUemail(), user.getUregdate()};
                        		int n = DBUtil.excuteDML(sql, objs);
                        		
                        		return n;
                        	}
                        	@Override
                        	public User findOne(String uid, String pwd) {
                        		Connection conn = DBUtil.getConnection();
                        		String sql = "select * from t_user where uid=? and upassword=?";
                        		PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
                        		ResultSet rs = null;
                        		User user = null;
                        		try {
                        			ps.setString(1, uid);
                        			ps.setString(2, pwd);
                        			
                        			rs = ps.executeQuery();
                        			if(rs.next()) {
                        				user = new User(rs.getString("uid"), rs.getString("uname"), rs.getString("upassword"), rs.getString("usex"), rs.getString("uemail"), rs.getDate("uregdate"));		
                        			}
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		} finally{
                        			DBUtil.closeAll(rs, ps, conn);
                        		}
                        		
                        		return user;
                        	}
                        	@Override
                        	public boolean findOne(String uid) {
                        		Connection conn = DBUtil.getConnection();
                        		String sql = "select * from t_user where uid=?";
                        		PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
                        		ResultSet rs = null;
                        		try {
                        			ps.setString(1, uid);
                        			
                        			rs = ps.executeQuery();
                        			if(rs.next()) {
                        				return true;
                        			}
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		} finally{
                        			DBUtil.closeAll(rs, ps, conn);
                        		}
                        		
                        		return false;
                        	}
                        }
                        

                        entity层

                        News类
                        import java.util.Date;
                        public class News {
                        	private static int id;
                        	private String title;
                        	private String author;
                        	private String content;
                        	private Date enterdate;
                        	private int hot;
                        	public News(int id, String title, String author, String content, Date enterdate, int hot) {
                        		super();
                        		this.id = id;
                        		this.title = title;
                        		this.author = author;
                        		this.content = content;
                        		this.enterdate = enterdate;
                        		this.hot = hot;
                        	}
                        	public static int getId() {
                        		return id;
                        	}
                        	public void setId(int id) {
                        		this.id = id;
                        	}
                        	public String getTitle() {
                        		return title;
                        	}
                        	public void setTitle(String title) {
                        		this.title = title;
                        	}
                        	public String getAuthor() {
                        		return author;
                        	}
                        	public void setAuthor(String author) {
                        		this.author = author;
                        	}
                        	public String getContent() {
                        		return content;
                        	}
                        	public void setContent(String content) {
                        		this.content = content;
                        	}
                        	public Date getEnterdate() {
                        		return enterdate;
                        	}
                        	public void setEnterdate(Date enterdate) {
                        		this.enterdate = enterdate;
                        	}
                        	public int getHot() {
                        		return hot;
                        	}
                        	public void setHot(int hot) {
                        		this.hot = hot;
                        	}
                        	@Override
                        	public String toString() {
                        		return "News [id=" + id + ", title=" + title + ", author=" + author + ", content=" + content + ", enterdate="
                        				+ enterdate + ", hot=" + hot + "]";
                        	}	
                        }
                        
                        PageBean类
                        import java.util.List;
                        public class PageBean {
                            private Integer currentPage; // 当前页数
                            private Integer totalPage; // 总页数
                            private Integer totalSize; // 总记录数
                            private Integer pageSize; // 每页记录数
                            private List list; // 当前页数据
                            // 构造方法,根据总记录数、当前页数和每页记录数计算总页数
                            public PageBean() {
                                this.totalSize = totalSize;
                                this.currentPage = currentPage;
                                this.pageSize = pageSize;
                                this.totalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize + 1);
                            }
                            // getter和setter方法
                            public Integer getCurrentPage() {
                                return currentPage;
                            }
                            public void setCurrentPage(Integer currentPage) {
                                this.currentPage = currentPage;
                            }
                            public Integer getTotalPage() {
                                return totalPage;
                            }
                            public void setTotalPage(Integer totalPage) {
                                this.totalPage = totalPage;
                            }
                            public Integer getTotalSize() {
                                return totalSize;
                            }
                            public void setTotalSize(Integer totalSize) {
                                this.totalSize = totalSize;
                            }
                            public Integer getPageSize() {
                                return pageSize;
                            }
                            public void setPageSize(Integer pageSize) {
                                this.pageSize = pageSize;
                            }
                            public List getList() {
                                return list;
                            }
                            public void setList(List list) {
                                this.list = list;
                            }
                        }
                        
                        User类
                        import java.util.Date;
                        public class User {
                        	private String uid;
                        	private String uname;
                        	private String upassword;
                        	private String usex;
                        	private String uemail;
                        	private Date uregdate;
                        	
                        	public User(String uid, String uname, String upassword, String usex, String uemail, Date uregdate) {
                        		super();
                        		this.uid = uid;
                        		this.uname = uname;
                        		this.upassword = upassword;
                        		this.usex = usex;
                        		this.uemail = uemail;
                        		this.uregdate = uregdate;
                        	}
                        	
                        	public String getUid() {
                        		return uid;
                        	}
                        	public void setUid(String uid) {
                        		this.uid = uid;
                        	}
                        	public String getUname() {
                        		return uname;
                        	}
                        	public void setUname(String uname) {
                        		this.uname = uname;
                        	}
                        	public String getUpassword() {
                        		return upassword;
                        	}
                        	public void setUpassword(String upassword) {
                        		this.upassword = upassword;
                        	}
                        	public String getUsex() {
                        		return usex;
                        	}
                        	public void setUsex(String usex) {
                        		this.usex = usex;
                        	}
                        	public String getUemail() {
                        		return uemail;
                        	}
                        	public void setUemail(String uemail) {
                        		this.uemail = uemail;
                        	}
                        	public Date getUregdate() {
                        		return uregdate;
                        	}
                        	public void setUregdate(Date uregdate) {
                        		this.uregdate = uregdate;
                        	}
                        	@Override
                        	public String toString() {
                        		return "User [uid=" + uid + ", uname=" + uname + ", upassword=" + upassword + ", usex=" + usex + ", uemail="
                        				+ uemail + ", uregdate=" + uregdate + "]";
                        	}
                        }
                        

                        service层

                        NewsService类
                        import nuc.ss.entity.News;
                        import nuc.ss.entity.PageBean;
                        import java.util.List;
                        public interface NewsService {
                        	int addNews(News news);
                        	List https://blog.csdn.net/m0_72471315/article/details/queryAllNews();
                        	int removeNews(int id);
                        	News queryOneNews(int id);
                        	int updateNews(News news);
                            int getTotalCount();
                        	// 声明一个分页查询的方法
                        	PageBean queryNewsByPage(int currentPage, int pageSize);
                        }
                        
                        UserService类
                        import nuc.ss.entity.User;
                        public interface UserService {
                        	int register(User user);
                        	User login(String uid, String pwd);
                        }
                        

                        serviceimpl层

                        NewsServiceImpl类
                        import nuc.ss.dao.NewsDao;
                        import nuc.ss.daoimpl.NewsDaoImpl;
                        import nuc.ss.entity.News;
                        import nuc.ss.entity.PageBean;
                        import nuc.ss.service.NewsService;
                        import java.util.List;
                        public class NewsServiceImpl implements NewsService {
                        	private NewsDao nd = new NewsDaoImpl();
                        	private NewsServiceImpl newsDao;
                        	@Override
                        	public int addNews(News news) {
                        		return nd.insertOne(news);
                        	}
                        	@Override
                        	public List https://blog.csdn.net/m0_72471315/article/details/queryAllNews() {
                        		return nd.selectAll();
                        	}
                        	@Override
                        	public int removeNews(int id) {
                        		
                        		return nd.deleteOne(id);
                        	}
                        	@Override
                        	public News queryOneNews(int id) {
                        		
                        		return nd.selectOne(id);
                        	}
                        	@Override
                        	public int updateNews(News news) {
                        		
                        		return nd.updateOne(news);
                        	}
                        	@Override
                        	public int getTotalCount() {
                        		return 0;
                        	}
                        	@Override
                        	public PageBean queryNewsByPage(int currentPage, int pageSize) {
                        		return null;
                        	}
                        	public void setNewsDao(NewsDao newsDao) {
                        		this.newsDao = (NewsServiceImpl) newsDao;
                        	}
                        }
                        
                        UserServiceImpl类
                        import nuc.ss.dao.UserDao;
                        import nuc.ss.daoimpl.UserDaoImpl;
                        import nuc.ss.entity.User;
                        import nuc.ss.service.UserService;
                        public class UserServiceImpl implements UserService{
                        	private UserDao ud = new UserDaoImpl();
                        	
                        	@Override
                        	public int register(User user) {
                        		if(ud.findOne(user.getUid())){
                        			return -1;
                        		}
                        		return ud.insertOne(user);
                        	}
                        /*	public int register(User user) {
                        		return ud.insertOne(user);
                        	}*/
                        	@Override
                        	public User login(String uid, String pwd) {
                        		
                        		return ud.findOne(uid, pwd);
                        	}
                        }
                        

                        util层

                        public class DBUtil {
                        	public static Connection getConnection() {
                        		Connection connection = null;
                        		try {
                        			Class.forName("com.mysql.jdbc.Driver");
                        			System.out.println("加载成功!");
                        			String url = "jdbc:mysql://localhost:3306/db_news2019?useUnicode=true&characterEncoding=utf-8";
                        			String username = "root";
                        			String password = "12345678";
                        			connection = DriverManager.getConnection(url, username, password);
                        		} catch (ClassNotFoundException e) {
                        			// TODO Auto-generated catch block
                        			e.printStackTrace();
                        			System.out.println("加载失败!驱动类没有找到!");
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        			System.out.println("数据库连接失败!请检查数据库名以及用户帐号!");
                        		}
                        		return connection;
                        	}
                        	// 关闭资源连接connection,statement,resultset
                        	public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
                        		if(resultSet != null) {
                        			try {
                        				resultSet.close();
                        			} catch (SQLException e) {
                        				e.printStackTrace();
                        			}
                        		}
                        		
                        		if(statement != null) {
                        			try {
                        				statement.close();
                        			} catch (SQLException e) {
                        				// TODO Auto-generated catch block
                        				e.printStackTrace();
                        			}
                        		}
                        		
                        		if(connection != null) {
                        			try {
                        				connection.close();
                        			} catch (SQLException e) {
                        				// TODO Auto-generated catch block
                        				e.printStackTrace();
                        			}
                        		}
                        			
                        	}
                        	
                        	// 创建数据库操作对象preparedStatement
                        	public static PreparedStatement getPreparedStatement(Connection connection, String sql) {
                        		PreparedStatement preparedStatement = null;
                        		try {
                        			preparedStatement = connection.prepareStatement(sql);
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		}
                        		return preparedStatement;
                        	}
                        	
                        	// 创建数据库操作对象statement
                        	public static Statement getStatement(Connection connection) {
                        		Statement statement = null;
                        		try {
                        			statement = connection.createStatement();
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		}
                        		return statement;
                        	}
                        	
                        	// 封装DML语句(更新操作)
                        	public static int excuteDML(String sql, Object...objs) {
                        		int n = 0;
                        		Connection connection = null;
                        		PreparedStatement preparedStatement = null;
                        		try {
                        			connection = getConnection();
                        			preparedStatement = getPreparedStatement(connection, sql);
                        			for (int i = 0; i < objs.length; i++) {
                        				preparedStatement.setObject(i+1, objs[i]);
                        			}
                        			n = preparedStatement.executeUpdate();
                        		} catch (SQLException e) {
                        			e.printStackTrace();
                        		}finally {
                        			closeAll(null, preparedStatement, connection);
                        		}
                        		return n;
                        	}
                        	
                        }
                        

                        jsp代码

                        addNews
                        <%@ page language="java" contentType="text/html; charset=utf-8"
                            pageEncoding="utf-8"%>
                        
                        
                        
                        	
                        	增加新闻
                        
                        
                        	
                        新闻题目
                        新闻作者
                        新闻内容
                        新闻日期
                        新闻热度
                        displayNews
                        <%@ page language="java" 
                        	contentType="text/html; charset=UTF-8"
                            pageEncoding="UTF-8"%>
                        <%@page import="java.sql.*"%>
                        <%@page import="nuc.ss.entity.News"%>
                        
                        
                        	
                        	查询新闻详情
                        
                        
                        <% 
                        	News news = (News)request.getAttribute("news");
                        	if(news != null){
                        %>
                        		
                        新闻题目
                        新闻作者
                        新闻内容
                        新闻日期
                        新闻热度
                        <% } %>
                        返回
                        login
                        <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
                        
                        
                        
                        登录程序之表单页面
                        
                        
                        <%
                        	String id = "";
                        	String password = "";
                        	String remenber = null;
                        	Cookie[] cookies = request.getCookies();
                        	if(cookies != null){
                        		for(int i=0; i
                        			if (cookies[i].getName().equals("Cookie2020")) {
                        		           //如果cookie对象的名称为Cookie2019
                        				id = cookies[i].getValue().split("#")[0];  //获取用户名
                        				password = cookies[i].getValue().split("#")[1];  //获取密码
                        				remenber = cookies[i].getValue().split("#")[2];  //获取remenber
                        			}
                        		}
                        	}
                        %>
                        

                        登录操作


                        用户登录
                        登录ID:
                        登录密码:
                                           %> checked <%}%>>记住我
                        如果您尚未注册,请先进行 注册 
                        login_failure
                        <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
                        
                        
                        
                        登录程序之登录成功页面
                        
                        
                        

                        登录操作


                        用户ID或密码错误!请重新登录

                        https://blog.csdn.net/m0_72471315/article/details/queryAllNews
                        <%@ page language="java" contentType="text/html; charset=utf-8"
                            pageEncoding="utf-8"%>
                        <%@page import="java.util.*" %>
                        <%@page import="nuc.ss.service.NewsService"%>
                        <%@page import="nuc.ss.serviceimpl.NewsServiceImpl"%>
                        <%@page import="nuc.ss.entity.News"%>
                        <%@page import="nuc.ss.entity.User"%>
                        
                        
                        
                        	
                        	显示所有新闻
                        	
                        
                        
                        <%
                            if(session.getAttribute("username") == null)
                            {
                        		response.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/login.jsp"); 
                            }
                        %>
                        	
                        登录成功!欢迎[<%=session.getAttribute("username")%>]访问新闻管理系统!

                        增加新闻  安全退出
                        <% List list = (List)request.getAttribute("list"); for(News news: list){ %> <% } %>
                        标题 作者 日期 热度 操作
                        <%=news.getTitle() %> <%=news.getAuthor() %> <%=news.getEnterdate() %> <%=news.getHot() %> 查询详情 修改 删除
                        reg_failure
                        <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
                        
                        
                        
                        登录程序之登录成功页面
                        
                        
                        

                        注册操作


                        <%=session.getAttribute("info")%>请重新注册

                        reg_success
                        <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
                        
                        
                        
                        登录程序之登注册成功页面
                        
                        
                            

                        [<%=session.getAttribute("info")%>]恭喜您--注册成功!

                        5秒后自动跳转到登录页面

                        <% response.setHeader("refresh", "5;https://blog.csdn.net/m0_72471315/article/details/login.jsp"); %>
                        register
                        <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
                        
                        
                        
                        新闻管理系统之用户注册页面
                        
                        
                        

                        注册操作


                        用户注册
                        登录ID:
                        真实姓名:
                        性   别:
                        E-Mail:
                        登录密码:
                        确认密码:
                            
                         返回登录页面 
                        updateNews
                        <%@ page language="java" 
                        	contentType="text/html; charset=UTF-8"
                            pageEncoding="UTF-8"%>
                        <%@page import="java.sql.*"%>
                        <%@page import="nuc.ss.entity.News"%>
                        
                        
                        	
                        	修改新闻
                        
                        
                        <%	
                        	News news = (News)request.getAttribute("news");
                        %>
                        	
                        新闻题目
                        新闻作者
                        新闻内容
                        新闻日期
                        新闻热度

                        返回

                        配置web.xml

                        参考配置

                        
                        
                          ProjforNewsV1_0
                          
                            index.html
                            index.htm
                            index.jsp
                            default.html
                            default.htm
                            https://blog.csdn.net/m0_72471315/article/details/login.jsp
                          
                          
                            LoginServlet
                            nuc.ss.controller.LoginServlet
                          
                          
                            LoginServlet
                            /login
                          
                          
                            LogoutServlet
                            nuc.ss.controller.LogoutServlet
                          
                          
                            LogoutServlet
                            /https://blog.csdn.net/m0_72471315/article/details/logout
                          
                          
                            RegisterServlet
                            nuc.ss.controller.RegisterServlet
                          
                          
                            RegisterServlet
                            /register
                          
                          
                            QueryAllNewsServlet
                            nuc.ss.controller.QueryAllNewsServlet
                          
                          
                            QueryAllNewsServlet
                            /https://blog.csdn.net/m0_72471315/article/details/queryAllNews
                          
                          
                            AddNewsServlet
                            nuc.ss.controller.AddNewsServlet
                          
                          
                            AddNewsServlet
                            /addNews
                          
                          
                            DeleteNewsServlet
                            nuc.ss.controller.DeleteNewsServlet
                          
                          
                            DeleteNewsServlet
                            /deleteNews
                          
                          
                            QueryNewsServlet
                            nuc.ss.controller.QueryNewsServlet
                          
                          
                            QueryNewsServlet
                            /queryNews
                          
                          
                            UpdateNewsServlet
                            nuc.ss.controller.UpdateNewsServlet
                          
                          
                            UpdateNewsServlet
                            /updateNews
                          
                        
                        

                        成果

                        登陆界面

                        注册界面

                        新闻展示页面

                        增加新闻

                        新闻详情

                        修改页面

                        删除新闻

                        分页查询

                        增加一个分页查询的功能

                        servlet代码

                        @WebServlet("/https://blog.csdn.net/m0_72471315/article/details/queryAllNews")
                        public class QueryAllNewsServlet extends HttpServlet {
                        	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		// 1、调用service层,查询新闻
                        		NewsService ns = new NewsServiceImpl();
                        		int currentPage = 1; // 默认为第一页
                        		int pageSize = 10; // 默认每页显示10条
                        		String currentPageStr = req.getParameter("currentPage"); // 获取请求参数中的当前页数
                        		if (currentPageStr != null && !"".equals(currentPageStr)) { // 如果不为空,则转换为整数
                        			currentPage = Integer.parseInt(currentPageStr);
                        		}
                        		String pageSizeStr = req.getParameter("pageSize"); // 获取请求参数中的每页记录数
                        		if (pageSizeStr != null && !"".equals(pageSizeStr)) { // 如果不为空,则转换为整数
                        			pageSize = Integer.parseInt(pageSizeStr);
                        		}
                        		PageBean pageBean = ns.queryNewsByPage(currentPage, pageSize); // 调用service层的方法获取PageBean对象
                        		// 2、处理结果
                        		req.setAttribute("pageBean", pageBean); // 将PageBean对象存入request域中
                        		req.getRequestDispatcher("https://blog.csdn.net/m0_72471315/article/details/queryAllNews.jsp").forward(req, resp); // 转发到JSP页面显示数据
                        	}
                        	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                        		doGet(req, resp);
                        	}
                        }
                        

                        jsp代码

                        <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
                        <%@page import="java.util.*" %>
                        <%@page import="nuc.ss.service.NewsService"%>
                        <%@page import="nuc.ss.serviceimpl.NewsServiceImpl"%>
                        <%@page import="nuc.ss.entity.News"%>
                        <%@page import="nuc.ss.entity.User"%>
                        
                        
                        
                            
                            显示所有新闻
                            
                        
                        
                            <% if (session.getAttribute("username") == null) {
                            response.sendRedirect("https://blog.csdn.net/m0_72471315/article/details/login.jsp");
                        }%>
                        
                        登录成功!欢迎[<%=session.getAttribute("username")%>]访问新闻管理系统!

                        增加新闻 安全退出
                        标题 作者 日期 热度 操作
                        ${news.title} ${news.author} ${news.enterdate} ${news.hot} 查询详情 修改 删除
                        第${pageBean.currentPage}/${pageBean.totalPage}页 总记录数:${pageBean.totalSize}条 每页${pageBean.pageSize}条 首页 上一页 下一页 末页

                        在entity层中添加一个pagebean类

                        public class PageBean {
                            private Integer currentPage; // 当前页数
                            private Integer totalPage; // 总页数
                            private Integer totalSize; // 总记录数
                            private Integer pageSize; // 每页记录数
                            private List list; // 当前页数据
                            // 构造方法,根据总记录数、当前页数和每页记录数计算总页数
                            public PageBean() {
                                this.totalSize = totalSize;
                                this.currentPage = currentPage;
                                this.pageSize = pageSize;
                                this.totalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize + 1);
                            }
                            // getter和setter方法
                            public Integer getCurrentPage() {
                                return currentPage;
                            }
                            public void setCurrentPage(Integer currentPage) {
                                this.currentPage = currentPage;
                            }
                            public Integer getTotalPage() {
                                return totalPage;
                            }
                            public void setTotalPage(Integer totalPage) {
                                this.totalPage = totalPage;
                            }
                            public Integer getTotalSize() {
                                return totalSize;
                            }
                            public void setTotalSize(Integer totalSize) {
                                this.totalSize = totalSize;
                            }
                            public Integer getPageSize() {
                                return pageSize;
                            }
                            public void setPageSize(Integer pageSize) {
                                this.pageSize = pageSize;
                            }
                            public List getList() {
                                return list;
                            }
                            public void setList(List list) {
                                this.list = list;
                            }
                        }
                        

                        在dao层增加一个方法

                        public PageBean queryNewsByPage(int currentPage, int pageSize) {
                        		// 创建一个PageBean对象
                        		PageBean pageBean = new PageBean();
                        		// 设置当前页数和每页记录数
                        		pageBean.setCurrentPage(currentPage);
                        		pageBean.setPageSize(pageSize);
                        		// 调用dao层的方法获取总记录数
                        		int totalSize = NewsDao.getTotalSize();
                        		// 设置总记录数
                        		pageBean.setTotalSize(totalSize);
                        		// 计算总页数
                        		int totalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize + 1);
                        		// 设置总页数
                        		pageBean.setTotalPage(totalPage);
                        		// 计算开始索引
                        		int startIndex = (currentPage - 1) * pageSize;
                        		// 调用dao层的方法获取当前页数据
                        		List list = NewsDao.queryNewsByPage(startIndex, pageSize);
                        		// 设置当前页数据
                        		pageBean.setList(list);
                        		// 返回PageBean对象
                        		return pageBean;
                        	}
                        

                        展示一下成果吧

                        以上就是这次实验的全部内容啦

                        总结

                        本文是对javaweb实验的总结,主要介绍了基于MVC模式的web应用开发的过程和心得。MVC模式是一种设计模式,将web应用分为三个层次:模型(Model)、视图(View)和控制器(Controller)。模型负责封装业务逻辑和数据,视图负责展示用户界面,控制器负责处理用户请求和调用模型和视图。MVC模式的优点是可以实现高内聚低耦合,提高代码的可维护性和可重用性。

                        在本实验中,我使用了idea作为开发工具,Tomcat作为web服务器,MySQL作为数据库,JSP和Servlet作为视图和控制器,JavaBean作为模型。我实现了一个简单的登录系统和新闻管理系统,包括登录、注册、查询、修改、删除等功能。在开发过程中,我遵循了MVC模式的原则,将不同的功能分配到不同的层次,使得代码结构清晰,易于修改和扩展。我也遇到了一些困难和问题,例如如何保持用户的登录状态,如何防止SQL注入攻击,如何处理中文乱码等。通过查阅资料和调试代码,我逐一解决了这些问题,并从中学习到了很多知识和技巧。

                        总之,通过本实验,我深刻理解了MVC模式的原理和优势,掌握了基于MVC模式的web应用开发的方法和步骤,提高了我的编程能力和解决问题的能力。我认为这是一次非常有意义和有价值的实验,对于我的未来学习和工作都有很大的帮助。

转载请注明来自码农世界,本文标题:《javaweb实验:Java Web综合应用开发》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,123人围观)参与讨论

还没有评论,来说两句吧...

Top