com.hgsoft.example.hello.web.HelloServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.hgsoft.example.hello.web.HelloServlet.java

Source

/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hgsoft.example.hello.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hgsoft.example.api.HelloWorldService;
import com.hgsoft.example.util.JNDIHelper;

public class HelloServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("doPost");
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        //      String[] beanNames = context.getBeanDefinitionNames();
        //      if(beanNames != null)
        //      {
        //         for(String beanName:beanNames)
        //         {
        //            System.out.println("bean name >> "+beanName);
        //         }
        //      }
        //      HelloWorldBusiness helloWorldBusiness = context.getBean("helloWorldBusiness",HelloWorldBusiness.class);
        //      helloWorldBusiness.hello();

        //       BlueprintContainer container = (BlueprintContainer) getServletContext().getAttribute(BlueprintContextListener.CONTAINER_ATTRIBUTE);
        //
        //       System.out.println("BlueprintContainer "+container);
        //       
        //       Set<String> componentIds =  container.getComponentIds();
        //       if(componentIds != null)
        //      {
        //         for(String componentId:componentIds)
        //         {
        //            System.out.println("componentId >> "+componentId);
        //         }
        //      }
        //jndi?
        HelloWorldService helloWorldService = (HelloWorldService) JNDIHelper.getHelloWorldService();
        helloWorldService.hello();

        EntityManagerFactory emf = (EntityManagerFactory) context.getBean("entityManagerFactory");
        Metamodel model = emf.getMetamodel();

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is the result of test: ");
        if (null != model) {
            if (null != model.getEntities()) {
                for (EntityType type : model.getEntities()) {
                    out.print(type.getName());
                }
            }

        }
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();

    }

}