SOLUTIONS/ANSWERS
1) True/ False
a) False b) True c) True
2) A servlet is a Java class and therefore needs to be executed in a Java VM by a
service we call a Servlet engine. Servlets dynamically extend the functionality
of a web server and basically developed for the server side applications. Servlets
have the following advantages over other common server extension
mechanisms:
service we call a Servlet engine. Servlets dynamically extend the functionality
of a web server and basically developed for the server side applications. Servlets
have the following advantages over other common server extension
mechanisms:
• They are faster than other server extensions like, CGI scripts because they
use a different process model.
• They use a standard API that is supported by many web servers.
• It executes within the address space of a web server.
• Since servlets are written in Java, servlets are portable between servers and
operating systems. They have all of the advantages of the Java language,
including ease of development and platform independence.
• It does not require creation of a separate process for each client request
use a different process model.
• They use a standard API that is supported by many web servers.
• It executes within the address space of a web server.
• Since servlets are written in Java, servlets are portable between servers and
operating systems. They have all of the advantages of the Java language,
including ease of development and platform independence.
• It does not require creation of a separate process for each client request
3) Code to display “Welcome to Fifth semester of MCA”
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>5th Semester MCA </TITLE></HEAD>");
out.println("<BODY>");
out.println("<B>Welcome to Fifth Semester of MCA</B>");
out.println("</BODY></HTML>");
}
}
out.println("<BODY>");
out.println("<B>Welcome to Fifth Semester of MCA</B>");
out.println("</BODY></HTML>");
}
}
4) Servlets are the Java classes which are created when needed and destroyed
when not needed. Since servlets run within a Servlet Container, creation and
destruction of servlets is the duty of Servlet Container. There are three principal
stages in the life of a Java Servlet Life Cycle, namely:
when not needed. Since servlets run within a Servlet Container, creation and
destruction of servlets is the duty of Servlet Container. There are three principal
stages in the life of a Java Servlet Life Cycle, namely:
i) Servlet Initialisation: In this first stage, the servlet's constructor is called
together with the servlet method init( ) - this is called automatically once
during the servlet's execution life cycle and can be used to place any one-off
initialisation such as opening a connection to a database.
together with the servlet method init( ) - this is called automatically once
during the servlet's execution life cycle and can be used to place any one-off
initialisation such as opening a connection to a database.
ii) Servlet Execution: Once your servlet is initialized and its init() method
called, any request that the Servlet Container receives will be forwarded to
your Servlet's service() method. HttpServlet class breaks this service() method
into more useful doGet(), doPost(), doDelete(), doOptions(), doPut() and
doTrace() methods depending on the type of HTTP request it receives. So in
order to generate response, the doGet() or doPost() method should be
overridden as per the requirement.
called, any request that the Servlet Container receives will be forwarded to
your Servlet's service() method. HttpServlet class breaks this service() method
into more useful doGet(), doPost(), doDelete(), doOptions(), doPut() and
doTrace() methods depending on the type of HTTP request it receives. So in
order to generate response, the doGet() or doPost() method should be
overridden as per the requirement.
When a servlet request is made to the Servlet engine, the Servlet engine
receives all the request parameters (such as the IP address of client), user
information and user data and constructs a Servlet request object, which
encapsulates all this information
receives all the request parameters (such as the IP address of client), user
information and user data and constructs a Servlet request object, which
encapsulates all this information
iii) Servlet Destruction: When the application is stopped or Servlet Container
shuts down, Servlet’s destroy() method will be called to clean up any
resources allocated during initialisation and to shutdown gracefully. Hence, it
acts as a place to deallocate resources such as an open file or open database
connection
shuts down, Servlet’s destroy() method will be called to clean up any
resources allocated during initialisation and to shutdown gracefully. Hence, it
acts as a place to deallocate resources such as an open file or open database
connection
No comments:
Post a Comment