新闻发布系统项目

时间:2021-03-10 08:57:11

增删改查

下面来看看我们真实的需要做的

新闻发布系统项目

 

登录后跳转的页面

新闻发布系统项目

 

添加新闻===文件上传

新闻发布系统项目

 

编辑新闻----修改  删除

新闻发布系统项目

 

添加主题===分类

新闻发布系统项目

 

编辑主题

新闻发布系统项目

web项目里后台的分层

新闻发布系统项目

一:先说登录主要代码从我的数据库里拿数据,servlet调用,然后被前台调用。代码如下:

@Override
public boolean isLogin(UserInfo info) throws Exception {
boolean flag
=false;
String sql
="select count(*) from login where username=? and userpwd=?";
ResultSet rs
=executeQuery(sql, info.getUserName(),info.getUserPwd());
if (rs.next()) {
int count=rs.getInt(1);
if(count>0){
flag
=true;
}
}
return flag;
}

我的servlet调用===判断加效验===加我的注销===移除session记录的账户

request.setCharacterEncoding("utf-8");
if (request.getParameter("uname")!=null) {
String uname
=request.getParameter("uname");
String upwd
=request.getParameter("upwd");

//拼接成一个用户对象
UserInfo info=new UserInfo();
info.setUserName(uname);
info.setUserPwd(upwd);

IUserInfoService service
=new UserInfoService();
try {
boolean flag
= service.isLogin(info);
if (flag) { //登录成功
//3.记录session
request.getSession().setAttribute("uname", uname);
request.setAttribute(
"loginsuccess","true");
//4.转发或者重定向 转发不需要加项目名称
request.getRequestDispatcher("/NewsServlet").forward(request, response);
return;
}
}
catch (Exception e) {
e.printStackTrace();
}

}
if ("".equals(request.getParameter("uname")) || "".equals(request.getParameter("upwd"))) {
request.setAttribute(
"longinfailure","true");
request.getRequestDispatcher(
"/NewsServlet").forward(request, response);
return;
}

request.setCharacterEncoding(
"utf-8");

if(request.getSession().getAttribute("uname")!=null){
request.setCharacterEncoding(
"utf-8");

request.getSession().removeAttribute(
"uname");
//重定向
response.sendRedirect("/News/NewsServlet");
return;
}

我的页面调用:

<script type="text/javascript">

<%if("true".equals(request.getAttribute("longinfailure"))){%>
alert(
"用户名或密码为空!");
<%}%>

</script>

</head>

<body>
<div id="header">
<div id="top_login">
<form action="<%=path%>/UserInfoNewServlet" method="post">
<label> 登录名 </label> <input type="text" id="uname" name="uname"
value
="" class="login_input" /> <label> 密&#160;&#160;码 </label> <input
type
="password" id="upwd" name="upwd" value="" class="login_input" />
<input type="submit" class="login_sub" value="登录" />

二:下面说说我的分页
定义了一个util实体包供我分页使用

package cn.news.util;

import java.util.List;

import cn.news.entity.NewsInfo;

public class Page {
// 当前页
private int pageIndex;
// 页面总记录数
private int pageSize;
// 本业显示真实数据
private List<NewsInfo> list;
// 总页数
private int totalPages;
// 总记录数
private int totalRecords;

public int getPageIndex() {
return pageIndex;
}

public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public List<NewsInfo> getList() {
return list;
}

public void setList(List<NewsInfo> list) {
this.list = list;
}

public int getTotalPages() {
return totalPages;
}

public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}

public int getTotalRecords() {
return totalRecords;
}

public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}

}

主要从数据库里拿到的分页代码如下:

@Override
public List<NewsInfo> getPageInfos(int pageIndex, int pageSize) throws Exception {
List
<NewsInfo> list=new ArrayList<NewsInfo>();

String sql
="SELECT * FROM newinfo LIMIT ?,?";
rs
=executeQuery(sql,(pageIndex-1)*pageSize,pageSize);
if (rs!=null) {
while (rs.next()) {
NewsInfo info
=new NewsInfo();
int nid=rs.getInt("nid");
String ntitle
=rs.getString("ntitle");
String nauthor
=rs.getString("nauthor");
Date npublisherdate
=rs.getDate("npublisherdate");
String ncontent
=rs.getString("ncontent");
int tid=rs.getInt("tid");

info.setNauthor(nauthor);
info.setNcontent(ncontent);
info.setNid(nid);
info.setNpublisherdate(npublisherdate);
info.setNtitle(ntitle);
info.setTid(tid);

list.add(info);
}
}
return list;
}

 

