news总结
上回的因为停网所以无法上传,被我保存成了一个我不会打开的东西,没法用了。
news:新闻发布系统。
完成状态:差
个人理解度:一知半解
总结目的:秘密
直到现在,我对整个练习的知识点上的理解都不是很好,个人感觉思维梳理的不清晰。所以我就迷迷糊糊的总结了。
要求:如图:
1.登录:需与数据库的管理者表进行正确后方可登录管理页面
如图:
以主题为例(这个简单):
大概的要求就这些个:那么要如何完成它呢》
首先是打大的架子: 刚开始写的时候我直接用的控制台版news的后台代码。
//把biz层改成了servlet了 这就是我最开始的想法。(是为因web是网页,数据传递的方式不同么,所以就换了一个实现的类吧,我的个人感觉)
1.后台数据层的代码用之前的就可以。
2. servlet类 :这个类也是一个Java类,他通过实现servlet接口或继承其他实现了该接口的类
而有了处理请求的能力。
3.jsp :这个是;一种实现动态网页的技术
下面就是正式的开始写了。
1.创建一个web项目
2.配置环境(tomcat)
3.检查是否可以正常运行
4。写
1.数据层代码,先写了,用单元测试跑通。(在编写边学的过程中,我们有使用了连接池,那个不能用单元测试)
例:
package demo.news.dao.impl; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.junit.Test; import demo.news.dao.AdminDao;
import demo.news.dao.BaseDao;
import demo.news.model.Admin; public class AdminDaoImpl extends BaseDao implements AdminDao { /* public String login(Admin admin) {
Object[] o={admin.getAname(),admin.getApwd()};
return loginResult("select aname from admin where aname=? and apwd=?",o); }*/
public String login(Admin admin) {
Object[] o={admin.getAname(),admin.getApwd()};
return loginResult("select aname from admin where aname=? and apwd=?",o); }
public boolean loginGetBool(Admin admin) {
rs= executeSelect("select *from admin where name=? and \"pwd\"=?",admin.getAname(),admin.getApwd());
try {
if(rs.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} @Test
public void test(){ System.out.println(login(new Admin("1","1")));
} public Admin loginGetAdmin(Admin admin) {
QueryRunner query =new QueryRunner();
Object[] o={admin.getAname(),admin.getApwd()};
/* try {
//admin=query.query(getConnection(), "select *from admin where name=? and pwd=?",new BeanHandler<Admin>(Admin.class), o);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/ return admin; } }
2.jsp页面(用的素材)就是一些模板,也可以自己写 跟html差不多。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ include file="/Test.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <base href="<%=basePath%>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻中国</title> <link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body onload="showtime()"> <div id="header">
<div id="top_login">
<!-- ${pageContext.request.contextPath}/LonginServlet -->
<form action="${pageContext.request.contextPath}/LonginServlet" method="post">
<label> 登录名 </label>
<input type="text" name="uname" value='<%=request.getParameter("uname")==null?"":request.getParameter("uname") %>' class="login_input" />
<label> 密 码 </label>
<input type="password" name="upwd" value='<%=request.getParameter("upwd")==null?"":request.getParameter("upwd") %>' class="login_input" />
<input type="submit" class="login_sub" value="登录" />
<label id="error"> </label><div id="clock" style="display:inline-block; margin-left:200px"></div>
<img src="data:images/friend_logo.gif" alt="Google" id="friend_logo" />
</form> </div>
<div id="nav">
<div id="logo"> <img src="data:images/logo.jpg" alt="新闻中国" /> </div>
<div id="a_b01"> <img src="data:images/a_b01.gif" alt="" /> </div>
<!--mainnav end-->
</div>
</div>
<div id="container">
<div class="sidebar">
<!-- 得到全部国内的新闻 -->
<h1> <img src="data:images/title_1.gif" alt="国内新闻" /> </h1>
<div class="side_list">
<ul>
<c:forEach var="item" items="${requestScope.guoNei}">
<li> <a href='#'><b>${item.ntitle}</b></a> </li>
</c:forEach>
</ul>
</div>
<!-- 得到全部国际的新闻 -->
<h1> <img src="data:images/title_2.gif" alt="国际新闻" /> </h1>
<div class="side_list">
<ul>
<c:forEach var="item" items="${requestScope.guoWai}">
<li> <a href='#'><b>${item.ntitle}</b></a> </li>
</c:forEach>
</ul>
</div>
<!-- 得到全部娱乐的新闻 -->
<h1> <img src="data:images/title_3.gif" alt="娱乐新闻" /> </h1>
<div class="side_list">
<ul>
<c:forEach var="item" items="${requestScope.yuLe}">
<li> <a href='#'><b>${item.ntitle}</b></a> </li>
</c:forEach>
</ul>
</div>
</div>
<div class="main">
<div class="class_type"> <img src="data:images/class_type.gif" alt="新闻中心" /> </div>
<div class="content">
<ul class="class_date">
<li>
<!-- 循环得到所有新闻类型 -->
<c:forEach var="item" items="${requestScope.typeList}"> <a href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${item.typeid}" style="color:red; margin-right:10px; font-size:14px;">${item.typeName}</a>
</c:forEach>
</li>
</ul>
<ul class="classlist">
<li><a href='${pageContext.request.contextPath}/CommentServlet?newsid='> 深足教练组:说我们买球是侮辱 朱广沪常暗中支招 </a><span> 2013-06-06 01:03:51.0 </span></li>
<c:set var="newsList" value="${requestScope.newsList}"/>
<!-- 循环得到前三条新闻news -->
<c:forEach var="item" items="${requestScope.newsList}">
<li ><a href="${pageContext.request.contextPath}/CommentServlet?newsid=${item.nid}">${item.ntitle}</a><span> <fmt:formatDate value="${item.startTime}" pattern="yyyy-MM-dd HH:mm:ss" /></span> </li>
</c:forEach>
<p align="right">
<!-- 循环得到显示页数的数字-->
<c:forEach var="item" begin="1" end="${requestScope.newsListLength}" step="1">
<a style="margin-right:6px" href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${requestScope.typeDangID}&startPage=${(item-1)*3}&newPage=${item}">${item}</a>
</c:forEach>
<br/>
<!-- 首页 -->
<a href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${requestScope.typeDangID}&newPage=1">首页</a>
<!-- 判断是否显示上一页 -->
<c:if test="${requestScope.newPage>1}">
<a href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${requestScope.typeDangID}&startPage=${(newPage-2)*3}&newPage=${requestScope.newPage-1}">上一页</a>
</c:if>
当前页数:[${requestScope.newPage}/${requestScope.newsListLength}]
<!-- 判断是否显示下一页 -->
<c:if test="${requestScope.newPage<requestScope.newsListLength}">
<a href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${requestScope.typeDangID}&startPage=${(newPage*3)}&newPage=${requestScope.newPage+1}">下一页</a>
</c:if>
<!-- 末尾页 -->
<a href="${pageContext.request.contextPath}/LoadIndexServlet?typeId=${requestScope.typeDangID}&startPage=${((requestScope.newsListLength-1)*3)-1}&newPage=${requestScope.newsListLength}">末页</a> </p>
</ul>
</div>
<%@include file="index-elements/index_rightbar.html"%>
</div>
</div>
<%@include file="index-elements/index_bottom.html"%>
${sessionScope.index}
<c:remove var="index" />
</body>
</html>
3.servlet
package demo.news.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import demo.news.dao.impl.*;
import demo.news.model.Admin; public class LonginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L; /**
* doGet方法
* */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* doPost
* */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收请求时的编码utf-8
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd"); Admin admin=new Admin(name,pwd);
System.out.println(admin.getAname()); AdminDaoImpl adi=new AdminDaoImpl(); String dbn=adi.login(admin); if(dbn!=null){ Cookie cookie=new Cookie("unameCookie",name);
cookie.setMaxAge(60*60*24); response.addCookie(cookie); System.out.println("登陆成功!");
HttpSession session= request.getSession();
session.setAttribute("uname", name);
session.setMaxInactiveInterval(60*10); response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
} }
我也不知道自己要干啥,也许写写自己能清楚点,这个是大概,其中学了很多东西,如内置对象,文件上传,el表达式,jstl等等 。
随机推荐
- Mac中下载JDK手动更新出现“只支持10.7.3以上的系统版本”问题解决方案
今天倒腾Hadoop,要修改源码,由于之前搞研究是基于hadoop1.0.3来做的,所以这次也用的这个版本,但是我电脑上Java的版本是1.8以上的,结果用ant编译hadoop源码报错,错误大概是这 ...
- HDU5407.CRB and Candies(数论)
官方题解: The problem is just to calculate g(N) = LCM(C(N,0),C(N,1),...,C(N,N)) Introducing function f(n ...
- PC-改变电脑的CPU,内存,硬盘大小!
如何修改CPU频率及内存容量和硬盘大小 改变电脑的CPU,内存,硬盘大小!--------------------------------------------------------------- ...
- hibernate三级缓存介绍
hibernate缓存机制:分为三种:1.一级缓存(session级别缓存,同一session中不会重复发出sql,默认会实现)2.二级缓存(跨session的缓存,不同session会从同一缓存中取 ...
- HTML---Android与js交互实例
Android 中可以通过webview来实现和js的交互,在程序中调用js代码,只需要将webview控件的支持js的属性设置为true Android(Java)与JavaScript(HTML) ...
- MS-SQLSERVER中的MSDTC不可用解决方法
今天在本地机直接在触发器里更新还有一台服务器数据时出现: MSDTC不可用 解决的方法: 在windows控制面版-->管理工具-->服务-->Distributed Tra ...
- [React ] React Fundamentals: Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- Robots协议具体解释
禁止搜索引擎收录的方法(robots.txt) 一.什么是robots.txt文件? 搜索引擎通过一种程序robot(又称spider),自己主动訪问互联网上的网页并获取网页信息.您能够在您的站点中创 ...
- Hash表
Hash表 Hash表也称散列表,也有直接译作哈希表,Hash表是一种特殊的数据结构,它同数组.链表以及二叉排序树等相比较有很明显的区别,它能够快速定位到想要查找的记录,而不是与表中存在的记录的关键字 ...
- 标准I/O库之标准输入、标准输出和标准出错
对一个进程预定义了三个流,并且这三个流可以自动地被进程使用,它们是:标准输入.标准输出和标注出错.这些流引用的文件与文件描述符STDIN_FILENO.STDOUT_FILENO和STDERR_FIL ...