DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Software Development: Best Practices and Methods
  • Getting Started With Istio in AWS EKS for Multicluster Setup
  • Introduction to API Gateway in Microservices Architecture
  • Introduction to Domain-Driven Design

Trending

  • Software Development: Best Practices and Methods
  • Getting Started With Istio in AWS EKS for Multicluster Setup
  • Introduction to API Gateway in Microservices Architecture
  • Introduction to Domain-Driven Design
  1. DZone
  2. Coding
  3. Java
  4. Simple Java Servlet

Simple Java Servlet

Snippets Manager user avatar by
Snippets Manager
·
Feb. 10, 07 · Code Snippet
Like (1)
Save
Tweet
Share
1.46K Views

Join the DZone community and get the full member experience.

Join For Free
// Simple Servlet Skeleton


package net.tvs.servlet;

import java.io.IOException;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyServlet extends HttpServlet
{
  private static Log _logger = LogFactory.getLog( MyServlet.class );

  public void init(ServletConfig config) throws ServletException
  {
    super.init( config );
    try
      {
        // Initialization
      }
    catch ( Exception e )
      {
        _logger.fatal( e.getMessage() );
      }
    _logger.info( "MyServlet initialized" );
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
  {
    processRequest( request, response );
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
  {
    processRequest( request, response );
  }

  protected void processRequest(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType( "text/html;charset=UTF-8" );
    String param = request.getParameter( "param" );
    _logger.debug( "Received param: " + param );

    // Implementation...

    PrintWriter out = response.getWriter();
    response.setContentType( "text/xml" );
    response.setHeader( "Cache-Control", "no-cache" );

    out.println( "" );
    out.println( "" + param + "" );
    out.println( "" );
    out.close();
  }

}
Java (programming language)

Opinions expressed by DZone contributors are their own.

Trending

  • Software Development: Best Practices and Methods
  • Getting Started With Istio in AWS EKS for Multicluster Setup
  • Introduction to API Gateway in Microservices Architecture
  • Introduction to Domain-Driven Design

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: