commit | author | age
|
c583a8
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.wbx.web; |
|
7 |
|
|
8 |
import java.io.IOException; |
|
9 |
import java.io.PrintWriter; |
|
10 |
import java.util.logging.Level; |
|
11 |
import java.util.logging.Logger; |
|
12 |
import javax.naming.Context; |
|
13 |
import javax.naming.InitialContext; |
|
14 |
import javax.naming.NamingException; |
|
15 |
import javax.servlet.ServletContext; |
|
16 |
import javax.servlet.ServletException; |
|
17 |
import javax.servlet.http.HttpServlet; |
|
18 |
import javax.servlet.http.HttpServletRequest; |
|
19 |
import javax.servlet.http.HttpServletResponse; |
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
*/ |
|
24 |
public class TestServlet extends HttpServlet { |
|
25 |
|
|
26 |
private static final Logger logger = Logger.getLogger(TestServlet.class.getName()); |
|
27 |
|
|
28 |
public static final String JNDI_CTX_NAME = "java:comp/env"; |
|
29 |
public static final String PARAM_NAME = "wbxFileBase"; |
|
30 |
public static final String NOT_FOUND = " nicht gefunden"; |
|
31 |
public static final String NO_STRING = " ist kein String"; |
|
32 |
|
|
33 |
/** |
|
34 |
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|
35 |
* methods. |
|
36 |
* |
|
37 |
* @param request servlet request |
|
38 |
* @param response servlet response |
|
39 |
* @throws ServletException if a servlet-specific error occurs |
|
40 |
* @throws IOException if an I/O error occurs |
|
41 |
*/ |
|
42 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|
43 |
throws ServletException, IOException { |
|
44 |
response.setContentType("text/html;charset=UTF-8"); |
|
45 |
try (PrintWriter out = response.getWriter()) { |
|
46 |
out.println("<!DOCTYPE html>"); |
|
47 |
out.println("<html>"); |
|
48 |
out.println("<head>"); |
|
49 |
out.println("<title>Servlet TestServlet</title>"); |
|
50 |
out.println("</head>"); |
|
51 |
out.println("<body>"); |
|
52 |
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>"); |
|
53 |
|
|
54 |
// hier kommt veraenderlicher Inhalt |
828ffa
|
55 |
ServletContext ctx = request.getSession().getServletContext(); |
c583a8
|
56 |
Object o = ctx.getAttribute("filebase"); |
U |
57 |
String base; |
|
58 |
if(o instanceof String) { |
|
59 |
base = o.toString(); |
|
60 |
} else { |
|
61 |
base = "unbekannt"; |
|
62 |
} |
|
63 |
out.print("<p>filebase: "); |
|
64 |
out.print(base); |
|
65 |
out.print("</p>\n"); |
|
66 |
|
|
67 |
out.print("<p>"); |
|
68 |
out.print(PARAM_NAME); |
|
69 |
out.print(": "); |
|
70 |
out.print(getJNDIParameter(PARAM_NAME)); |
|
71 |
out.print("</p>\n"); |
|
72 |
|
|
73 |
out.print("<p>"); |
|
74 |
out.print("Request URL: "); |
|
75 |
out.print(request.getRequestURL()); |
|
76 |
out.print("</p>\n"); |
|
77 |
// Ende des veraenderlichen Inhalts |
|
78 |
|
|
79 |
out.println("</body>"); |
|
80 |
out.println("</html>"); |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
private String getJNDIParameter(String pname) { |
|
85 |
try { |
|
86 |
// unseren environment naming context ermitteln |
|
87 |
Context initCtx = new InitialContext(); |
|
88 |
Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME); |
|
89 |
|
|
90 |
// unseren Parameter lesen |
|
91 |
Object o = envCtx.lookup(pname); |
|
92 |
if(o instanceof String) { |
|
93 |
return o.toString(); |
|
94 |
} else { |
|
95 |
return NO_STRING; |
|
96 |
} |
|
97 |
} catch (NamingException ex) { |
|
98 |
logger.log(Level.SEVERE, ex.getMessage()); |
|
99 |
return NOT_FOUND; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
|
|
104 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
|
105 |
/** |
|
106 |
* Handles the HTTP <code>GET</code> method. |
|
107 |
* |
|
108 |
* @param request servlet request |
|
109 |
* @param response servlet response |
|
110 |
* @throws ServletException if a servlet-specific error occurs |
|
111 |
* @throws IOException if an I/O error occurs |
|
112 |
*/ |
|
113 |
@Override |
|
114 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|
115 |
throws ServletException, IOException { |
|
116 |
processRequest(request, response); |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* Handles the HTTP <code>POST</code> method. |
|
121 |
* |
|
122 |
* @param request servlet request |
|
123 |
* @param response servlet response |
|
124 |
* @throws ServletException if a servlet-specific error occurs |
|
125 |
* @throws IOException if an I/O error occurs |
|
126 |
*/ |
|
127 |
@Override |
|
128 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|
129 |
throws ServletException, IOException { |
|
130 |
processRequest(request, response); |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* Returns a short description of the servlet. |
|
135 |
* |
|
136 |
* @return a String containing servlet description |
|
137 |
*/ |
|
138 |
@Override |
|
139 |
public String getServletInfo() { |
|
140 |
return "Short description"; |
|
141 |
}// </editor-fold> |
|
142 |
|
|
143 |
} |