摘要:
没有偶然的运气,只有不断的努力!首先新闻发布会结合了JSP里的Servlet和request对象,response对象还有使用session对象和cookie对象。";Object[]paras={info.getUname(),info.getUpwd()};getConection();rs=executeQuery;if{intcount=rs.getInt;if{//登录成功flag=true;}}closeResources();returnflag;}packagecn.news.serlvet;importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.SQLException;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcn.news.entity.Topic;importcn.news.service.ITopicService;importcn.news.service.impl.TopicServiceImpl;publicclassTopicServletextendsHttpServlet{publicvoiddoGetthrowsServletException,IOException{doPost;}publicvoiddoPostthrowsServletException,IOException{ITopicServiceservice=newTopicServiceImpl();try{Listlist=service.getAllTopics();request.setAttribute;//转发到列表页面request.getRequestDispatcher.forward;}catch{}}}登录名密 码
没有偶然的运气,只有不断的努力!
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="http://t.zoukankan.com/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" name="uname" value="" /> <label> 密  码 </label> <input type="password" name="upwd" value="" /> <input type="submit" value="登录" onclick="login()"/> </form> <label id="error"> </label> <img src="http://t.zoukankan.com/images/friend_logo.gif" alt="Google" /> </div> <div id="nav"> <div id="logo"> <img src="http://t.zoukankan.com/images/logo.jpg" alt="新闻中国" /> </div> <div id="a_b01"> <img src="http://t.zoukankan.com/images/a_b01.gif" alt="" /> </div> <!--mainnav end--> </div> </div> <div id="container"> <div class="sidebar"> <h1> <img src="http://t.zoukankan.com/images/title_1.gif" alt="国内新闻" /> </h1> <div class="side_list"> <ul> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 景区,如何不再依靠门票收入 </b></a> </li> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 高考期间中东部地区将现大范围降雨 </b></a> </li> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 山西离柳焦煤集团井下人行车发生事故6人死亡 </b></a> </li> </ul> </div> <h1> <img src="http://t.zoukankan.com/images/title_2.gif" alt="国际新闻" /> </h1> <h1> <img src="http://t.zoukankan.com/images/title_3.gif" alt="娱乐新闻" /> </h1> <div class="side_list"> <ul> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> "星跳水立方"决战临近 陈楚生被华谊要求进前三 </b></a> </li> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 《新恋爱时代》登东方卫视 《非诚》元素遭删除 </b></a> </li> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 《海角七号》导演新片开机 吴宇森等出席 </b></a> </li> <li> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 《致命黑兰》佐伊坐拥7种武器 暴力登陆全国院线 </b></a> </li> </ul> </div> </div> <div class="main"> <div class="class_type"> <img src="http://t.zoukankan.com/images/class_type.gif" alt="新闻中心" /> </div> <div class="content"> <ul class="class_date"> <li id='class_month'> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 国内 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 国际 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 军事 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 体育 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 娱乐 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 社会 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 财经 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 科技 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 健康 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 汽车 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 教育 </b></a> </li> <li id='class_month'> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 房产 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 家居 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 旅游 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 文化 </b></a> <a href='http://t.zoukankan.com/wangdan123-p-6777721.html#'><b> 其他 </b></a> </li> </ul> <ul class="classlist"> <% Page onepage=(Page)request.getAttribute("onepage"); for(NewsInfo item : onepage.getList()){ %> <li> <a href='http://t.zoukankan.com/news_read.jsp'> <%=item.getNtitle() %> </a><span> <%=item.getNcreateda() %> </span> </li> <% } %> <p align="right"> 当前页数:[1/2] <a href="http://t.zoukankan.com/UserInfoServlet?pageIndex=<%=onepage.getPageIndex()+1%>">下一页</a> <a href="http://t.zoukankan.com/wangdan123-p-6777721.html#">末页</a> </p> </ul> </div> <div class="picnews"> <ul> <li> <a href="http://t.zoukankan.com/wangdan123-p-6777721.html#"><img src="http://t.zoukankan.com/images/Picture1.jpg" alt="" /> </a><a href="http://t.zoukankan.com/wangdan123-p-6777721.html#">幻想中穿越时空</a> </li> <li> <a href="http://t.zoukankan.com/wangdan123-p-6777721.html#"><img src="http://t.zoukankan.com/images/Picture2.jpg" alt="" /> </a><a href="http://t.zoukankan.com/wangdan123-p-6777721.html#">国庆多变的发型</a> </li> <li> <a href="http://t.zoukankan.com/wangdan123-p-6777721.html#"><img src="http://t.zoukankan.com/images/Picture3.jpg" alt="" /> </a><a href="http://t.zoukankan.com/wangdan123-p-6777721.html#">新技术照亮都市</a> </li> <li> <a href="http://t.zoukankan.com/wangdan123-p-6777721.html#"><img src="http://t.zoukankan.com/images/Picture4.jpg" alt="" /> </a><a href="http://t.zoukankan.com/wangdan123-p-6777721.html#">群星闪耀红地毯</a> </li> </ul> </div> </div> </div> <div id="footer"> <iframe src="http://t.zoukankan.com/index-elements/index_bottom.html" scrolling="no" frameborder="0" height="190px"></iframe> </div> </body> </html>
<!DOCTYPE html> <html> <head> <title> <%= title %></title> <link rel='stylesheet' href='http://t.zoukankan.com/stylesheets/style.css' /> <script src="http://t.zoukankan.com/wangdan123-p-6777721.html" type="text/javascript"></script> </head> <body> <h1> <%= title %></h1> <div> 标题:<input type="text" /> </div> <div> 内容:<textarea id="content"></textarea> </div> <div> <input type="button" type="button" 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>
结束!谢谢观看。