基于jsp的新闻发布系统
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于jsp的新闻发布系统
新闻发布系统
下面就让我们来说一说基于jsp的新闻发布系统,其中使用的技术有JavaBean、fillter、数据库等,能够实现新闻的发布功能,在发布之后能够进行对每一条新闻的删除、修改、或者继续增加新的文章,最后还能够进行查询功能,其中引用了百度编辑器,能够进行图文并茂的编辑,极大地方便用户的使用。
注:完整项目下载地址:新闻发布系统
一、效果演示
首先让我们来看一看实现的效果:
下面是登陆的首界面:
图1 首界面
管理员登录页面:
下面是管理员登陆之后的界面,可以进行添加新闻、删除新闻、查询新闻:
下面是添加新闻界面,能够进行新闻的添加:
下面是所添加的新闻:
下面是修改新闻界面:
下面是修改后的界面
下面是删除新闻后的界面:
图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而言,主要是实现字符串的包装,其内容与数据库里边的内容一一对应,其代码分别如下所示:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
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账户
[java] view plain copy 在CODE上查看代码片派生到我的代码片
//获取连接
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
conn = DriverManager.getConnection(url, "scott", "wjk139");
,其他的操作分别为增删改查的具体代码,分别对应Servlet中或jsp中需要实例化的代码,下面就是具体的DAO类代码:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
package dao;
import com.lut.beans.NewsRealese;
import static ng.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.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字符串,所以executeUpdate 是无参的。
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
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字符串,所以executeUpdate 是无参的。
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<addnews_list.size();i++,j++)
{
System.out.println("j:"+j+"值:"+addnews_list.get(i).toString());
}*/
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 = "insert into newmessage(newsId,classId,kindId,myOther,headTitle,content,connectRealtive,author,editor,news From"
+ ",top,newsTime,hits,state,tag) V ALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.valueOf(addnews_list.get(0).toString()));
ps.setInt(2, Integer.valueOf(addnews_list.get(1).toString()));
ps.setInt(3, Integer.valueOf(addnews_list.get(2).toString()));
ps.setInt(4, Integer.valueOf(addnews_list.get(3).toString()));
ps.setString(5, addnews_list.get(4).toString());
ps.setString(6, addnews_list.get(5).toString());
ps.setString(7, addnews_list.get(6).toString());
ps.setString(8, addnews_list.get(7).toString());
ps.setString(9, addnews_list.get(8).toString());
ps.setString(10, addnews_list.get(9).toString());
ps.setInt(11, Integer.valueOf(addnews_list.get(10).toString()));
ps.setString(12, addnews_list.get(11).toString());
ps.setString(13, addnews_list.get(12).toString());
ps.setString(14, addnews_list.get(13).toString());
ps.setString(15, addnews_list.get(14).toString());
for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) {
System.out.println("j:" + j + "值:" + addnews_list.get(i).toString());
}
int i = ps.executeUpdate();
mit();
System.out.println("成功添加" + i + "行");
stat.close();
conn.close();
return i + "conn:" + conn;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return conn.toString();
}
//更新数据
public String updateOneNews(ArrayList addnews_list) throws Exception { Connection conn = null;
/* for(int i=0,j=1;i<addnews_list.size();i++,j++)
{
System.out.println("j:"+j+"值:"+addnews_list.get(i).toString());
}*/
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();
//获取newsid,使用?代替字符串,以免会发生错误
String sql = "UPDATE newmessage set classId=?,kindId=?,myOther=?,headTitle=?,content=?,"
+
"connectRealtive=?,author=?,editor=?,newsFrom=?,top=?,newsTime=?,hits=?,state=?,tag=? where newsid=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.valueOf(addnews_list.get(1).toString()));
ps.setInt(2, Integer.valueOf(addnews_list.get(2).toString()));
ps.setInt(3, Integer.valueOf(addnews_list.get(3).toString()));
ps.setString(4, addnews_list.get(4).toString());
ps.setString(5, addnews_list.get(5).toString());
ps.setString(6, addnews_list.get(6).toString());
ps.setString(7, addnews_list.get(7).toString());
ps.setString(8, addnews_list.get(8).toString());
ps.setString(9, addnews_list.get(9).toString());
ps.setInt(10, Integer.valueOf(addnews_list.get(10).toString()));
ps.setString(11, addnews_list.get(11).toString());
ps.setString(12, addnews_list.get(12).toString());
ps.setString(13, addnews_list.get(13).toString());
ps.setString(14, addnews_list.get(14).toString());
ps.setInt(15, Integer.valueOf(addnews_list.get(0).toString()));
for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) {
System.out.println("j:" + j + "值:" + addnews_list.get(i).toString());
}
int i = ps.executeUpdate();
mit();
System.out.println("成功更新" + i + "行");
stat.close();
conn.close();
return i + "conn:" + conn;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return conn.toString();
}
public String ischecked(String user, String pass) throws Exception {
Connection conn = null;
//获取连接
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
try {
conn = DriverManager.getConnection(url,user, pass);//不安全
//建立连接
return conn.toString();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
try {
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return null;
}
}
(2)首页展示页面的编写,命名为index.html,由于可能有网络的延迟,我们使用渐进的方式跳转(渐进方式跳转:window.location.href="newRealese_brief.jsp",从index.html页面跳转到ewRealese_brief.jsp页面),代码如下所示:
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href="newsRealese.css"/>
<title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂</title>
</head>
<body id="body">
正在跳转...,请等待...
<script language="javascript" type="text/javascript">
window.location.href="newRealese_brief.jsp";
</script>
</body>
</html>
在缓冲时,会进行跳转,跳转后的页面为newRealese_brief.jsp,其代码如下所示:进入新闻展示页面之后,我们能够对新闻进行搜素,还能够查看详细内容。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<%@page contentType="text/html" import="java.util.*,java.sql.*,com.lut.beans.NewsRealese,dao.NewsRealeseDao"
pageEncoding="UTF-8" language="java"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂</title>
<link type="text/css" rel="stylesheet" href="newsRealese.css"/>
</head>
<body id="body">
<a href="adminLogin.jsp" style="table-layout: right">管理员登录</a><h1 id="p">最新资讯</h1><br>
<%@include file="a_queryNews.jsp" %>
<table border="0" style="width:100%; height:100%" align="center">
<tr>
<td style="width: 10%"> </td>
<td>
<%
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.queryAllNews();
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese newRealese = (NewsRealese) newsRealese.get(i);
%>
<center><table border="0" width="100%" id="table">
<tr>
<td width="128"><p>此处假设无图片</p>
<p>此处假设无等级信息</p>
</td>
<td ><table border="0" width="100%">
<tr>
<td width="11%" >原创或转载:</td>
<td colspan="3"><%=newRealese.getMyOther()%> </td>
<td width="10%" >新闻标题:</td>
<td
width="44%" ><%=newRealese.getHeadTitle()%> </td>
</tr>
<tr>
<td colspan="6">新闻内容:</td>
</table>
<table width="100%" border="0">
<tr>
<td>
<a target="_blank" href="newRealese_detail.jsp?newsid=<%=newRealese.getNewsId()%>"/>
<table width="100%" border="0" align="center">
<tr>
<td>
<%=newRealese.getContent()%>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" width="100%">
<tr>
<td width="51" height="30">作者:</td>
<td width="203"><%=newRealese.getAuthor()%> </td>
<td width="97">新闻发布时间:</td>
<td
width="167"><%=newRealese.getNewsTime()%> </td>
<td width="99">新闻点击次数:</td>
<td width="191"><%=newRealese.getHits()%> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="21" colspan="2"><hr></td>
</tr>
</table></center>
<%
}
%>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
(3)详细新闻页面的设计,命名为newRealese_detail.jsp其代码如下所示:再详细新闻页面上,我们能够进行数据的详细情况的了解,在观看完详细信息之后,我们能够进行点击返回按钮进行返回操作
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<%@page contentType="text/html" import="java.util.*,java.sql.*" pageEncoding="UTF-8" language="java"%>
<%@page import="com.lut.beans.NewsRealese" %>
<%@page import="dao.NewsRealeseDao" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="newsRealese.css"/>
<title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂</title>
</head>
<body id="body">
<h1>最新资讯</h1><br>
<%
String newsid=request.getParameter("newsid");
int newsid_int=Integer.parseInt(newsid);
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
ArrayList newsRealese=newsRealeseDao.queryOneNews(newsid_int);
%>
<table border="1">
<tr >
<td >新闻编号</td>
<td>所属新闻栏目编号</td>
<td>所属新闻分类编号</td>
<td>原创或转载</td>
<td>新闻标题</td>
<td>新闻内容</td>
<td>相关文章</td>
<td>作者</td>
<td>编辑</td>
<td>出处</td>
<td>是否置顶</td>
<td>新闻发布时间</td>
<td>新闻点击次数</td>
<td>新闻状态</td>
<td>新闻标记</td>
</tr>
<%
for(int i=0;i<newsRealese.size();i++)
{
NewsRealese newRealese=(NewsRealese)newsRealese.get(i);
%>
<tr>
<td><%=newRealese.getNewsId() %> </td>
<td><%=newRealese.getClassId() %> </td>
<td><%=newRealese.getKindId() %> </td>
<td><%=newRealese.getMyOther() %> </td>
<td><%=newRealese.getHeadTitle()%> </td>
<td><%=newRealese.getContent() %> </td>
<td><%=newRealese.getConnectRealtive()%></td>
<td><%=newRealese.getAuthor() %> </td>
<td><%=newRealese.getEditor() %> </td>
<td><%=newRealese.getNewsFrom() %> </td>
<td><%=newRealese.getTop() %> </td>
<td><%=newRealese.getNewsTime() %> </td>
<td><%=newRealese.getHits() %> </td>
<td><%=newRealese.getState() %> </td>
<td><%=newRealese.getTag() %> </td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td width="100%" style="text-align: center; vertical-align: top">
<table width="90%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="4" >
<tr>
<th width="26%" class="TitleColor" style="text-align: right; vertical-align: top" scope="row">新闻标题: </th>
<td width="74%" style="vertical-align: top" class="TitleColor" ><p align="left"><%=newRealese.getHeadTitle()%><p></td>
</tr>
<tr>
<th scope="row" class="TitleColor" style="text-align: right; vertical-align: top">作者:</th>
<td style="vertical-align: top"><p align="left"><%=newRealese.getAuthor() %> <p> </td>
</tr>
<tr>
<th scope="row" class="TitleColor" style="text-align: right; vertical-align: top">新闻发布时间: </th>
<td style="vertical-align: top" class="TitleColor"><p align="left"><%=newRealese.getNewsTime() %><p></td>
</tr>
<tr>
<th scope="row" class="TitleColor" style="text-align: right; vertical-align: top">新闻点击次数: </th>
<td style="vertical-align: top" class="TitleColor"><p align="left"><%=newRealese.getHits() %><p></td>
</tr>
<tr>
<td colspan="2" class="StoryContentColor"><p align="center"><%=newRealese.getContent() %><p></td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
<table>
<tr>
<td >
<a href="newRealese_brief.jsp">返回</a>
</td>
<td>
<a href="#">发表评论</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr style="text-align: right">
<td> <a href="#">点赞</a> <a href="#">举报</a> <a href="#">投稿,联系作者</a> </td>
</tr>
</table>
<%
}
%>
</body>
</html>
(4)管理员登录界面,命名为adminLogin.jsp页面,使用连接数据库的方式进行登录,在这里我们的管理员名称为Scott,密码为wjk139,管理员登录界面的实现代码如下所示[html] view plain copy 在CODE上查看代码片派生到我的代码片
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂</title> <link type="text/css" rel="stylesheet" href="newsRealese.css"/>
</head>
<body id="body">
<form method="post" action="checkLogin">
<table border="0" align="center" id="table_vertical" >
<caption>管理员登录</caption>
<tr>
<td width="40">Admin</td>
<td width="183">
<input type="text" name="user"></td>
</tr>
<tr>
<td height="25">Pass</td>
<td><input type="password" name="pass"></td>
</tr>
</table>
<table align="center">
<tr>
<td>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
在登录的过程中,我们需要进行判断,命名为checkLogin.java,,通过此类进行判断登录是否成功,若成功,则跳转到管理界面,若失败,则停留在当前界面,其代码如下所示:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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 eption, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description"+"public String getServletInfo() ";
}// </editor-fold>
}
(5)管理员登陆之后的界面,命名为adminManager.jsp页面,在管理员界面能够进行删除修改增加和查询操作,其代码如下所示:
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<%--
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"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理员界面||adminManager.jsp</title>
<link type="text/css" rel="stylesheet" href="newsRealese.css"/>
</head>
<body id="body" style=" text-orientation: vertical-right">
<h1 id="p">新闻管理</h1><br>
<table border="1" style="width:100%; height:100%">
<tr>
<td width="10%" style="text-align:top">
<table border="0" style="width: 100%;height: 100%" >
<tr>
<td><div align="center">新闻信息显示</div></td>
</tr>
<tr>
<td><img src="./images/dog.jpg" width="100%" height="90%"></td>
</tr>
<tr>
<td><img src="./images/minus.gif" ></td>
</tr>
</table></td>
<td width="50%">
<table border="0">
<tr>
<td>
<% NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.queryAllNews();
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese newRealese = (NewsRealese) newsRealese.get(i);
%>
<table border="0" width="100%" id="table">
<tr>
<td width="128"><p>此处假设无图片</p>
<p>此处假设无等级信息</p> </td>
<td ><table border="0" width="100%">
<tr>。