Servlet可以通過以下幾種方式將數據返回給前端:
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("Hello, World!");
response.setContentType("application/octet-stream");
OutputStream out = response.getOutputStream();
byte[] data = "Hello, World!".getBytes();
out.write(data);
response.setHeader("Content-Type", "text/plain");
response.setHeader("Content-Disposition", "attachment; filename=\"data.txt\"");
request.setAttribute("message", "Hello, World!");
以上是Servlet常用的返回數據給前端的方式,可以根據具體的需求選擇合適的方式進行返回數據。