新聞發布系統(首頁)
一.首先建一個web工程,把tomcat容器調好,然后把jdbc的jar包導入
分成架構弄好,如圖所示:

.entity層,Dao層,和實現層都和oop一樣,層與層進行交互,特別說一下業務邏輯層(servlet)
建一個NewServlet類然后繼承HttpServlet從而繼承了它的doGet(),doPost()方法在doGet里進行
調用doPost(request, response);
doPost將泛型集合放入request作用域中,然后進行轉發。
進入index.jsp獲取標題位置進行頁面展示,如圖所示:
<%
List<NewsEntity> list=(List<NewsEntity>)request.getAttribute("list");
for(NewsEntity item:List)
{
%>
<li><a href='newspages/news_read.jsp'> <%=item.getNewsTitle() %> </a><span> <%=item.getNewsCreateDate() %> </span></li>
<%
}
%>
登陸

這是在Servlet里面寫的 LoginServlet 登陸方法
1 private ServletRequest session;
2 public void doGet(HttpServletRequest request, HttpServletResponse response)
3 throws ServletException, IOException {
4 doPost(request, response);
5 }
6 public void doPost(HttpServletRequest request, HttpServletResponse response)
7 throws ServletException, IOException {
8
9 try {
10 BaseDao dd=new BaseDao();
11 String uname=request.getParameter("uname");
12 String upwd=request.getParameter("upwd");
13 String sql="select * from user where userName=? and userPwd=?";
14 Object [] prams={uname,upwd};
15 ResultSet flag=dd.executeSelect(sql, prams);
16 if(flag.next())
17 {
18 HttpSession session=request.getSession();
19 session.setAttribute("unamesession", uname);
20 Cookie cookie=new Cookie("unamesession",uname);
21 cookie.setMaxAge(60*60*24);
22 response.addCookie(cookie);
23 //out.print("<script type='text/javascript'>"+"alert('成功登陸!')");
24 request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response);
25 }
26 else
27 {
28 response.sendRedirect("/index.jsp");
29 }
30 } catch (SQLException e) {
31 e.printStackTrace();
32 }
33 }
這是在jsp主頁面上寫的跳轉
1 <form action="LoginServlet" method="doPost"> 2 <label> 登錄名 </label> 3 <input type="text" name="uname" value="" class="login_input" /> 4 <label> 密  碼 </label> 5 <input type="password" name="upwd" value="" class="login_input" /> 6 <input type="submit" class="login_sub" value="登錄" /> 7 <label id="error"> </label> 8 <img src="images/friend_logo.gif" alt="Google" id="friend_logo" /> 9 </form>
登陸完跳轉

接下來是 添加

這是在impl包里的NewsDaoimpl.java獲取所有返回的方法
1 //添加
2 public boolean Add(News news) {
3 String sql="INSERT INTO news ( ntid ,ntitle ,nauthor ,ncreatedate ,npicpath ,ncontent ,nmodifydate ,nsummary ) VALUES (?,?,?,?,?,?,?,?)";
4 Object [] prams={news.getNtid(),news.getNtitle(),news.getNauthor(),news.getNcreatedate(),news.getNpicpath(),news.getNcontent(),news.getNmodifydate(),news.getNsummary()};
5 boolean flag=executeUpdate(sql,prams);
6 closeAll();
7 return flag;
8 }
提示:最后在web.xml頁面把路徑改好。
添加主題

這是在impl包里的NewsDaoimpl.java獲取所有返回的方法
1 //添加主題
2 @Override
3 public boolean Addtype(Type type) {
4 String sql="INSERT INTO TYPE(ntname) VALUES(?)";
5 Object [] prams={type.getNtname()};
6 boolean flag=executeUpdate(sql, prams);
7 closeAll();
8 return flag;
9 }
這是添加主題的Servlet類
動態加載新聞主題和新聞分頁