@Override
public int selectnewsall() throws Exception {
String sql
="SELECT count(1) FROM newinfo";
int count = 0;
ResultSet rs
= executeQuery(sql);
if (rs.next()) {
count
= rs.getInt(1);
}

return count;
}

servlet调用:

    public void pagenews(HttpServletRequest request,HttpServletResponse response) {
IUserInfoService daoInfoService
= new UserInfoService();
try {
Page oaPage
= new Page();
// 默认三条数据
int pageSize = 3;
oaPage.setPageSize(pageSize);
// pageIndex(当前页)
int myindex = 1;

String pageIndex
= request.getParameter("pageIndex");
if (null!=pageIndex && (!pageIndex.equals(""))) {
myindex
= Integer.parseInt(pageIndex);
}

// 总页数赋值=总记录数/pageSize
int mytotalPages = 0;
int ipages = daoInfoService.selectnewsall();
if (ipages % pageSize == 0) {
mytotalPages
= ipages / pageSize;
}
else {
mytotalPages
= ipages / pageSize + 1;
}
oaPage.setTotalPages(mytotalPages);
if(myindex>oaPage.getTotalPages()){
myindex
=oaPage.getTotalPages();
System.
out.println(myindex+"1");
}
System.
out.println("=================================");
if (myindex<1) {
myindex
=1;
System.
out.println(myindex+"2");
}
oaPage.setPageIndex(myindex);
// 泛型数据赋值
List<NewsInfo> list = daoInfoService.getPageInfos(oaPage.getPageIndex(), pageSize);
oaPage.setList(list);

// 将泛型集合,放入request作用域
request.setAttribute("list", oaPage);
// 转发index.jsp 转发不用加名称


}
catch (Exception e) {

e.printStackTrace();
}
}
public void pageNews(HttpServletRequest request,HttpServletResponse response) {
IUserInfoService daoInfoService
= new UserInfoService();
try {
Page oaPage
= new Page();
// 默认三条数据
int pageSize = 3;
oaPage.setPageSize(pageSize);
// pageIndex(当前页)
int myindex = 1;

String pageIndex
= request.getParameter("pageIndex");
if (null!=pageIndex && (!pageIndex.equals(""))) {
myindex
= Integer.parseInt(pageIndex);
}

// 给当前页赋值

// 总页数赋值=总记录数/pageSize
int mytotalPages = 0;
int ipages = daoInfoService.selectnewsall();
if (ipages % pageSize == 0) {
mytotalPages
= ipages / pageSize;
}
else {
mytotalPages
= ipages / pageSize + 1;
}
oaPage.setTotalPages(mytotalPages);
if(myindex>oaPage.getTotalPages()){
myindex
=oaPage.getTotalPages();
System.
out.println(myindex+"1");
}

if (myindex<1) {
myindex
=1;
System.
out.println(myindex+"2");
}
oaPage.setPageIndex(myindex);
// 泛型数据赋值
List<NewsInfo> list = daoInfoService.getPageInfos(oaPage.getPageIndex(), pageSize);
oaPage.setList(list);

// 将泛型集合,放入request作用域
request.setAttribute("list", oaPage);
// 转发index.jsp 转发不用加名称

}
catch (Exception e) {
e.printStackTrace();
}
}

dopost

if(request.getParameter("pageIndex")!=null){
pageNews(request, response);
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request,response);
return;
}

pagenews(request, response);
request.getRequestDispatcher(
"/index.jsp").forward(request,response);

页面调用servlet显示给顾客

<p align="right">
当前页数:[
<%=page2.getPageIndex()%>/<%=page2.getTotalPages()%>]&nbsp;
<a
href
="<%=path%>/NewsServlet?pageIndex=<%=page2.getPageIndex() - 1%>">上一页</a>
<a
href
="<%=path%>/NewsServlet?pageIndex=<%=page2.getPageIndex() + 1%>">下一页</a>
<a
href
="<%=path%>/NewsServlet?pageIndex=<%=page2.getTotalPages()%>">末页</a>
</p>


登陆后显示的页数

<p align="right"> 当前页数:[${list.pageIndex}/${list.totalPages}]&nbsp;
<a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.pageIndex-1}">上一页</a>&nbsp;
<a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.pageIndex+1}">下一页</a>&nbsp;
<a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.totalPages}">末页</a> </p>

