Java by API/javax.servlet/ServletResponse — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
ServletResponse: getBufferSize()
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Buffering extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setBufferSize(8 * 1024); // 8K
res.setContentType("text/html");
PrintWriter out = res.getWriter();
int size = res.getBufferSize(); // returns 8096 or greater
log("The default buffer size is " + size);
out.println("The client won"t see this");
res.reset();
out.println("Nor will the client see this!");
res.reset();
out.println("And this won"t be seen if sendError() is called");
if (req.getParameter("param") == null) {
res.sendError(res.SC_BAD_REQUEST, "param needed");
}
}
}
ServletResponse: getWriter()
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MainServlets extends HttpServlet {
int count;
public void init() throws ServletException {
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
} catch (NumberFormatException e) {
count = 0;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading (and with a possible initialization");
out.println("parameter figured in), this servlet has been accessed");
out.println(count + " times.");
}
}
ServletResponse: reset()
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Buffering extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setBufferSize(8 * 1024); // 8K
res.setContentType("text/html");
PrintWriter out = res.getWriter();
int size = res.getBufferSize(); // returns 8096 or greater
log("The default buffer size is " + size);
out.println("The client won"t see this");
res.reset();
out.println("Nor will the client see this!");
res.reset();
out.println("And this won"t be seen if sendError() is called");
if (req.getParameter("param") == null) {
res.sendError(res.SC_BAD_REQUEST, "param needed");
}
}
}
ServletResponse: setContentType(String arg0)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MainServlets extends HttpServlet {
int count;
public void init() throws ServletException {
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
} catch (NumberFormatException e) {
count = 0;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading (and with a possible initialization");
out.println("parameter figured in), this servlet has been accessed");
out.println(count + " times.");
}
}