/*
* 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 GET
and POST
* 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("");
out.println("");
out.println("
filebase: "); out.print(base); out.print("
\n"); out.print(""); out.print(PARAM_NAME); out.print(": "); out.print(getJNDIParameter(PARAM_NAME)); out.print("
\n"); out.print(""); out.print("Request URL: "); out.print(request.getRequestURL()); out.print("
\n"); // Ende des veraenderlichen Inhalts out.println(""); out.println(""); } } 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; } } //GET
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 POST
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";
}//