6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
8 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
9 |
|
import javax.servlet.annotation.WebServlet; |
10 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
11 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
29 |
/** |
/** |
30 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
31 |
*/ |
*/ |
32 |
|
@WebServlet(urlPatterns={"/DepartureServlet"}) |
33 |
public class DepartureServlet extends HttpServlet { |
public class DepartureServlet extends HttpServlet { |
34 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
35 |
|
|
95 |
|
|
96 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
97 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
98 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans, stationName, arrival) ); |
99 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
100 |
|
|
101 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
107 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
108 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
109 |
} else { |
} else { |
110 |
throw new ServletException("Unknown format"); |
resp.sendError(400, "Unknown format"); |
111 |
} |
} |
112 |
|
|
113 |
} |
} |
114 |
|
|
115 |
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{ |
protected String formatXml(List<DepartureBean> beans, String stationName, boolean arrival) throws ServletException{ |
116 |
String xml = ""; |
String xml = ""; |
117 |
try { |
try { |
118 |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
122 |
Document doc = impl.createDocument(null,null,null); |
Document doc = impl.createDocument(null,null,null); |
123 |
Element root = doc.createElement("departureinfo"); |
Element root = doc.createElement("departureinfo"); |
124 |
root.setAttribute("station", stationName); |
root.setAttribute("station", stationName); |
125 |
|
root.setAttribute("arrival", Boolean.toString(arrival) ); |
126 |
for (DepartureBean departure : beans) { |
for (DepartureBean departure : beans) { |
127 |
Element train = doc.createElement("train"); |
Element train = doc.createElement("train"); |
128 |
|
|