Welcome to Kode Java

Kode Java website provides beginners to Java programming some examples to use the Java API (Application Programming Interface) to develop applications. Learning from some examples will hopefully decrease the time required to learn Java.

In this website you will find a lot of examples which grouped by the Java API package. You can easily find a solution to your problem. To help you learn more we have also setup a discussion forums where everyone can share their Java knowledge.

Enjoy your study, come and visit the site regularly to find more and more examples of Java code.

--
I Wayan Saryada
Kode Java Webmaster

Java programmers recommend using Java Hosting for your scripts and JSP based websites to reach the full potential of your site and not experience any errors.

How do I create a HelloWorld Servlet?

Servlet is a Java solution for creating a dynamic web application, it can be compared with the old CGI technology. Using Servlet we can create a web application that can display information from database, receive information from a web form to be stored on the application database.

This example show the very basic of servlet, it returns a hello world html document for the browser. At the very minimum a servlet will have a doPost() and doGet() method which handles the HTTP POST and GET request.

package org.kodejava.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  
	public HelloWorld() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		
		PrintWriter writer = response.getWriter();
		writer.println("<html>");
		writer.println("<head><title>Hello World Servlet</title></head>");
		writer.println("<body>Hello World! How are you doing?</body>");
		writer.println("</html>");
		writer.close();
	}   	  	    
}

For a servlet to works on a servlet container such as Apache Tomcat we need to add or register the servlet in the application's web.xml file. This configuration tells the container about our servlet class and a url that maps a request to the servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>kodejava-example</display-name>
	<servlet>
		<description></description>
		<display-name>HelloWorld</display-name>
		<servlet-name>HelloWorld</servlet-name>
                <servlet-class>
                        org.kodejava.example.servlet.HelloWorld
                </servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/HelloWorld</url-pattern>
	</servlet-mapping>
</web-app>

 

When the servlet is deployed to the container we can access it from a url in a form of http://localhost:8080/app-name/HelloWorld

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats