每次都需要点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>