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常用的返回数据给前端的方式,可以根据具体的需求选择合适的方式进行返回数据。