三:页面显示数据库里的数据====查找
主要查找Dao层的代码:

@Override
public List<NewsInfo> findAll() throws Exception {
List
<NewsInfo> list=new ArrayList<NewsInfo>();

String sql
="select * from newinfo";
rs
=executeQuery(sql);
if (rs!=null) {
while (rs.next()) {
NewsInfo info
=new NewsInfo();
int nid=rs.getInt("nid");
String ntitle
=rs.getString("ntitle");
String nauthor
=rs.getString("nauthor");
Date npublisherdate
=rs.getDate("npublisherdate");
String ncontent
=rs.getString("ncontent");
int tid=rs.getInt("tid");

info.setNauthor(nauthor);
info.setNcontent(ncontent);
info.setNid(nid);
info.setNpublisherdate(npublisherdate);
info.setNtitle(ntitle);
info.setTid(tid);

list.add(info);
}
}
return list;
}

将数据用循环显示到页面在我的主页面写:

<ul class="classlist">
<%
Page page2
= (Page) request.getAttribute("list");
for (NewsInfo item : page2.getList()) {
%>
<li><a href="<%=path%>/news_read.jsp"><%=item.getNtitle()%>
</a><span><%=item.getNpublisherdate()%></span></li>
<%
}
%>

四:删除数据主要数据:

@Override
public boolean deleteNews(int id) throws Exception {
String sql
="delete from newinfo where nid=?";
boolean flag
=false;
int count = exeuteUpdate(sql,id);
if (count>0) {
flag
=true;
}
return flag;
}

el表达式:

<c:if test="${desuccess!=null}">
<script type="text/javascript">
alert(
"删除成功!!!");
</script>
</c:if>
</head>
<body>
<div id="main">
<div>
<iframe src="<%=path %>/newspages/console_element/top.jsp" scrolling="no" frameborder="0" width="947px" height="180px"></iframe>
</div>
<div id="opt_list">
<iframe src="<%=path%>/newspages/console_element/left.jsp" scrolling="no" frameborder="0" width="130px"></iframe>
</div>
<div id="opt_area">
<ul class="classlist">
<c:forEach var="item" items="${list.list}">
<li> ${item.ntitle}<span> 作者:${item.nauthor}
&#160;&#160;&#160;&#160; <a href='<%=path%>/NewsServlet?upid=${item.nid}'>修改</a> &#160;&#160;&#160;&#160;
<a href='${pageContext.request.contextPath}/NewsServlet?deid=${item.nid}'>删除</a>
</span>
</li>
</c:forEach>

  <li><a href="${pageContext.request.contextPath}/NewsServlet?xin=xin" target="_parent">添加新闻</a></li>
    <li><a href="${pageContext.request.contextPath}/NewsServlet?kin=kin" target="_parent">编辑新闻</a></li>
 所有的dopost数据:

    if (null!=request.getParameter("upid")) {
System.
out.println("1");
return;
}
if (null!=request.getParameter("deid")) {
System.
out.println("2");
IUserInfoService service
=new UserInfoService();
String deid
=request.getParameter("deid");
System.
out.println(deid);
Integer id
=Integer.parseInt(deid);
boolean flag
=false;
try {
flag
=service.deleteNews(id);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute(
"desuccess", flag);
pageNews(request, response);
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request, response);
return;
}
if("true".equals(request.getAttribute("loginsuccess"))){
System.
out.println("3");
pageNews(request, response);
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request, response);
return;
}
if (request.getParameter("kin")!=null) {//index
pageNews(request, response);
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request,response);
return;
}
if(request.getParameter("pageIndex")!=null){
pageNews(request, response);
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request,response);
return;
}
if (request.getParameter("xin")!=null) {

try {
addnews(request, response);
request.getRequestDispatcher(
"/newspages/news_add.jsp").forward(request, response);
IUserInfoService infoService
=new UserInfoService();
List
<NewsInfo> all = infoService.findAll();
if (all!=null) {
request.getRequestDispatcher(
"/newspages/admin.jsp").forward(request, response);
}
else {
request.getRequestDispatcher(
"/newspages/news_add.jsp").forward(request, response);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
pagenews(request, response);
request.getRequestDispatcher(
"/index.jsp").forward(request,response);

今天小编就到这里了 

剩余修改下期详解

 

希望给大家的知识