forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadCountries.java
More file actions
40 lines (29 loc) · 1.15 KB
/
ReadCountries.java
File metadata and controls
40 lines (29 loc) · 1.15 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
package com.zetcode.web;
import com.zetcode.bean.Country;
import com.zetcode.service.CountryService;
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.util.List;
import java.util.Optional;
@WebServlet(name = "ReadCountries", urlPatterns = {"/read"})
public class ReadCountries extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
Optional<List<Country>> countries = CountryService.getListOfCountries();
String templateName;
if (countries.isPresent()) {
request.setAttribute("countries", countries.get());
templateName = "listCountries.jsp";
} else {
templateName = "showError.jsp";
}
var dispatcher = request.getRequestDispatcher(templateName);
dispatcher.forward(request, response);
}
}