servlet访问首页前的工作

来源:互联网 发布:约束最优化方法 编辑:程序博客网 时间:2024/06/10 07:16

在访问一个web项目主页前,通常不直接跳转到主页,在这之前要做一系列工作,比如一个购物网站,需要先拿到热门,最新的商品,再跳转到首页显示,具体步骤是

1,配置web.xml

<display-name>myStore</display-name><welcome-file-list>    <welcome-file>default.jsp</welcome-file></welcome-file-list>
 这是欢迎页面,就是通过项目名直接访问的页面,display-name是项目名

2,新建default.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>    <%//        跳转到index servlet拿数据        response.sendRedirect(request.getContextPath()+"/index");    %></body></html>
3,到index   servlet上面拿数据再跳转到真正的首页

request.setAttribute("hotProductList",hotProduct);request.setAttribute("newProductList",newProduct);request.getRequestDispatcher("/index.jsp").forward(request,response);


4,这时我们直接访问localhost:8080/myStore

原创粉丝点击