package task5;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html; charset=UTF-8");
		PrintWriter out = resp.getWriter();

		//  
		String currentDateTime = USER_DATEFORMAT.format(Calendar.getInstance().getTime());
		//    :
		out.print(htmlTemplate.replace("%MESSAGE%", currentDateTime));
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

	}

	//   
	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		//      
		String resource = servletConfig.getInitParameter("demoPage");
		demoPageLocation =  servletConfig.getServletContext().getRealPath(resource);
		
		//      
		try (BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(demoPageLocation), StandardCharsets.UTF_8))) {

	        StringBuilder sb = new StringBuilder();
	        String line = br.readLine();

	        while (line != null) {
	            sb.append(line);
	            sb.append(System.lineSeparator());
	            line = br.readLine();
	        }
	        htmlTemplate = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static final SimpleDateFormat USER_DATEFORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
	private String htmlTemplate;
	private static String demoPageLocation;
	private static final long serialVersionUID = 5219320155836069023L;
}

@@ -719,17 +719,33 @@
 ;%0A%0A%09%09// 
- 
+%D0%A4%D0%BE%D1%80%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5 %D0%B4%D0%B0%D1%82%D1%8B
 %0A%09%09Strin
@@ -826,18 +826,8 @@
 ));%0A
-%09%09//    :%0A
 %09%09ou
@@ -1003,18 +1003,44 @@
 %09%7D%0A%0A%09// 
-  
+%C3%8C%C3%A5%C3%B2%C3%AE%C3%A4 %C3%A8%C3%AD%C3%A8%C3%B6%C3%A8%C3%A0%C3%AB%C3%A8%C3%A7%C3%A0%C3%B6%C3%A8%C3%A8 %C3%B1%C3%A5%C3%B0%C3%A2%C3%AB%C3%A5%C3%B2%C3%A0
 %0A%09@Overr
@@ -1150,21 +1150,71 @@
 );%0A%09%09// 
-     
+%C3%91%C3%B7%C3%A8%C3%B2%C3%BB%C3%A2%C3%A0%C3%A5%C3%AC %C3%AF%C3%A0%C3%B0%C3%A0%C3%AC%C3%A5%C3%B2%C3%B0 %C3%B1 %C3%B0%C3%A0%C3%B1%C3%AF%C3%AE%C3%AB%C3%AE%C3%A6%C3%A5%C3%AD%C3%A8%C3%A5%C3%AC %C3%B1%C3%B2%C3%A0%C3%B2%C3%A8%C3%B7%C3%A5%C3%B1%C3%AA%C3%AE%C3%A3%C3%AE %C3%B0%C3%A5%C3%B1%C3%B3%C3%B0%C3%B1%C3%A0
 %0A%09%09Strin
@@ -1357,21 +1357,56 @@
 %09%09%0A%09%09// 
-     
+%C3%91%C3%B7%C3%A8%C3%B2%C3%BB%C3%A2%C3%A0%C3%A5%C3%AC %C3%A4%C3%A0%C3%AD%C3%AD%C3%BB%C3%A5 %C3%A8%C3%A7 %C3%B8%C3%A0%C3%A1%C3%AB%C3%AE%C3%AD%C3%A0 %C3%A2 %C3%AF%C3%A5%C3%B0%C3%A5%C3%AC%C3%A5%C3%AD%C3%AD%C3%B3%C3%BE
 %0A%09%09try (

Update MainServlet.java