com.dm.platform.servlet.IndexServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.dm.platform.servlet.IndexServlet.java

Source

/*
 * 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 com.dm.platform.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "indexServlet", urlPatterns = "/platform/index")
public class IndexServlet extends HttpServlet {

    private ApplicationContext applicationContext;

    @Override
    public void init() throws ServletException {
        super.init(); //To change body of generated methods, choose Tools | Templates.

        //    applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
        //  testService = (TestService) applicationContext.getBean("testService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("utf-8");
        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>A Servlet</TITLE>");
            out.println("    <meta http-equiv=\"content-type\" " + "content=\"text/html; charset=utf-8\">");
            out.println(" </HEAD>");
            out.println("       <BODY>");
            out.println("             Hello AnnotationServlet.");
            out.println("     </BODY>");
            out.println("</HTML>");
            out.flush();
        }

    }

}