• +91 9723535972
  • info@interviewmaterial.com

Servlet Interview Questions and Answers

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.

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.Date;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  9.       private static final long serialVersionUID = 1L;
  10.       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11.             performTask(request, response);
  12.       }
  13.       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  14.       IOException {
  15.             performTask(request, response);
  16.       }
  17.       private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  18.       IOException {
  19.             response.setContentType("text/html");
  20.             response.addHeader("Refresh", "5");
  21.             PrintWriter out = response.getWriter();
  22.             out.println("TestServlet says hi at " + new Date());
  23.       }
  24. }

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners