每次都需要点submit才可以显示数据。
我感觉是因为页面需要请求才能进行反应,然后我jsp初次加载是没有请求的。
该怎么解决??
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.PersonDao;
import dao.PersonDaoImpl;
import entity.Person;
@WebServlet("/Test01Servlet")
public class Test01Servlet extends HttpServlet {
public Test01Servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String z = request.getParameter("mm");
if("showall".equals(z)){
List<Person> l = new PersonDaoImpl().showAll();
request.setAttribute("plist", l);
request.getRequestDispatcher("test01.jsp").forward(request, response);
}
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.*" %>
<%@ page import="entity.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test01</title>
</head>
<body>
<form action="Test01Servlet?mm=showall" method="post">
<table border="1">
<tr>
<th>name</th>
<th>age</th>
<th>phone</th>
<th>city</th>
</tr>
<c:forEach items="${plist }" var="p">
<tr>
<td>${p.name }</td>
<td>${p.age }</td>
<td>${p.phones }</td>
<td>${p.cities }</td>
</tr>
</c:forEach>
</table>
<input type="submit" value="show">
</form>
</body>
</html>
首次加载的时候的路径 直接请求到servlet 然后重定向到前端jsp页面 就可以了
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y