Servlets - Examples


Servlets are Java classes which administration HTTP asks for and actualize the javax.servlet.Servlet interface. Web application engineers ordinarily compose servlets that broaden javax.servlet.http.HttpServlet, a unique class that actualizes the Servlet interface and is uncommonly intended to deal with HTTP requests.

Sample Code

Following is the example source code structure of a servlet guide to indicate Hello World −

//Import required java libraries 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

//Extend HttpServlet class 

public class HelloWorld extends HttpServlet { 

private String message; 

public void init() throws ServletException { 

//Do required initialization 

message = "Hello World"; 

} 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set reaction content type 

response.setContentType("text/html"); 

//Actual rationale goes here. 

PrintWriter out = response.getWriter(); 

out.println("<h1>" + message + "</h1>"); 

} 

public void destroy() { 

//do nothing. 

} 

}

Compiling a Servlet

Let us make a record with name HelloWorld.java with the code appeared. Spot this document at C:\ServletDevel (in Windows) or at/usr/ServletDevel (in Unix). This way area must be added to CLASSPATH before continuing further.

Assuming your condition is setup appropriately, go in ServletDevel catalog and assemble HelloWorld.java as pursues −

$ javac HelloWorld.java 

If the servlet relies upon some other libraries, you need to incorporate those JAR records on your CLASSPATH too. I have included just servlet-api.jar JAR record since I'm not utilizing some other library in Hello World program.

This order line utilizes the implicit javac compiler that accompanies the Sun Microsystems Java Software Development Kit (JDK). For this direction to work legitimately, you need to incorporate the area of the Java SDK that you are utilizing in the PATH condition variable.

If everything goes fine, above arrangement would deliver HelloWorld.class document in a similar catalog. Next segment would clarify how an aggregated servlet would be conveyed in production.

Servlet Deployment

By default, a servlet application is situated at the way <Tomcat-installationdirectory>/webapps/ROOT and the class record would live in <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes.

If you have a completely qualified class name of com.myorg.MyServlet, at that point this servlet class must be situated in WEB-INF/classes/com/myorg/MyServlet.class.

For now, let us duplicate HelloWorld.class into <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes and make following passages in web.xml document situated in <Tomcat-establishment directory>/webapps/ROOT/WEB-INF/

<servlet> 

<servlet-name>HelloWorld</servlet-name> 

<servlet-class>HelloWorld</servlet-class> 

</servlet> 

<servlet-mapping> 

<servlet-name>HelloWorld</servlet-name> 

<url-pattern>/HelloWorld</url-pattern> 

</servlet-mapping>

Above sections to be made inside <web-app>...</web-app> labels accessible in web.xml record. There could be different passages in this table officially accessible, yet never mind.

You are nearly done, presently given us a chance to begin tomcat server utilizing <Tomcat-installationdirectory>\bin\startup.bat (on Windows) or <Tomcat-installationdirectory>/receptacle/startup.sh (on Linux/Solaris and so forth.) lastly type http://localhost:8080/HelloWorld in the program's location box. In the case of everything goes fine, you would get the accompanying result






© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3