沒有偶然的運氣,只有不斷的努力! There is no accidental luck, only constant efforts! 首先 新聞發佈會結合了JSP里的Servlet和request對象,response對象還有使用session對象和cookie對象。 -登錄頁面 package ...
沒有偶然的運氣,只有不斷的努力!
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>
結束!謝謝觀看。