访问首页怎么自动调用indexservlet?
视频中是直接在地址栏中访问的地址栏,现实中肯定首先访问的是首页,那怎么才能首页加载的时候就自动访问servlet查询数据?能否给些具体的实现。
想了一个方法,但没效果,不知道是不是因为加载到EL表达式的时候后台数据还没传回来导致的。
<script type="text/javascript">
window.onload = function () {
document.getElementById("index").submit();
}
</script>
<form action="${pageContext.request.contextPath }/IndexServlet" method="post" id="index"></form><div class="menu_title" ><a href="###">内容分类</a></div>
<ul id="title">
<c:forEach var="category" items="${categoryList }">
<li>${category.cname }</li>
</c:forEach>
</ul>
</div>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//默认首页初始化的servlet,用来展示商品,以及完成分页显示
//查询所有商品数据,调用业务层
CategoryService categorytService = new CategoryServiceImpl();
List<Category> categoryList = categorytService.findAll();
//跳转到首页
request.setAttribute("categoryList", categoryList);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}18
收起
正在回答
1回答
同学你好,同学可以在WebContent目录下添加一个welcome.jsp作为欢迎页,在web.xml中的<welcome-file-list>标签中设置首页<welcome-file>welcome.jsp</welcome-file>,然后在welcome.jsp页面中,使用重定向到IndexServlet中,如:

这样可以在启动项目后,访问welcome.jsp页面即可访问到localhost:8080/shop/IndexServlet路径中的内容。同学可以自己试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星