1.注意的地方
1.第一点
记得在JSP页面加上
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
这句话很重要,如果没有,那么JSP页面就无法显示出信息
记得导入包
2.第二点
正确的使用数组
数组的长度是一定的,如果要传入未知长度的数据,记得使用List,使用List要记得初始化,即
List < String> a = new ArrayList(); 不能单纯的写List< String> a;,添加数组元素的时候要使用add方法
2.例子
Servlet
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ShowFiles
*/
@WebServlet("/ShowFiles")
public class ShowFiles extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowFiles() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
List<String> a = new ArrayList();
String[] test={"a","b","c"};
int j=0;String b;
String path = "G:\\rsdownload"; // 路径
File f = new File(path);
if (!f.exists()) {
System.out.println(path + " not exists");
return;
}
File fa[] = f.listFiles();
for (int i = 0; i < fa.length; i++) {
File fs = fa[i];
if (fs.isDirectory()) {
//System.out.println(fs.getName()+" "+i);
b=fs.getName();
a.add(b);
} else {
//System.out.println(fs.getName());
b=fs.getName();
a.add(b);
}
}
request.setAttribute("File", a);
request.setAttribute("test", test);
System.out.println(a);
request.getRequestDispatcher("listFiles.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<!--<%request.getAttribute("File"); %> -->
<table class="table">
<tr class="table_header">
<td>
File Name
</td>
<td>
Downlink
</td>
</tr>
<c:forEach items="${File }" var="File" >
<td>
${File}<br>
</td>
<td>
www.google.com
</td>
<tr>
</c:forEach>
</table>
</body>
</html>
运行结果
对于从Servlet到JSP的中文乱码问题,我们要在doGet方法的第一句加上response.setCharacterEncoding(“utf-8”);这里是要么把response.getWriter().append("Served at: ").append(request.getContextPath());这句话删掉,要么就把response.setCharacterEncoding(“utf-8”);写在它的前面,而不要写在它的后面就行了。
3.参考
https://blog.csdn.net/qq_34646449/article/details/76146659
https://zhidao.baidu.com/question/394152982.html
https://zhidao.baidu.com/question/1447371832991262740.html
https://blog.csdn.net/eminem2011/article/details/7065860
http://ask.zol.com.cn/x/5321764.html
https://blog.csdn.net/x763795151/article/details/52472014