[go: up one dir, main page]

0% found this document useful (0 votes)
8 views2 pages

servlet

Java Servlet

Uploaded by

lagishetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

servlet

Java Servlet

Uploaded by

lagishetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SERVLET

A Servlet is a Java program that runs on a web server and handles requests from web clients (like browsers). It is
used to build dynamic web applications.

Definition:

A Servlet is a Java class that extends the capabilities of servers that host applications accessed by means of
a request-response programming model, typically over HTTP.

Key Points:

 Servlets are part of the Java EE (Jakarta EE) platform.


 They run on a Java-enabled web server (like Apache Tomcat).
 They process requests, generate responses, and are commonly used to create web-based applications.
 They are more powerful and flexible than traditional CGI scripts.

Part Description
init() Called once when the servlet is first loaded.
doGet() Handles HTTP GET requests (like form submissions).
doPost() Handles HTTP POST requests.
destroy() Called before the servlet is unloaded.
HttpServlet Base class that provides HTTP-specific methods.

Simple Servlet Program :


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class WelcomeServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

// Set response content type


response.setContentType("text/html");

// Get the writer object


PrintWriter out = response.getWriter();

// Write the HTML content


out.println("<html>");
out.println("<head><title>Welcome</title></head>");
out.println("<body>");
out.println("<h2>Welcome to Servlet Programming!</h2>");
out.println("</body>");
out.println("</html>");
}
}
How to Run:

1. Save the file as WelcomeServlet.java.


2. Compile the servlet:

javac -classpath ‘C:\xampp\tomcat\libservlet-api.jar’ WelcomeServlet.java

Deploy to Tomcat:

 Place the .class file in:


webapps/YourApp/WEB-INF/classes/
 Add servlet mapping in web.xml:

File Name: web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>

1. Start Tomcat and access:


http://localhost:8080/welcome/WelcomeServlet/

You might also like