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="images/friend_logo.gif" alt="Google" id="friend_logo" />
    </form>

  </div>
  <div id="nav">
    <div id="logo"> <img src="images/logo.jpg" alt="新闻中国" /> </div>
    <div id="a_b01"> <img src="images/a_b01.gif" alt="" /> </div>
    <!--mainnav end-->
  </div>
</div>
<div id="container">
  <div class="sidebar">
    <!-- 得到全部国内的新闻 -->
    <h1> <img src="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="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="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="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等等  。

                                                                    

 

posted on 2015-11-26 01:29  上雨  阅读(259)  评论(0编辑  收藏  举报

导航