/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
package de.uhilger.wbx.web;
|
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
import javax.naming.Context;
|
import javax.naming.InitialContext;
|
import javax.naming.NamingException;
|
import javax.servlet.ServletContext;
|
import javax.servlet.ServletException;
|
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
/**
|
*
|
*/
|
public class TestServlet extends HttpServlet {
|
|
private static final Logger logger = Logger.getLogger(TestServlet.class.getName());
|
|
public static final String JNDI_CTX_NAME = "java:comp/env";
|
public static final String PARAM_NAME = "wbxFileBase";
|
public static final String NOT_FOUND = " nicht gefunden";
|
public static final String NO_STRING = " ist kein String";
|
|
/**
|
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
|
* methods.
|
*
|
* @param request servlet request
|
* @param response servlet response
|
* @throws ServletException if a servlet-specific error occurs
|
* @throws IOException if an I/O error occurs
|
*/
|
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
throws ServletException, IOException {
|
response.setContentType("text/html;charset=UTF-8");
|
try (PrintWriter out = response.getWriter()) {
|
out.println("<!DOCTYPE html>");
|
out.println("<html>");
|
out.println("<head>");
|
out.println("<title>Servlet TestServlet</title>");
|
out.println("</head>");
|
out.println("<body>");
|
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
|
|
// hier kommt veraenderlicher Inhalt
|
ServletContext ctx = request.getSession().getServletContext();
|
Object o = ctx.getAttribute("filebase");
|
String base;
|
if(o instanceof String) {
|
base = o.toString();
|
} else {
|
base = "unbekannt";
|
}
|
out.print("<p>filebase: ");
|
out.print(base);
|
out.print("</p>\n");
|
|
out.print("<p>");
|
out.print(PARAM_NAME);
|
out.print(": ");
|
out.print(getJNDIParameter(PARAM_NAME));
|
out.print("</p>\n");
|
|
out.print("<p>");
|
out.print("Request URL: ");
|
out.print(request.getRequestURL());
|
out.print("</p>\n");
|
// Ende des veraenderlichen Inhalts
|
|
out.println("</body>");
|
out.println("</html>");
|
}
|
}
|
|
private String getJNDIParameter(String pname) {
|
try {
|
// unseren environment naming context ermitteln
|
Context initCtx = new InitialContext();
|
Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME);
|
|
// unseren Parameter lesen
|
Object o = envCtx.lookup(pname);
|
if(o instanceof String) {
|
return o.toString();
|
} else {
|
return NO_STRING;
|
}
|
} catch (NamingException ex) {
|
logger.log(Level.SEVERE, ex.getMessage());
|
return NOT_FOUND;
|
}
|
}
|
|
|
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
|
/**
|
* Handles the HTTP <code>GET</code> method.
|
*
|
* @param request servlet request
|
* @param response servlet response
|
* @throws ServletException if a servlet-specific error occurs
|
* @throws IOException if an I/O error occurs
|
*/
|
@Override
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
throws ServletException, IOException {
|
processRequest(request, response);
|
}
|
|
/**
|
* Handles the HTTP <code>POST</code> method.
|
*
|
* @param request servlet request
|
* @param response servlet response
|
* @throws ServletException if a servlet-specific error occurs
|
* @throws IOException if an I/O error occurs
|
*/
|
@Override
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
throws ServletException, IOException {
|
processRequest(request, response);
|
}
|
|
/**
|
* Returns a short description of the servlet.
|
*
|
* @return a String containing servlet description
|
*/
|
@Override
|
public String getServletInfo() {
|
return "Short description";
|
}// </editor-fold>
|
|
}
|