没有偶然的运气,只有不断的努力!
There is no accidental luck, only constant efforts!
首先 新闻发布会结合了JSP里的Servlet和request对象,response对象还有使用session对象和cookie对象。


-登录页面

package cn.news.dao.impl;
import java.sql.SQLException;
import org.junit.Test;
import cn.news.dao.BaseDAO;
import cn.news.dao.IUserInfoDAO;
import cn.news.entity.UserInfo;
public class UserInfoDAOImpl extends BaseDAO implements IUserInfoDAO {
@Test
public void loginTest() throws SQLException{
UserInfo info =new UserInfo();
info.setUname("admin");
info.setUpwd("admin");
boolean flag= isLogin(info);
if (flag) {
System.out.println("login success!");
}
}
public boolean isLogin(UserInfo info) throws SQLException {
boolean flag=false;
String sql="select count(1) as mycount from userinfo where uname=? and upwd=?";
Object[] paras={info.getUname(),info.getUpwd()};
getConection();
rs=executeQuery(sql,paras);
if (rs.next()) {
int count = rs.getInt("mycount");
if (count>0) {
//登录成功
flag=true;
}
}
closeResources();
return flag;
}
package cn.news.serlvet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.news.entity.Topic;
import cn.news.service.ITopicService;
import cn.news.service.impl.TopicServiceImpl;
public class TopicServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ITopicService service=new TopicServiceImpl();
try {
List<Topic> list = service.getAllTopics();
request.setAttribute("topicList",list);
//转发到列表页面
request.getRequestDispatcher("/newspages/topic_list.jsp").forward(request, response);
} catch (SQLException e) {
}
}
}

<%@page import="cn.news.entity.Page"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="cn.news.entity.NewsInfo"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<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>
<%
Context ctx=new InitialContext();
String ss=(String)ctx.lookup("java:comp/env/hehe");
%>
<%=ss %>
<div id="header">
<div id="top_login">
<%--
登录区域 需要程序员构建表单
--%>
<form action="UserInfoServlet?action=login" method="post">
<label> 登录名 </label>
<input type="text" id="uname" name="uname" value="" class="login_input" />
<label> 密  码 </label>
<input type="password" id="upwd" name="upwd" value="" class="login_input" />
<input type="submit" class="login_sub" value="登录" onclick="login()"/>
</form>
<label id="error"> </label>
<img src="images/friend_logo.gif" alt="Google" id="friend_logo" /> </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>
<li> <a href='#'><b> 景区,如何不再依靠门票收入 </b></a> </li>
<li> <a href='#'><b> 高考期间中东部地区将现大范围降雨 </b></a> </li>
<li> <a href='#'><b> 山西离柳焦煤集团井下人行车发生事故6人死亡 </b></a> </li>
</ul>
</div>
<h1> <img src="images/title_2.gif" alt="国际新闻" /> </h1>
<h1> <img src="images/title_3.gif" alt="娱乐新闻" /> </h1>
<div class="side_list">
<ul>
<li> <a href='#'><b> "星跳水立方"决战临近 陈楚生被华谊要求进前三 </b></a> </li>
<li> <a href='#'><b> 《新恋爱时代》登东方卫视 《非诚》元素遭删除 </b></a> </li>
<li> <a href='#'><b> 《海角七号》导演新片开机 吴宇森等出席 </b></a> </li>
<li> <a href='#'><b> 《致命黑兰》佐伊坐拥7种武器 暴力登陆全国院线 </b></a> </li>
</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 id='class_month'> <a href='#'><b> 国内 </b></a> <a href='#'><b> 国际 </b></a> <a href='#'><b> 军事 </b></a> <a href='#'><b> 体育 </b></a> <a href='#'><b> 娱乐 </b></a> <a href='#'><b> 社会 </b></a> <a href='#'><b> 财经 </b></a> <a href='#'><b> 科技 </b></a> <a href='#'><b> 健康 </b></a> <a href='#'><b> 汽车 </b></a> <a href='#'><b> 教育 </b></a> </li>
<li id='class_month'> <a href='#'><b> 房产 </b></a> <a href='#'><b> 家居 </b></a> <a href='#'><b> 旅游 </b></a> <a href='#'><b> 文化 </b></a> <a href='#'><b> 其他 </b></a> </li>
</ul>
<ul class="classlist">
<%
Page onepage=(Page)request.getAttribute("onepage");
for(NewsInfo item : onepage.getList()){
%>
<li> <a href='news_read.jsp'> <%=item.getNtitle() %> </a><span> <%=item.getNcreateda() %> </span> </li>
<%
}
%>
<p align="right"> 当前页数:[1/2] <a href="UserInfoServlet?pageIndex=<%=onepage.getPageIndex()+1%>">下一页</a> <a href="#">末页</a> </p>
</ul>
</div>
<div class="picnews">
<ul>
<li> <a href="#"><img src="images/Picture1.jpg" width="249" alt="" /> </a><a href="#">幻想中穿越时空</a> </li>
<li> <a href="#"><img src="images/Picture2.jpg" width="249" alt="" /> </a><a href="#">国庆多变的发型</a> </li>
<li> <a href="#"><img src="images/Picture3.jpg" width="249" alt="" /> </a><a href="#">新技术照亮都市</a> </li>
<li> <a href="#"><img src="images/Picture4.jpg" width="249" alt="" /> </a><a href="#">群星闪耀红地毯</a> </li>
</ul>
</div>
</div>
</div>
<div id="footer">
<iframe src="index-elements/index_bottom.html" scrolling="no" frameborder="0" width="947px" height="190px"></iframe>
</div>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>
<%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src="javascripts/zepto.js" type="text/javascript"></script>
</head>
<body>
<h1>
<%= title %></h1>
<div>
标题:<input type="text" id="title" />
</div>
<div>
内容:<textarea id="content"></textarea>
</div>
<div>
<input type="button" type="button" id="ok" value="添加新闻" />
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#ok').click(function () {
var param = {};
param.title = $('#title').val();
param.content = $('#content').val();
$.post('/addNews', param, function () {
console.log('添加成功');
});
});
});
</script>
</body>
结束!谢谢观看。
