|
17 | 17 | */
|
18 | 18 | package de.dominikschadow.webappsecurity.servlets;
|
19 | 19 |
|
20 |
| -import java.io.IOException; |
21 |
| -import java.io.InputStream; |
22 |
| -import java.io.PrintWriter; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.w3c.dom.Document; |
| 23 | +import org.w3c.dom.NodeList; |
| 24 | +import org.xml.sax.SAXException; |
23 | 25 |
|
24 | 26 | import javax.annotation.PostConstruct;
|
25 |
| -import javax.servlet.ServletException; |
26 | 27 | import javax.servlet.annotation.WebServlet;
|
27 | 28 | import javax.servlet.http.HttpServlet;
|
28 | 29 | import javax.servlet.http.HttpServletRequest;
|
|
34 | 35 | import javax.xml.xpath.XPathExpression;
|
35 | 36 | import javax.xml.xpath.XPathExpressionException;
|
36 | 37 | import javax.xml.xpath.XPathFactory;
|
37 |
| - |
38 |
| -import org.slf4j.Logger; |
39 |
| -import org.slf4j.LoggerFactory; |
40 |
| -import org.w3c.dom.Document; |
41 |
| -import org.w3c.dom.NodeList; |
42 |
| -import org.xml.sax.SAXException; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.io.PrintWriter; |
43 | 41 |
|
44 | 42 | /**
|
45 | 43 | * Servlet using an XPath expression to query the customer XML document.
|
|
52 | 50 | @WebServlet(name = "XPathServlet", urlPatterns = {"/XPathServlet"})
|
53 | 51 | public class XPathServlet extends HttpServlet {
|
54 | 52 | private static final Logger LOGGER = LoggerFactory.getLogger(XPathServlet.class);
|
55 |
| - private static final long serialVersionUID = 1L; |
56 |
| - private Document doc; |
| 53 | + private static Document doc; |
57 | 54 |
|
58 | 55 | @PostConstruct
|
59 | 56 | @Override
|
60 | 57 | public void init() {
|
61 |
| - InputStream inputStream = null; |
62 |
| - |
63 |
| - try { |
64 |
| - inputStream = getClass().getClassLoader().getResourceAsStream("/customer.xml"); |
| 58 | + try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/customer.xml");) { |
65 | 59 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
66 | 60 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
67 | 61 | doc = dBuilder.parse(inputStream);
|
68 | 62 | } catch (SAXException | IOException | ParserConfigurationException ex) {
|
69 | 63 | LOGGER.error(ex.getMessage(), ex);
|
70 |
| - } finally { |
71 |
| - if (inputStream != null) { |
72 |
| - try { |
73 |
| - inputStream.close(); |
74 |
| - } catch (IOException ex) { |
75 |
| - LOGGER.error(ex.getMessage(), ex); |
76 |
| - } |
77 |
| - } |
78 | 64 | }
|
79 | 65 | }
|
80 | 66 |
|
81 | 67 | @Override
|
82 |
| - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
| 68 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
83 | 69 | String name = request.getParameter("name");
|
84 | 70 | String password = request.getParameter("password");
|
85 | 71 | LOGGER.info("Received {} and {} as parameter", name, password);
|
|
0 commit comments