forked from marekbruchaty/SimpleJavaWebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterServlet.java
More file actions
51 lines (40 loc) · 1.68 KB
/
CounterServlet.java
File metadata and controls
51 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Marek on 13/04/2017.
*/
@WebServlet(name = "CounterServlet")
public class CounterServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
Integer counter = 0;
getServletContext().setAttribute("counter", counter);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer counter = (Integer) getServletContext().getAttribute("counter");
counter++;
getServletContext().setAttribute("counter", counter);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try (PrintWriter writer = response.getWriter()) {
writer.println("<!DOCTYPE html><html>");
writer.println("<head>");
writer.println("<meta charset=\"UTF-8\" />");
writer.println("<title>CounterServlet using doGet() to increment local variable</title>");
writer.println("</head>");
writer.println("<body>");
writer.println("<h1>You are our " + counter + ". customer</h1>");
writer.println("</body>");
writer.println("</html>");
}
}
}