mvc2项目实践

2021-02-02 05:14

阅读:467

标签:mvc   char   name   org   exception   query   BMI   prefix   bsp   

删除功能:

创建DeleteNewsServlet

@WebServlet("/deleteServlet")
public class DeleteNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public DeleteNewsServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int newsID=Integer.valueOf(request.getParameter("newid"));
		System.out.println("newsID:"+newsID);
		NewService service=new NewService();
		service.DeleteNews(newsID);
		request.getRequestDispatcher("ShowListServlet").forward(request,response);
	}

}

  

在NewServlet中写入DeleteNews方法

	public void DeleteNews(int newsID) {
		String sql="delete from News where NewsID=‘"+newsID+"‘";
		SqlHelper.executeQuery(sql);
	};

  

完成删除功能

编辑功能:

在NewServlet写入GetNews方法

public News GetNews(int newsID) throws SQLException {
        String sql="Select * from News where NewsID=‘"+newsID+"‘";
        ResultSet rs;
        rs=SqlHelper.executeQuery(sql);
        News news = new News();
        if (rs.next()) {
            news.setNewid(rs.getInt("NewsID"));
            news.setTitle(rs.getString("Title"));
            news.setNewcontent(rs.getString("NewsContent"));
            news.setAuthor(rs.getString("Author"));
            news.setWritedate(rs.getDate("NewsData"));
        }
        return news;
    };

创建editServlet类

@WebServlet("/editServlet")
public class editServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public editServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int newsID=Integer.valueOf(request.getParameter("newid"));
        System.out.println("newsID:"+newsID);
        NewService service=new NewService();
        try {
            News news=service.GetNews(newsID);
            request.setAttribute("news", news);
            request.getRequestDispatcher("EditNews.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }

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

}

EditNews.jsp文件

@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
title>Insert title heretitle>
head>
body>
form action="SaveNewServlet"  method="post">
    新闻IDinput type="text" name="newid" value="${news.newid}"/>
    新闻标题input type="text" name="title" value="${news.title}"/>
     新闻内容input type="text" name="newcontent" value="${news.newcontent}"/>
     作者input type="text" name="author" value="${news.author}"/>
    时间input type="text" name="writedate" value="${news.writedate}"/>
    input type="submit" value="更新"/> 
   form>
body>
html>

jsp效果

技术图片

 

在NewServlet写入UpdateNews方法

public void UpdateNews(News news) {
        String sqltitle="update News set Title=‘"+news.getTitle()+"‘ where NewsID=‘"+news.getNewid()+"‘";
        String sqlauthor="update News set Author=‘"+news.getAuthor()+"‘ where NewsID=‘"+news.getNewid()+"‘";
        String sqlcontent="update News set NewsContent=‘"+news.getNewcontent()+"‘ where NewsID=‘"+news.getNewid()+"‘";
        SqlHelper.executeUpdate(sqltitle);
        SqlHelper.executeUpdate(sqlauthor);
        SqlHelper.executeUpdate(sqlcontent);
    };

创建SaveNewServlet类

@WebServlet("/SaveNewServlet")
public class SaveNewServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public SaveNewServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.setCharacterEncoding("utf-8");
        //response.setContentType("text/html;charset=utf-8");
        News news=new News();
        news.setNewid(Integer.valueOf(request.getParameter("newid")));
        news.setAuthor(request.getParameter("author"));
        news.setTitle(request.getParameter("title"));
        news.setNewcontent(request.getParameter("newcontent"));
        String strdate=request.getParameter("writedate");
        SimpleDateFormat sDateFormat=new SimpleDateFormat();
        Date date = null;
        try {
            date = sDateFormat.parse(strdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        news.setWritedate(date);
        System.out.println(news.getNewcontent());
        NewService service=new NewService();
        service.UpdateNews(news);
        request.getRequestDispatcher("ShowListServlet").forward(request,response);
    }

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

}

修改过程发现输入中文会产生乱码,故添加过滤器,添加过滤器过程不再叙述

修改功能完成

 添加功能:

在NewServlet写入GetNews方法

public void AddNews(News news) {
  String sql="insert into News (Title,NewsContent,Author)"
  + "values(‘"+news.getTitle()+"‘,‘"+news.getNewcontent()+"‘,‘"+news.getAuthor()+"‘);";
  SqlHelper.executeUpdate(sql);
};

改一下ShowList.jsp,把添加新闻的那个超链接改为+添加新闻

创建AddNewsServlet

@WebServlet("/AddNewsServlet")
public class AddNewsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public AddNewsServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        News news=new News();
        news.setAuthor(request.getParameter("author"));
        news.setTitle(request.getParameter("title"));
        news.setNewcontent(request.getParameter("newcontent"));
        NewService service=new NewService();
        service.AddNews(news);
        request.getRequestDispatcher("ShowListServlet").forward(request,response);
    }

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

}

创建AddNews.jsp

@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
title>Insert title heretitle>
head>
body>
form action="AddNewsServlet"  method="post">
    新闻标题input type="text" name="title" />
     新闻内容input type="text" name="newcontent" />
     作者input type="text" name="author"/>
    input type="submit" value="添加"/> 
   form>
body>
html>

完成添加功能

查看功能:

创建viewServlet

@WebServlet("/viewServlet")
public class viewServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public viewServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int newsID=Integer.valueOf(request.getParameter("newid"));
        NewService service=new NewService();
        try {
            News news=service.GetNews(newsID);
            request.setAttribute("news", news);
            request.getRequestDispatcher("view.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

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

}

随便写个view.jsp,此处不再去找新模板了

@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
title>Insert title heretitle>
head>
body>
    新闻ID:${news.newid}br/>
    新闻标题${news.title}br/>
     新闻内容${news.newcontent}"br/>
     作者${news.author}br/>
    时间${news.writedate}br/>
body>
html>

 

 查看功能完成

码云地址:https://gitee.com/teikigen/mvc_project

mvc2项目实践

标签:mvc   char   name   org   exception   query   BMI   prefix   bsp   

原文地址:https://www.cnblogs.com/teikigen/p/13170380.html


评论


亲,登录后才可以留言!