import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintWriter; public class DateS extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(reponse()); } private static String tag(String tag,String contenu){ return "<"+tag+(contenu.length()==0?"/":">"+contenu+""; } private static String tagNL(String tag,String contenu){ return tag(tag,contenu)+"\n"; } private static String html(String contenu){return tagNL("hmtl",contenu);} private static String head(String contenu){return tagNL("head",contenu);} private static String body(String contenu){return tagNL("body",contenu);} private static String title(String contenu){return tag("title",contenu);} private static String i(String contenu){return tag("i",contenu);} private static String br(){return tagNL("br","");} private static String p(String contenu){return tagNL("p",contenu);} private static String reponse(){ return html(head(title("Date en Servlet"))+ body(p("Le "+ i(new java.text.SimpleDateFormat( "EEEEE d MMMMM yyyy HH:mm", java.util.Locale.CANADA_FRENCH). format(new java.util.Date()) ) )+ p("merci d'avoir consulté cette Servlet") ) ); } // pour tester isolement public static void main (String[] args){ System.out.println(reponse()); } }