新闻发布系统
下面就让我们来说一说基于jsp的新闻发布系统,其中使用的技术有JavaBean、fillter、数据库等,能够实现新闻的发布功能,在发布之后能够进行对每一条新闻的删除、修改、或者继续增加新的文章,最后还能够进行查询功能,其中引用了百度编辑器,能够进行图文并茂的编辑,极大地方便用户的使用。
注 :完整项目
下载地址 : 新闻发布系统(下面的部分代码和下载地址的有所区别,但是只是优化而已,出问题可随时问我)
一、效果演示
首先让我们来看一看实现的效果:
下面是登陆的首界面:
图1 首界面
管理员登录页面:
图2 管理员登录界面
下面是管理员登陆之后的界面,可以进行添加新闻、删除新闻、查询新闻:
图3 管理员管理界面
下面是添加新闻界面,能够进行新闻的添加:
图4 添加新闻界面
下面是所添加的新闻:
图5 添加后的新闻
下面是修改新闻界面:
图6 修改新闻界面
下面是修改后的界面
图7 修改后的界面
下面是删除新闻后的界面:
图8 删除新闻后的界面
对于新闻的查找,与其他类似,在这里我们使用的是利用新闻编号进行查询所需的信息,然后在新的页面上进行显示
二、具体实现过程
1、环境:编程环境为NetBeans8.0和Oracle数据库
2、数据库后台的设计
在新闻管理系统中,我们需要数据库后台管理,其具体步骤如下所示:
根据所需的业务进行建表,
可参见如下所示 :
建立数据库
3、编写步骤:
(1)首先建立以个项目,在此命名为Demo_08_NewsRealese,项目布局结构如下图所示,根据如下布局对项目进行布局设计,方便后面的编程,项目中web-inf为项目配置文件夹,images文件夹用于储存项目中使用的图片文件,ueditor为百度富文本编辑器文件夹,源包文件夹为Java类文件夹:
图9 项目布局结构图
接着,我们进行创建数据库连接层,即DAO和VO,分别存于包com.lut.beans和dao,类命名分别命名为NewsRealese、NewsRealeseDao,对于类NewsRealese而言,主要是实现字符串的包装,其内容与数据库里边的内容一一对应,其代码分别如下所示:
package com.lut.beans;
public class NewsRealese {
private String newsId;
private String classId;
private String kindId;
private String myOther;
private String headTitle;
private String content;
private String connectRealtive;
private String author;
private String editor;
private String newsFrom;
private String top;
private String newsTime;
private String hits;
private String state;
private String tag;
public String getNewsId() {
return newsId;
}
public void setNewsId(String newsId) {
this.newsId = newsId;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getKindId() {
return kindId;
}
public void setKindId(String kindId) {
this.kindId = kindId;
}
public String getMyOther() {
return myOther;
}
public void setMyOther(String myOther) {
this.myOther = myOther;
}
public String getHeadTitle() {
return headTitle;
}
public void setHeadTitle(String headTitle) {
this.headTitle = headTitle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getConnectRealtive() {
return connectRealtive;
}
public void setConnectRealtive(String connectRealtive) {
this.connectRealtive = connectRealtive;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String getNewsFrom() {
return newsFrom;
}
public void setNewsFrom(String newsFrom) {
this.newsFrom = newsFrom;
}
public String getTop() {
return top;
}
public void setTop(String top) {
this.top = top;
}
public String getNewsTime() {
return newsTime;
}
public void setNewsTime(String newsTime) {
this.newsTime = newsTime;
}
public String getHits() {
return hits;
}
public void setHits(String hits) {
this.hits = hits;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
}
下面是类DAO类,命名为NewsRealeseDao.java,对于DAO类,主要用于实现数据库的连接问题,进行数据库的链接,在连接过程中需要用到各种有关数据库的技术,在这里我们使用的是Oracle数据库,其获取链接的方式为如下代码,其中第一行为注册驱动,第二行为数据库地址(orcl为Oracle数据库中的SID,在安装Oracle数据库时需要记住),第三行为具体的连接,及使用URL找到数据库位置,然后使用密码wjk139登录到Scott账户
//1、注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2、url地址
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//3、获取连接
conn = DriverManager.getConnection(url, "scott", "wjk139");
其他的操作分别为增删改查的具体代码,分别对应Servlet中或jsp中需要实例化的代码,下面就是具体的DAO类代码:
package dao;
import com.lut.beans.NewsRealese;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class NewsRealeseDao {
public ArrayList queryAllNews() throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
conn = DriverManager.getConnection(url, "scott", "wjk139");
//运行SQL语句
String sql = "select * from newmessage";//获取
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setClassId(rs.getString("CLASSID"));
news.setKindId(rs.getString("KINDID"));
news.setMyOther(rs.getString("MYOTHER"));
news.setHeadTitle(rs.getString("HEADTITLE"));
news.setContent(rs.getString("CONTENT"));
news.setConnectRealtive(rs.getString("CONNECTREALTIVE"));
news.setAuthor(rs.getString("AUTHOR"));
news.setEditor(rs.getString("EDITOR"));
news.setNewsFrom(rs.getString("NEWSFROM"));
news.setTop(rs.getString("TOP"));
news.setNewsTime(rs.getString("NEWSTIME"));
news.setHits(rs.getString("HITS"));
news.setState(rs.getString("STATE"));
news.setTag(rs.getString("TAG"));
newsRealese.add(news);
}
rs.close();
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//查询一个消息
public ArrayList queryOneNews(int newsid) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
int temp_id = newsid;
try {
//获取连接
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
conn = DriverManager.getConnection(url, "scott", "wjk139");
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "select * from newmessage where newsid=?";//获取newsid,使用?代替字符串,是一种预编译的提交方式
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.valueOf(newsid));
ResultSet rs = ps.executeQuery();// 之前已经给了sql字符串,所以executeQuery是无参的。
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setClassId(rs.getString("CLASSID"));
news.setKindId(rs.getString("KINDID"));
news.setMyOther(rs.getString("MYOTHER"));
news.setHeadTitle(rs.getString("HEADTITLE"));
news.setContent(rs.getString("CONTENT"));
news.setConnectRealtive(rs.getString("CONNECTREALTIVE"));
news.setAuthor(rs.getString("AUTHOR"));
news.setEditor(rs.getString("EDITOR"));
news.setNewsFrom(rs.getString("NEWSFROM"));
news.setTop(rs.getString("TOP"));
news.setNewsTime(rs.getString("NEWSTIME"));
news.setHits(rs.getString("HITS"));
news.setState(rs.getString("STATE"));
news.setTag(rs.getString("TAG"));
newsRealese.add(news);
}
rs.close();
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//删除数据
public String deleteOneNews(int newsid) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
int temp_id = newsid;
try {
//获取连接
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
conn = DriverManager.getConnection(url, "scott", "wjk139");
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "DELETE FROM newmessage WHERE newsid =?";//获取newsid,使用?代替字符串,是一种预编译的提交方式
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.valueOf(newsid));
ResultSet rs = ps.executeQuery();// 之前已经给了sql字符串,所以executeQuery是无参的。
rs.close();
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese.toString();
}
}
//插入数据
public String insertOneNews(ArrayList addnews_list) throws Exception {
Connection conn = null;
/* for(int i=0,j=1;i
(2)首页展示页面的编写,命名为index.html,由于可能有网络的延迟,我们使用渐进的方式跳转(渐进方式跳转:window.location.href="newRealese_brief.jsp",从index.html页面跳转到NewRealese_brief.jsp页面,其实这里的跳转功能使用的是JavaScript中的window对象进行跳转的),代码如下所示:
新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂
正在跳转...,请等待...
在缓冲时,会进行跳转,跳转后的页面为newRealese_brief.jsp,其代码如下所示:进入新闻展示页面之后,我们能够对新闻进行搜素,还能够查看详细内容。
<%@page contentType="text/html" import="java.util.*,java.sql.*,com.lut.beans.NewsRealese,dao.NewsRealeseDao"
pageEncoding="UTF-8" language="java"%>
新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂
管理员登录 最新资讯
<%@include file="a_queryNews.jsp" %>
<%
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.queryAllNews();
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese newRealese = (NewsRealese) newsRealese.get(i);
%>
此处假设无图片
此处假设无等级信息
原创或转载:
<%=newRealese.getMyOther()%>
新闻标题:
<%=newRealese.getHeadTitle()%>
新闻内容:
<%=newRealese.getContent()%>
作者:
<%=newRealese.getAuthor()%>
新闻发布时间:
<%=newRealese.getNewsTime()%>
新闻点击次数:
<%=newRealese.getHits()%>
<%
}
%>
(3)详细新闻页面的设计,命名为newRealese_detail.jsp其代码如下所示:再详细新闻页面上,我们能够进行数据的详细情况的了解,在观看完详细信息之后,我们能够进行点击返回按钮进行返回操作
<%@page contentType="text/html" import="java.util.*,java.sql.*" pageEncoding="UTF-8" language="java"%>
<%@page import="com.lut.beans.NewsRealese" %>
<%@page import="dao.NewsRealeseDao" %>
新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂
最新资讯
<%
String newsid=request.getParameter("newsid");
int newsid_int=Integer.parseInt(newsid);
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
ArrayList newsRealese=newsRealeseDao.queryOneNews(newsid_int);
%>
新闻编号
所属新闻栏目编号
所属新闻分类编号
原创或转载
新闻标题
新闻内容
相关文章
作者
编辑
出处
是否置顶
新闻发布时间
新闻点击次数
新闻状态
新闻标记
<%
for(int i=0;i
<%=newRealese.getNewsId() %>
<%=newRealese.getClassId() %>
<%=newRealese.getKindId() %>
<%=newRealese.getMyOther() %>
<%=newRealese.getHeadTitle()%>
<%=newRealese.getContent() %>
<%=newRealese.getConnectRealtive()%>
<%=newRealese.getAuthor() %>
<%=newRealese.getEditor() %>
<%=newRealese.getNewsFrom() %>
<%=newRealese.getTop() %>
<%=newRealese.getNewsTime() %>
<%=newRealese.getHits() %>
<%=newRealese.getState() %>
<%=newRealese.getTag() %>
新闻标题:
<%=newRealese.getHeadTitle()%>
作者:
<%=newRealese.getAuthor() %>
新闻发布时间:
<%=newRealese.getNewsTime() %>
新闻点击次数:
<%=newRealese.getHits() %>
<%=newRealese.getContent() %>
<%
}
%>
(4)管理员登录界面,命名为adminLogin.jsp页面,使用连接数据库的方式进行登录,在这里我们的管理员名称为Scott,密码为wjk139,管理员登录界面的实现代码如下所示
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂
在登录的过程中,我们需要进行判断,命名为checkLogin.java,,通过此类进行判断登录是否成功,若成功,则跳转到管理界面,若失败,则停留在当前界面,其代码如下所示:
package servlet;
import com.lut.beans.NewsRealese;
import dao.NewsRealeseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class checkLogin extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String user=request.getParameter("user");
String pass=request.getParameter("pass");
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
String newsRealese=newsRealeseDao.ischecked(user,pass);
if(newsRealese!=null)
{
response.sendRedirect("adminManager.jsp");
}
else
{
response.sendRedirect("adminLogin.jsp");
}
}
catch (Exception ex)
{
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description"+"public String getServletInfo() ";
}
}
(5)管理员登陆之后的界面,命名为adminManager.jsp页面,在管理员界面能够进行删除修改增加和查询操作,其代码如下所示:
<%--
Document : adminManager
Created on : 2016-5-18, 17:10:01
Author : Administrator
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.lut.beans.NewsRealese"%>
<%@page import="dao.NewsRealeseDao"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
管理员界面||adminManager.jsp
新闻管理
新闻信息显示
<% NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.queryAllNews();
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese newRealese = (NewsRealese) newsRealese.get(i);
%>
<%
}
%>
<%@include file="a_queryNews.jsp" %>
添加新闻
二
三
(6)增加新闻页面,命名为a_addNews.jsp,能够进行新闻的增加,在添加完信息之后,我们点击发布之后就可以成功添加信息,其代码如下所示,增加新闻时需要对新闻进行编辑,对于长篇幅新闻内容且含有图片的新闻内容的编写,在这儿我们使用功能强大的百度编辑器,首先从官网下载: 百度编辑器下载,下载之后解压(我使用的版本是ueditor1_4_3_2-utf8-jsp),如下图所示结构,我们也能够在这里查看百度编辑器的配置: 百度编辑器的配置
图10 百度富文本编辑器文件夹结构图
然后只需将整个文件夹拷贝到项目下即可,在使用时引入和修改路径,在jsp页面里边我已经配置好引用,只需修改路径即可,修改路径时只需将editor_config.js中查找URL变量配置编辑器在你项目中的路径。其中./ueditor为项目中的文件夹
var URL= window.UEDITOR_HOME_URL||"./ueditor/";
这样就配置好了编辑器,下面就是增加新闻的jsp页面,在添加完新闻之后,只需点击发布即可进行新闻的发布操作,代码如下所示:
<%@page contentType="text/html" import="java.util.*,java.sql.*" pageEncoding="UTF-8" language="java"%>
<%@page import="com.lut.beans.NewsRealese" %>
<%@page import="dao.NewsRealeseDao" %>
添加新闻- 新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿tang
在增加新闻时,需要对Servlet进行配置,如下图所示所示为配置文件的结构:
插入数据Servlet结构图
下面是InserOneNews.java代码,能够进行对数据的插入功能
package servlet;
import dao.NewsRealeseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InsertOneNews extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
ArrayList addnews_list = new ArrayList();
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
addnews_list.add(0, request.getParameter("newsid"));
addnews_list.add(1, request.getParameter("classid"));
addnews_list.add(2, request.getParameter("kindid"));
addnews_list.add(3, request.getParameter("myother"));
addnews_list.add(4, request.getParameter("headtitle"));
addnews_list.add(5, request.getParameter("content"));
addnews_list.add(6, request.getParameter("connectrealtive"));
addnews_list.add(7, request.getParameter("author"));
addnews_list.add(8, request.getParameter("editor"));
addnews_list.add(9, request.getParameter("newsfrom"));
addnews_list.add(10, request.getParameter("top"));
addnews_list.add(11, request.getParameter("newstime"));
addnews_list.add(12, request.getParameter("hits"));
addnews_list.add(13, request.getParameter("state"));
addnews_list.add(14,request.getParameter("tag"));
try {
String newsRealese = newsRealeseDao.insertOneNews(addnews_list);
System.out.println("String:"+newsRealese);
if (newsRealese!= null) {
response.sendRedirect("adminManager.jsp");
} else {
response.sendRedirect("a_addNews.jsp");
}
} catch (Exception ex) {
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
至此,我们完整的实现了插入工作
(7)现在完成删除操作,在Servlet包中建立如下所示代码,命名为DeleteOneNews.java:
package servlet;
import dao.NewsRealeseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteOneNews extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String str_id=request.getParameter("newsid");
int newsid_int=Integer.valueOf(str_id);
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
String newsRealese = newsRealeseDao.deleteOneNews(newsid_int);
System.out.println("String:"+newsRealese);
if (newsRealese!= null) {
response.sendRedirect("adminManager.jsp");
} else {
response.sendRedirect("a_addNews.jsp");
}
} catch (Exception ex) {
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}
}
(8)实现查询操作,命名为a_queryNews.jsp,其代码如下所示:
<%--
Document : a_queryNews
Created on : 2016-5-18, 17:08:40
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
查询页面
对于查询这件事,有成功,有失败,在这儿我们进行失败后文件的设置,代码如下所示,命名为a_queryNewsfailed.jsp,使用超级连接的方式,连接到主页面
<%--
Document : a_queryNeesfailed
Created on : 2016-6-6, 8:43:35
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
查询失败
没有找到你要查找的内容,但是你可以进入
主页进行查看
实现了以上显示层的操作之后,下面就是控制层的实现,命名为QueryOneNews.java,其代码如下所示:
package servlet;
import dao.NewsRealeseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryOneNews extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String str_id=request.getParameter("query");
int newsid_int=Integer.valueOf(str_id);
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
ArrayList newsRealese = newsRealeseDao.queryOneNews(newsid_int);
char [] s1=newsRealese.toString().toCharArray();
System.out.println("String+newsRealese:"+newsRealese);
System.out.println("String+newsRealese:"+s1[0]+" -------"+s1[1]);
if(s1[0]=='['&&s1[1]==']')
{
response.sendRedirect("a_queryNewsfailed.jsp");
}
else
{
response.sendRedirect("newRealese_detail.jsp?newsid="+str_id);
}
} catch (Exception ex) {
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
(9)下面实现更新数据的操作,命名为a_updateNews.jsp,其代码如下所示:
<%@page contentType="text/html" import="java.util.*,java.sql.*" pageEncoding="UTF-8" language="java"%>
<%@page import="com.lut.beans.NewsRealese" %>
<%@page import="dao.NewsRealeseDao" %>
更改新闻-
<%
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
String newsid_s=request.getParameter("newsid");
int news_int=Integer.valueOf(newsid_s);
ArrayList news_list=newsRealeseDao.queryOneNews(news_int);
%>
<%
for(int i=0;i
<%
}
%>
在Servlet中进行如下代码所示配置:
package servlet;
import dao.NewsRealeseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UpdateOneNews extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
ArrayList addnews_list = new ArrayList();
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
addnews_list.add(0, request.getParameter("newsid"));
addnews_list.add(1, request.getParameter("classid"));
addnews_list.add(2, request.getParameter("kindid"));
addnews_list.add(3, request.getParameter("myother"));
addnews_list.add(4, request.getParameter("headtitle"));
addnews_list.add(5, request.getParameter("content"));
addnews_list.add(6, request.getParameter("connectrealtive"));
addnews_list.add(7, request.getParameter("author"));
addnews_list.add(8, request.getParameter("editor"));
addnews_list.add(9, request.getParameter("newsfrom"));
addnews_list.add(10, request.getParameter("top"));
addnews_list.add(11, request.getParameter("newstime"));
addnews_list.add(12, request.getParameter("hits"));
addnews_list.add(13, request.getParameter("state"));
addnews_list.add(14,request.getParameter("tag"));
try {
String newsRealese = newsRealeseDao.updateOneNews(addnews_list);
System.out.println("String:"+newsRealese);
if (newsRealese!= null) {
response.sendRedirect("adminManager.jsp");
} else {
response.sendRedirect("a_addNews.jsp");
}
} catch (Exception ex) {
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
到这儿,我们已经实现了对增删改查功能的实现,当然,基本的新闻发布功能已经实现