Question - Can you refresh servlet in client and server-side automatically?
Answer -
Yes, There are a couple of primary ways in which a servlet can be automatically refreshed. One way is to add a “Refresh” response header containing the number of seconds after which a refresh should occur. The TestServlet class below shows an example of this.
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- performTask(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
- IOException {
- performTask(request, response);
- }
- private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
- IOException {
- response.setContentType("text/html");
- response.addHeader("Refresh", "5");
- PrintWriter out = response.getWriter();
- out.println("TestServlet says hi at " + new Date());
- }
- }