本小系统采用MyEclipse4.0GA+MySQL5.0+JDK5.0+Tomcat4.0+PowerDesigner12.0开发,为上一篇的续,第一次发,恳请各位大虾指正不足。
1、先改写补充list.jsp增加相应3个按钮,并用JavaScript对相应按钮进行“设置”,code as follows:
<%@ page contentType="text/html;charset=GBK" import="java.util.*,org.riker.news.data.*"%>
<html>
<head>
<title></title>
<jsp:useBean id = "news" scope = "page" class = "org.riker.news.logic.NewsLogicBean"/>
</head>
<body>
<%
String username = (String)session.getAttribute("user");
if(username != null) {
}
%>
欢迎您进入本系统:<%=username%>
<hr>
<form action="insert.jsp" name="form1" method="post">
<center>
<table border="1">
<tr><td>序号</td><td>新闻标题</td></tr>
<%
String str_curPage = request.getParameter("curPage");
if(str_curPage!=null){
news.setCurPage(Integer.parseInt(str_curPage));
}
List list = news.findAll();
int allRows = news.getAllRows();
int curPage = news.getCurPage();
int rowPerPage = news.getRowPerPage();
int allPage = news.getAllPages();
for(int i=0; i<list.size(); i++) {
NewsDataBean bean = (NewsDataBean)list.get(i);
%>
<tr><td><input type="radio" name="newsid" value="<%=bean.getNewsid()%>"><%=(curPage-1)*rowPerPage+i+1%></td>
<td><a href="detail.jsp?newid=<%=bean.getNewsid()%>"><%=bean.getNewstitle()%>(<%=bean.getNewsdate()%>)</td></tr>
<%
}
%>
</table>
<%
if(curPage!=1) {
%>
<a href="/news/main/list.jsp?curPage=1">首页</a>
<a href="/news/main/list.jsp?curPage=<%=curPage-1%>">上一页</a>
<% }
%>
<%
if(curPage!=allPage){
%>
<a href="/news/main/list.jsp?curPage=<%=curPage+1%>">下一页</a>
<a href="/news/main/list.jsp?curPage=<%=allPage%>">末页</a>
<%
}
%>
</center>
<hr>
<input type="submit" name="operate" value="增加新闻" onclick="return insertNews();"/>
<input type="submit" name="operate" value="修改新闻" onclick="return updateNews();"/>
<input type="submit" name="operate" value="删除新闻" onclick="return deleteNews();"/>
</form>
<script type="text/javascript">
function insertNews() {
document.form1.action="main/insert.jsp";
}
function updateNews() {
var selected = false;
for(var i=0; i<document.form1.newsid.length; i++) {
if(document.form1.newsid[i].checked) {
selected = true;
break;
}
}
if(!selected) {
alert("请选择所要修改的新闻!!");
return false;
}
document.form1.action="FindByIdServlet";
}
function deleteNews() {
var selected = false;
for(var i=0; i<document.form1.newsid.length; i++) {
if(document.form1.newsid[i].checked) {
selected = true;
break;
}
}
if(!selected) {
alert("请选择所要删除的新闻!!");
return false;
}
document.form1.action="delete.jsp";
}
</script>
</body>
</html>
2、2、把index.jsp复制到main下,改名为insert.jsp。<[textarea] row[s].. col[s]..>,将此表单提交给InsertServlet,cAf:
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title></title>
</head>
<body>
<h2 align="center">增加新闻</h2>
<hr>
<div align="center">
<form action="/news/InsertServlet" method="post">
新闻标题:
<input type="text" name="newsTitle"/>
<br>
<textarea rows="10" cols="50" name="newsContent">
</textarea>
<br>
<input type="submit" value="增加新闻"/>
<input type="reset" value="重置"/>
</form>
</div>
</body>
</html>
3、src下建立InsertServlet,放在control包下。把Servlet/JSP MappingURL:/servlet/InsertServlet改成/InsertServlet。把doPost方法中的冗余代码Delete,接收页面提交过来的参数newsTitle,newsContent。注意接收的参数名要与提交过来的参数名(name="")一致,cAf:
package org.riker.news.control;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.riker.news.logic.NewsLogicBean;
public class InsertServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public InsertServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsTitle = request.getParameter("newsTitle");
String newsContent = request.getParameter("newsContent");
int result = new NewsLogicBean().insertNews(newsTitle, newsContent);
if(result==1){
request.getRequestDispatcher("main/list.jsp?currentPage=1").forward(request, response);
} else {
request.getRequestDispatcher("main/error.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
4、接下来,我们来写后台Logic中的插入方法(插入的具体实现),打开
org.riker.news.logic包下的NewsLogicBean.java,编写相应的查询语句。
然后回到InsertServlet.java中doPost方法,调用插入数据的方法。去
LoginServlet.java下COPY过来request.getRequestDispatcher("main/list.jsp").forward(request, response); 补充当前页为1这个参数。
否则的话,跳到error.jsp。复制insert.jsp建立error.jsp,操作失败<a href="javascript:history.back();">返回</a>>。若是采用内接Tomcat的话
,此时最好重新部署一下,然后启动服务器运行。Debug:function insertNews() {document.form1.action="main/insert.jsp";}这里main/insert.jsp为正确,/main/insert.jsp则为错误,需要引起高度注意!
此时,进行插入数据,错误!看看提交给谁了?来到insert.jsp中<form action="InsertServlet" method="post">改成<form action="/news/InsertServlet" method="post">,cAf:
package org.riker.news.logic;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.riker.news.comm.DataSource;
import org.riker.news.data.NewsDataBean;
public class NewsLogicBean {
private int allRows;
private int curPage = 1;
private int rowPerPage = 5;
private int allPages;
public List findAll() {
List list = new ArrayList();
Statement st = null;
ResultSet rs = null;
try {
st = DataSource.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery("select newsid, newstitle, newscontent, newscount, newsdate from news order by newsdate desc");
rs.last();
this.allRows = rs.getRow();
if(this.allRows%this.rowPerPage==0) {
this.allPages = this.allRows/this.rowPerPage;
} else {
this.allPages = this.allRows/this.rowPerPage + 1;
}
if(this.curPage == 1) {
rs.beforeFirst();
} else {
rs.absolute((this.curPage - 1) * this.rowPerPage);
}
int i = 0;
while(rs.next() && i < this.rowPerPage) {
NewsDataBean bean = new NewsDataBean();
bean.setNewsid(rs.getString(1));
bean.setNewstitle(rs.getString(2));
bean.setNewscontent(rs.getString(3));
bean.setNewscount(rs.getString(4));
bean.setNewsdate(rs.getString(5));
list.add(bean);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int insertNews(String newsTitle, String newsContent) {
int result = 0;
try {
Statement st = DataSource.getConnection().createStatement();
result = st
.executeUpdate("insert into news(newstitle, newscontent, newscount, newsdate) values ('"
+ newsTitle + "', '" + newsContent + "', 0, now()");
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
public int updateNews(String newsid, String newsTitle, String newsContent) {
int result = 0;
try {
Statement st = DataSource.getConnection().createStatement();
result = st
.executeUpdate("update news set newstitle='"+newsTitle+"', newscontent='"+newsContent+"' where newsid="+newsid);
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
public NewsDataBean findById(String newsid) {
NewsDataBean bean = new NewsDataBean();
Statement st = null;
ResultSet rs = null;
try {
st = DataSource.getConnection().createStatement();
rs = st.executeQuery("select newsid, newstitle, newscontent, newscount, newsdate from news where newsid=" + newsid);
rs.last();
if(rs.next() ) {
bean.setNewsid(rs.getString(1));
bean.setNewstitle(rs.getString(2));
bean.setNewscontent(rs.getString(3));
bean.setNewscount(rs.getString(4));
bean.setNewsdate(rs.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public static void main(String[] args) {
List list = new NewsLogicBean().findAll();
System.out.println(list.size());
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getAllPages() {
return allPages;
}
public int getAllRows() {
return allRows;
}
public int getRowPerPage() {
return rowPerPage;
}
}
insert.jsp
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title></title>
</head>
<body>
<h2 align="center">增加新闻</h2>
<hr>
<div align="center">
<form action="/news/InsertServlet" method="post">
新闻标题:
<input type="text" name="newsTitle"/>
<br>
<textarea rows="10" cols="50" name="newsContent">
</textarea>
<br>
<input type="submit" value="增加新闻"/>
<input type="reset" value="重置"/>
</form>
</div>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title></title>
</head>
<body>
操作失败<a href="javascript:history.back();">返回</a>>
</body>
</html>
Update.jsp
<%@ page contentType="text/html;charset=GBK" import="org.riker.news.data.NewsDataBean"%>
<html>
<head>
<title></title>
</head>
<%
NewsDataBean bean =(NewsDataBean)request.getAttribute("bean");
%>
<body>
<h2 align="center">修改新闻</h2>
<hr>
<div align="center">
<form action="/news/UpdateServlet" method="post">
新闻标题:
<input type="text" name="newsTitle" value="<%= bean.getNewstitle()%>"/>
<br>
<textarea rows="10" cols="50" name="newsContent" >
<%=bean.getNewscontent()%>
</textarea>
<br>
<input type="hidden" name="newsid" value="<%=bean.getNewsid()%>"/>
<input type="submit" value="修改新闻"/>
<input type="reset" value="重置"/>
</form>
</div>
</body>
</html>
package org.riker.news.control;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.riker.news.logic.NewsLogicBean;
public class UpdateServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UpdateServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsid = request.getParameter("newsid");
String newsTitle = request.getParameter("newsTitle");
String newsContent = request.getParameter("newsContent");
int result = new NewsLogicBean().updateNews(newsid, newsTitle, newsContent);
if(result==1){
request.getRequestDispatcher("main/list.jsp?currentPage=1").forward(request, response);
} else {
request.getRequestDispatcher("main/error.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
5、增加写完了,来写修改。修改提交位置,建立FindByIdServlet.java,
编辑NewsLogicBean.java,然后回FindByIdServlet.java调用。NewsDataBean bean = new NewsLogicBean().findById(newsid),cAf:
package org.riker.news.control;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.riker.news.data.NewsDataBean;
import org.riker.news.logic.NewsLogicBean;
public class FindByIdServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public FindByIdServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsid = request.getParameter("newsid");
NewsDataBean bean = new NewsLogicBean().findById(newsid);
if(bean!=null) {
request.setAttribute("bean",bean);
request.getRequestDispatcher("main/update.jsp").forward(request, response);
} else {
request.getRequestDispatcher("main/error.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}