javaweb学习总结----jsp综合练习

JSP综合练习

模拟数据库

package com.wu.db;

import com.wu.pojo.News;

import java.util.ArrayList;
import java.util.List;

public class NewsDB {

    public static List<News> list = new ArrayList<News>();

    static {
        list.add(new News("7.16","学习了JSP的使用以及源码分析"));
        list.add(new News("7.14","学习了Servlet,简单的JSP的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
        list.add(new News("7.13","学习了Tomcat,Servlet的使用"));
    }

}

实体类

package com.wu.pojo;

//实体类,一般放在pojo包下,【 or  entity  or JavaBeans】
//实体类,只有属性,一般用来映射数据库中的字段  ( O R M )
//私有属性 , 无参构造,有参构造
//get/set方法
//为了方便程序打印,建议加上toString();
public class News {

    private String data;
    private String content;

    public News() {
    }

    public News(String data, String content) {
        this.data = data;
        this.content = content;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "News{" +
                "data='" + data + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

dao层

package com.wu.dao;


//dao包一般用来存放操作数据库的对象类;

import com.wu.pojo.News;

import java.util.List;

public interface NewsDao {
    //获得所有的新闻
    public List<News> getAllNews();

}
package com.wu.dao;

import com.wu.db.NewsDB;
import com.wu.pojo.News;

import java.util.List;

//NewsDao的实现类
public class NewsDaoImpl implements NewsDao {
    @Override
    public List<News> getAllNews() {
        List<News> list = NewsDB.list;
        return list;
    }

}

service层

package com.wu.service;

import com.wu.pojo.News;

import java.util.List;

//service一般存放业务类
public interface NewsService {
    //获得所有的新闻
    public List<News> getAllNews();
}
package com.wu.service;

import com.wu.dao.NewsDao;
import comwu.dao.NewsDaoImpl;
import com.wu.pojo.News;

import java.util.List;

public class NewsServiceImpl implements NewsService {

    //从dao层中取出相应的操作数据库的方法
    NewsDao newsDao = new NewsDaoImpl();

    @Override
    public List<News> getAllNews() {
        return newsDao.getAllNews();
    }
}

servlet

package com.wu.servlet;

import com.wu.pojo.News;
import com.wu.service.NewsService;
import com.wu.service.NewsServiceImpl;
import org.apache.catalina.Session;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewsServlet extends HttpServlet {

    NewsService newsService = new NewsServiceImpl();

    @Test
    public void test(){


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //写代码读取数据存到某个地方,让前端读取;

        List<News> allNews = newsService.getAllNews();//从业务层去获得数据中的全部新闻

        int i = 1;

        for (News allNew : allNews) { //遍历获得每一个新闻

            String data = allNew.getData();
            String content = allNew.getContent();

            //四大作用域
            //      page(不用它,代表当前页面)
            //      request(一次请求中有效): 登录注册
            //      session (一次会话中存在) : 购物车
            //      application:本质就是Context (全局,所有会话共享) : 广告
            //这四个作用域用来存放一些内容或者对象
            HttpSession session = req.getSession();

            session.setAttribute("data"+i,data);
            session.setAttribute("content"+i,content);

            System.out.println(session.getAttribute("data"+i));
            System.out.println(session.getAttribute("content"+i));

            i++;//自增
        }

        //数据都读取出来了,给你放到session中了,该去访问页面了
        resp.sendRedirect("index.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<servlet>
    <servlet-name>newsServlet</servlet-name>
    <servlet-class>com.wu.servlet.NewsServlet</servlet-class>
</servlet>


<servlet-mapping>
    <servlet-name>newsServlet</servlet-name>
    <url-pattern>/news.do</url-pattern>
</servlet-mapping>

index.jsp

<%@ page import="com.wu.db.NewsDB" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>首页</title>

  <%--引入css--%>
  <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/layui.css">
  <%--引入静态资源文件必须带上项目路径位置--%>
  <Script src="${pageContext.request.contextPath}/statics/layui/layui.js"></Script>

</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>常规时间线</legend>
</fieldset>

<ul class="layui-timeline">


<%
  for (int i = 1; i <= NewsDB.list.size(); i++) {
%>

  <li class="layui-timeline-item">
    <i class="layui-icon layui-timeline-axis"></i>
    <div class="layui-timeline-content layui-text">
      <h3 class="layui-timeline-title">
        <%=request.getSession().getAttribute("data"+i)%>
        <%--${sessionScope.data1}--%>
      </h3>
      <p>
        <%--${sessionScope.content1}--%>
          <%=request.getSession().getAttribute("content"+i)%>
      </p>
    </div>
  </li>

<%
  }
%>


  <li class="layui-timeline-item">
    <i class="layui-icon layui-timeline-axis"></i>
    <div class="layui-timeline-content layui-text">
      <div class="layui-timeline-title">过去</div>
    </div>
  </li>
</ul>


</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章