使用基本MVC2模式创建新闻网站

一、新闻首页

二、登录中心

三、操作页面

四、富文本编辑页面

五、新闻详情页

六、项目结构和关键代码

operateServlet.java

package com.hedong.Control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

/**1、这是操作中心,包括管理员登录、新闻的增删改查。
 * 2、对于不同的操作,程序会去调用服务层的相应功能。
 */



/**
 * Servlet implementation class operateServlet
 */
@WebServlet("/operateServlet")
public class operateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public operateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out=response.getWriter();
		//获取news参数和登录账号密码参数
		String action=request.getParameter("action");
		String myType=request.getParameter("newsType");
		String myTitle=request.getParameter("newsTitle");
		String myContent=request.getParameter("editorValue");
		String writer=request.getParameter("newsWriter");
		String myID=request.getParameter("newsID");
		String User=request.getParameter("userName");
		String Password=request.getParameter("passWord");		

		
		if(action.equals("add")) {
			
			com.hedong.Service.NewsService newsService=new com.hedong.Service.NewsService();//调用服务层添加数据
			newsService.InsertNews(myType,myTitle,myContent,writer);
			response.sendRedirect("/NewsWebsite/GoPage?flag=showlist");//重新打开新闻管理页面
			
		}else if(action.equals("dele")) {
			
			com.hedong.Service.NewsService newsService=new com.hedong.Service.NewsService();//调用服务层删除数据
			newsService.DeleteNews(myID);
			response.sendRedirect("/NewsWebsite/GoPage?flag=showlist");//重新打开新闻管理页面
			
		}else if(action.equals("edit")) {
			
			com.hedong.Service.NewsService newsService=new com.hedong.Service.NewsService();//调用服务层获取数据
			newsService.EditNews(myType,myTitle,myContent,writer,myID);
			response.sendRedirect("/NewsWebsite/GoPage?flag=showlist");//重新打开新闻管理页面
			
		}else if(action.equals("login")) {
			
			com.hedong.Service.NewsService newsService=new com.hedong.Service.NewsService();//调用服务层判断数据
			System.out.println(User+Password);
			boolean isLogin=newsService.isUser(User,Password);
			
			
			if(isLogin) {
				//这个字符串将会在js中被接收
				//1代表登录成功
				//2代表用户名或密码为空
				//3代表用户名或密码错误
				request.getSession().setAttribute("user",User);
				out.write("1");
			
			}else if(User==""||Password==""){
				out.write("2");
			}else {
				out.write("3");
			}
				out.close();
			
		}
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  

posted @ 2019-04-27 17:18  DoNg_一隅  阅读(217)  评论(0编辑  收藏  举报