franta-hg@177
|
1 |
package cz.frantovo.nekurak.servlet;
|
franta-hg@177
|
2 |
|
franta-hg@177
|
3 |
import java.io.IOException;
|
franta-hg@177
|
4 |
import java.util.Locale;
|
franta-hg@179
|
5 |
import javax.servlet.RequestDispatcher;
|
franta-hg@177
|
6 |
import javax.servlet.ServletException;
|
franta-hg@177
|
7 |
import javax.servlet.http.HttpServlet;
|
franta-hg@177
|
8 |
import javax.servlet.http.HttpServletRequest;
|
franta-hg@177
|
9 |
import javax.servlet.http.HttpServletResponse;
|
franta-hg@177
|
10 |
|
franta-hg@177
|
11 |
/**
|
franta-hg@177
|
12 |
*
|
franta-hg@177
|
13 |
* @author fiki
|
franta-hg@177
|
14 |
*/
|
franta-hg@177
|
15 |
public class SuperServlet extends HttpServlet {
|
franta-hg@177
|
16 |
|
franta-hg@177
|
17 |
public static final String PARAMETR_JAZYK = "jazyk";
|
franta-hg@177
|
18 |
private static final String ATRIBUT_JAZYK = "javax.servlet.jsp.jstl.fmt.locale.session";
|
franta-hg@179
|
19 |
private static final String ADRESAR_SABLON = "/WEB-INF/casti/";
|
franta-hg@177
|
20 |
|
franta-hg@177
|
21 |
@Override
|
franta-hg@177
|
22 |
protected void doGet(HttpServletRequest požadavek, HttpServletResponse odpověď) throws ServletException, IOException {
|
franta-hg@177
|
23 |
společné(požadavek, odpověď);
|
franta-hg@177
|
24 |
}
|
franta-hg@177
|
25 |
|
franta-hg@177
|
26 |
/**
|
franta-hg@193
|
27 |
* <p>Vytáhne z požadované cesty (konec URL) ID objektu.</p>
|
franta-hg@193
|
28 |
*
|
franta-hg@193
|
29 |
* <p>Příklad:
|
franta-hg@193
|
30 |
* pro <code>GET /podnik/123</code>
|
franta-hg@193
|
31 |
* vrátí 123 (podnik je název objektu/akce)
|
franta-hg@193
|
32 |
* </p>
|
franta-hg@193
|
33 |
*
|
franta-hg@193
|
34 |
* @param požadavek HTTP požadavek
|
franta-hg@193
|
35 |
* @return ID objektu
|
franta-hg@193
|
36 |
*/
|
franta-hg@193
|
37 |
protected int getIdCesty(HttpServletRequest požadavek) {
|
franta-hg@193
|
38 |
String[] parametryCesty = (požadavek.getPathInfo().split("/"));
|
franta-hg@193
|
39 |
return Integer.parseInt(parametryCesty[1]);
|
franta-hg@193
|
40 |
}
|
franta-hg@193
|
41 |
|
franta-hg@193
|
42 |
/**
|
franta-hg@179
|
43 |
* Vykreslí stránku pomocí JSP šablony
|
franta-hg@179
|
44 |
* @param jsp JSP šablona stránky – např. detail.jsp (nacházejí se v /WEB-INF/casti/)
|
franta-hg@179
|
45 |
*/
|
franta-hg@179
|
46 |
protected void zpracujJSP(String jsp, HttpServletRequest požadavek, HttpServletResponse odpověď) throws ServletException, IOException {
|
franta-hg@179
|
47 |
/** Nastavení správného MIME typu */
|
franta-hg@179
|
48 |
//požadavek.setAttribute("jspMimeTyp", MIME_TYP);
|
franta-hg@179
|
49 |
/**
|
franta-hg@179
|
50 |
* odpověď.setContentType(MIME_TYP); – nefunguje (JSP si tam procpe svoje text/xml)
|
franta-hg@179
|
51 |
*/
|
franta-hg@179
|
52 |
/*
|
franta-hg@179
|
53 |
* Mělo by jít změnit přes web.xml:
|
franta-hg@179
|
54 |
<jsp-config>
|
franta-hg@179
|
55 |
<jsp-property-group>
|
franta-hg@179
|
56 |
<url-pattern>/</url-pattern>
|
franta-hg@179
|
57 |
<default-content-type>application/xhtml+xml</default-content-type>
|
franta-hg@179
|
58 |
</jsp-property-group>
|
franta-hg@179
|
59 |
</jsp-config>
|
franta-hg@179
|
60 |
*/
|
franta-hg@179
|
61 |
RequestDispatcher rd = getServletContext().getRequestDispatcher(ADRESAR_SABLON + jsp);
|
franta-hg@179
|
62 |
rd.forward(požadavek, new XhtmlOdpověď(odpověď));
|
franta-hg@179
|
63 |
}
|
franta-hg@179
|
64 |
|
franta-hg@179
|
65 |
/**
|
franta-hg@177
|
66 |
* Společné činnosti, které děláme pro všechny HTTP metody (GET, POST atd.)
|
franta-hg@177
|
67 |
*/
|
franta-hg@177
|
68 |
private void společné(HttpServletRequest požadavek, HttpServletResponse odpověď) throws ServletException, IOException {
|
franta-hg@177
|
69 |
nastavJazyk(požadavek);
|
franta-hg@177
|
70 |
}
|
franta-hg@177
|
71 |
|
franta-hg@177
|
72 |
/**
|
franta-hg@177
|
73 |
* Pokud se chce uživatel ručně přepnout na jiný jazyk, než jaký posílá jeho prohlížeč.
|
franta-hg@177
|
74 |
*/
|
franta-hg@177
|
75 |
private void nastavJazyk(HttpServletRequest požadavek) {
|
franta-hg@177
|
76 |
String jazyk = požadavek.getParameter(PARAMETR_JAZYK);
|
franta-hg@177
|
77 |
if (jazyk != null) {
|
franta-hg@177
|
78 |
požadavek.getSession().setAttribute(ATRIBUT_JAZYK, new Locale(jazyk));
|
franta-hg@177
|
79 |
}
|
franta-hg@177
|
80 |
}
|
franta-hg@177
|
81 |
}
|