servlet

Sample Java Servlet

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 7th, 2019
0 733 1 minute read

In this example we are going to see how to create a simple Java Servlet. AsOracle states in it’s site:

Servlets are the Java platform technology of choice for extending and enhancing Web servers. Servlets provide a component-based, platform-independent method for building Web-based applications, without the performance limitations of CGI programs. And unlike proprietary server extension mechanisms (such as the Netscape Server API or Apache modules), servlets are server- and platform-independent. This leaves you free to select a “best of breed” strategy for your servers, platforms, and tools.
 
 

 
Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and crash protection.

Today servlets are a popular choice for building interactive Web applications. Third-party servlet containers are available for Apache Web Server, Microsoft IIS, and others. Servlet containers are usually a component of Web and application servers, such as BEA WebLogic Application Server, IBM WebSphere, Sun Java System Web Server, Sun Java System Application Server, and others.

You might want to check out the latest information on JavaServer Pages (JSP) technology.

package com.javacodegeeks.snippets.enterprise;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class SampleJavaServlet extends HttpServlet {private static final long serialVersionUID = -2128122335811219481L;public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {PrintWriter out = res.getWriter();out.println("<html>");out.println("<head>");out.println("<title>JCG Snippets</title>");out.println("</head>");out.println("<body>");out.println("Hello JCG Snippets @ " + new Date());out.println("</body>");out.println("</html>");out.close();}}

web.xml

Want to be a Servlets Master ?
Subscribe to our newsletter and download the Java ServletUltimateGuideright now!
In order to help you master programming with Java Servlets, we have compiled a kick-ass guide with all the major servlet API uses and showcases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

<?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"  version="2.5"><display-name>JCG Snippets Web Project</display-name><servlet><servlet-name>JCG Snippets Application</servlet-name><servlet-class>com.javacodegeeks.snippets.enterprise.SampleJavaServlet</servlet-class></servlet><servlet-mapping><servlet-name>JCG Snippets Application</servlet-name><url-pattern>/jcgservlet</url-pattern></servlet-mapping></web-app>

URL:

http://myhost:8080/jcgsnippets/jcgservlet

Output:

Hello JCG Snippets @ Wed Nov 16 18:58:27 EET 2011

This was a Sample Java Servlet.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 7th, 2019
0 733 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.