org.akhikhl.examples.gretty.hellogretty.ExampleServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.akhikhl.examples.gretty.hellogretty.ExampleServlet.java

Source

/*
 * Gretty
 *
 * Copyright (C) 2013-2014 Andrey Hihlovskiy and contributors.
 *
 * See the file "LICENSE" for copying and usage permission.
 * See the file "CONTRIBUTORS" for complete list of contributors.
 */
package org.akhikhl.examples.gretty.hellogretty;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

public class ExampleServlet extends HttpServlet {

    private static final long serialVersionUID = -6506276378398106663L;

    @Autowired
    DataSource dataSource;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            out.println("<h1>Hello, world!" + (dataSource != null ? dataSource.getConnection() : "null") + "</h1>");
            out.flush();
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } finally {
            out.close();
        }
    }

    @Override
    public void init() throws ServletException {
        super.init();
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
}