老师关于页面跳转转发的问题????
在这里通过写输出打印参考语句的方式debug出,页面无法跳转是因为转发语句没起到作用但是不知道错误在哪,还请老师多讲讲,麻烦老师们了!
Servlet代码
=================================================================
package com.goudan.jdbc.servlet;
import com.goudan.jdbc.bean.Message;
import com.goudan.jdbc.service.MessageService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
/**
* 消息列表的Servlet
*/
public class MessageListServlet extends HttpServlet {
private MessageService messageService;
@Override
public void init() throws ServletException {
super.init();
messageService = new MessageService();
System.out.println("0000000");
}
/**
* 分页功能:故此处需要分页功能
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pageStr = request.getParameter("page");//获取当前页码
int page = 1;//页码默认值是1,当拿不到页面值的时候
if (pageStr != null && Objects.equals("", pageStr)) {
page = Integer.parseInt(pageStr);
}
List<Message> messages = messageService.getMessages(page, 5);//分页查询全部留言
int count = messageService.countMessages();
int last = count % 5 == 0 ? (count / 5) : ((count / 5) + 1);
request.setAttribute("last", last);
request.setAttribute("messgaes", messages);
request.setAttribute("page", page);
System.out.println("111111");
request.getRequestDispatcher("/WEB-INF/views/biz/message_list.jsp").forward(request,response);
}
@Override
public void destroy() {
super.destroy();
messageService = null;
System.out.println("2222222222");
}
}
=================================================================
xml配置文件
=================================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>MessageListServlet</servlet-name>
<servlet-class>com.goudan.jdbc.servlet.MessageListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageListServlet</servlet-name>
<url-pattern>/message/list.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error/500.jsp</location>
</error-page>
</web-app>
=================================================================
index页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>messageBorad</title>
<
<meta http-equiv="refresh" content="0;url=<%=request.getContextPath()%>/message/list.do">
</head>
</html>
正在回答
你好同学,测试了你的代码是可以实现跳转到message_list.jsp页面的。如果你要看是否实现了跳转,应该在跳转代码的下面打印信息呀,在request.getRequestDispatcher前打信息是验证不了是否跳转的。另外,没有实现跳转是否有报错信息呢?祝学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星