Initial parameters in servlets

...

...

...

package my.servlet.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* http://localhost:8080/SERVLET/params
* http://localhost:8080/SERVLET/params?x=1
*/
public class InitialParamsServlet extends HttpServlet {

private static final long serialVersionUID = -7625395636649850641L;

@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/plain");

PrintWriter out = res.getWriter();
out.println("Init Parameters:");
Enumeration e = getInitParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(name + ": " + getInitParameter(name));
}

out.println("\nRequest info:");
out.println("Auth type: " + req.getAuthType());
out.println("Character encoding: " + req.getCharacterEncoding());
out.println("Content length: " + req.getContentLength());
out.println("Content type: " + req.getContentType());
out.println("Context path: " + req.getContextPath());
out.println("Local address: " + req.getLocalAddr());
out.println("Local name: " + req.getLocalName());
out.println("Local port: " + req.getLocalPort());
out.println("Method: " + req.getMethod());
out.println("Parameter: " + req.getParameter("greeting"));
out.println("Path info: " + req.getPathInfo());
out.println("Path translated: " + req.getPathTranslated());
out.println("Protocol: " + req.getProtocol());
out.println("Query string: " + req.getQueryString());
out.println("Remote address: " + req.getRemoteAddr());
out.println("Remote host: " + req.getRemoteHost());
out.println("Remote port: " + req.getRemotePort());
out.println("Remote user: " + req.getRemoteUser());
out.println("Requested sessionId: " + req.getRequestedSessionId());
out.println("Request uri: " + req.getRequestURI());
out.println("Scheme: " + req.getScheme());
out.println("Server name: " + req.getServerName());
out.println("Server port: " + req.getServerPort());
out.println("Servlet path: " + req.getServletPath());
out.println("Attribute: " + req.getAttribute("greeting"));

out.println("\nResponse info:");
out.println("Buffer size: " + res.getBufferSize());
out.println("Character encoding: " + res.getCharacterEncoding());
out.println("Content type: " + res.getContentType());

out.println("\nServer info:");
out.println("Context path: " + getServletContext().getContextPath());
out.println("Init parameter: " + getServletContext().getInitParameter("greeting"));
out.println("Major version: " + getServletContext().getMajorVersion());
out.println("Mime type: " + getServletContext().getMimeType(null));
out.println("Minor version: " + getServletContext().getMinorVersion());
out.println("Server info: " + getServletContext().getServerInfo());
out.println("Servlet context name: " + getServletContext().getServletContextName());

}

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SERVLET</display-name>
<servlet>
<description></description>
<display-name>InitParamsServlet</display-name>
<servlet-name>InitParamsServlet</servlet-name>
<servlet-class>my.servlet.test.InitialParamsServlet</servlet-class>
<init-param>
<param-name>greeting</param-name>
<param-value>Hello</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitParamsServlet</servlet-name>
<url-pattern>/params</url-pattern>
</servlet-mapping>
</web-app>

Deploy the application on Tomcat and see if it works:

http://localhost:8080/SERVLET/params