0% found this document useful (0 votes)
7 views

Java Assignment 2

Lorem40

Uploaded by

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

Java Assignment 2

Lorem40

Uploaded by

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

Assignment: 2

Q.1) Explain working with apache tomcat server and steps to create a servlet in tomcat.
How Apache Tomcat Server Works:
1. Client Request (via Browser): A user sends an HTTP request to the server
(e.g., http://localhost:8080/MyApp/HelloServlet).
2. Tomcat Server Receives It: Tomcat listens for incoming HTTP requests on port
8080 (by default).
3. Servlet Container (Catalina): Tomcat's core component called Catalina identifies
the correct servlet to handle the request.
4. Servlet is Executed: The servlet is loaded, and its service() method processes
the request.
5. Response is Sent Back: The servlet generates an HTML (or JSON/XML)
response which is sent back to the browser.
Steps to Create a Servlet in Apache Tomcat:
1. Install Tomcat:
 Download from: https://tomcat.apache.org/
 Unzip the folder (e.g., apache-tomcat-9.x)
 Run startup.bat (on Windows) or startup.sh (on Linux/Mac) inside the bin/ folder

2. Create a Java Servlet Project (Using Eclipse or IntelliJ):


 Create a new Dynamic Web Project
 Set Apache Tomcat as the runtime server
 Create a new Servlet class like this:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>Hello from Servlet!</h1>");
}
}

3. Configure web.xml:
This file is located inside WebContent/WEB-INF/web.xml
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

4. Deploy the Servlet:


 Export the project as a .war file (WAR = Web Application Archive)
 Copy the .war file to tomcat/webapps/ folder

5. Start Tomcat and Access the Servlet:


 Start Tomcat using startup.bat
 Open your browser and visit:
http://localhost:8080/YourAppName/HelloServlet
Q.2) Explain various servlet request methods for form data and http header data.
How Apache Tomcat Server Works:
1. Client Request (via Browser): A user sends an HTTP request to the server
(e.g., http://localhost:8080/MyApp/HelloServlet).
2. Tomcat Server Receives It: Tomcat listens for incoming HTTP requests on port
8080 (by default).
3. Servlet Container (Catalina): Tomcat's core component called Catalina identifies
the correct servlet to handle the request.
4. Servlet is Executed: The servlet is loaded, and its service() method processes
the request.
5. Response is Sent Back: The servlet generates an HTML (or JSON/XML)
response which is sent back to the browser.
Steps to Create a Servlet in Apache Tomcat:
1. Install Tomcat:
 Download from: https://tomcat.apache.org/
 Unzip the folder (e.g., apache-tomcat-9.x)
 Run startup.bat (on Windows) or startup.sh (on Linux/Mac) inside the bin/ folder
2. Create a Java Servlet Project (Using Eclipse or IntelliJ):
 Create a new Dynamic Web Project
 Set Apache Tomcat as the runtime server
 Create a new Servlet class like this:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>Hello from Servlet!</h1>");
}
}

3. Configure web.xml:
This file is located inside WebContent/WEB-INF/web.xml.
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

4. Deploy the Servlet:


 Export the project as a .war file (WAR = Web Application Archive)
 Copy the .war file to tomcat/webapps/ folder

5. Start Tomcat and Access the Servlet:


 Start Tomcat using startup.bat
 Open your browser and visit:
http://localhost:8080/YourAppName/HelloServlet

You might also like