1.Explain Servlet life cycle in java.
- Servlet engine controls the life cycle of servlet
- Servlet life cycle is described by three life cycle methods under 5 life cycle phases
- life cycle methods are
- init(ServletConfig obj)
- service(servletRequest, servletResponse)
- When ever somethings happens in the life of servlet. servlet engine calls these methods on the servlet instance and hence the name.
Read more here: Servlet Life Cycle
2.What is ServletConfig ?
- Request parameters are used by the servlet to receive data from client to process the request.
- Some specific data must be supplied to the servlet at the time of initialization of the servlet and this data is specific to the client.But data is not supplied by the client via request parameters
- Use initialization parameters to supply the data to the servlet at the time of initialization of the servlet. initialization parameters are set in deployment descriptor (Web.xml).
- Servlet can access these parameters during the run time
- Request parameters change form Request to request
- Initialization parameters change from servlet to servlet.
Read more at : ServletConfig
3. What is ServletContext?
- We can deploy multiple web applications in a servlet container.
- Each web application contains its own resources in a separate environment. This environment called ad Web Application context or ServletContext.
- The resources belongs to the one web application context are not available to the other web application context.
- A Servlet context contains zero or more number of servlets. For every servlet object the container creates separate ServletConfig object.
4. Can we define a Constructor in servlet?
- Yes we can define a constructor in servlet but we can not call the constructor explicitly because servlet container will create the object of servlet so it will be called by the servlet container.
5. Can we call destroy() method inside init() method of a servlet. If yes what will happen?
- Yes we can call destroy() method inside a init() method.
- Actually container will call destroy() method if we call that method explicitly nothing will happen.
- If we override destroy() method and called from init(). method will be called and code will be executed.
6.What are the difference between GenericServlet and HttpServlet?
|Abstract Class||Abstract Class|
|Protocol Independent||Http Protocol Dependent|
|Subclass of Servlet||Subclass of GenericServlet|
|public void service(ServletRequest req,ServletResponse res ).||Supports public void service() and protected void service(), doGet(),doPost(),doPut(),doDelete(),doHead(),doTrace(),doOptions()etc.|
7.What are the differences between doGet() and doPost()?
|protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException||Protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException|
|handles client's GET request||handles client's POST request|
|Request parameters appended to the URL and sent along with header information||Request parameters are submitted via form.|
|Request Parameters are not encrypted||Request Parameters are encrypted|
|Maximum size of data that can be sent using doget() method is 240 bytes||There is no maximum size for data.|
8. Can we call a servlet from another servlet?
- Yes. We can call a servlet from another servlet this is known as inter servlet communication.
- By using RequestDispatcher object we can do this.
- RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");
- rd.forward(req, res);
9. What are the differences between forward() and sendRedirect() methods?
|Used to forward the Request to resources available in within the server||Used to redirect the Request to other resources or domains|
10.How can you get the information about one servlet context in another servlet?
- By using setAttribut() method we can set the data in one servlet and get in another
- Context.setAttribute (“name”,” value”)
- Context.getAttribute (“name”)
11.Explain Servlet architecture?
- Frequently asking java interview question in servlets you may get questions like
- explain the architecture of java servlet?
- explain the architecture of java servlet with diagram?
- define the servlet architecture?
- servlet architecture overview?
- Servlets read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
- Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
Read More : servlet architecture with diagram
The Servlet architecture in Java is a Java-based framework for building web applications. It is a specification defined by the Java Community Process (JCP) and is implemented by various web containers such as Apache Tomcat, Jetty, and GlassFish.
The Servlet architecture consists of several components, including:
Servlets: These are the core components of the Servlet architecture. They are Java classes that handle HTTP requests and responses. Servlets are executed by a web container, which is responsible for managing their lifecycle, threading, and security.
Web Containers: Also known as servlet engines, web containers are responsible for managing the lifecycle of servlets, providing services such as request handling, thread management, and security.
JSP (JavaServer Pages): These are Java-based pages that are used to create dynamic web content. JSPs are compiled into servlets by the web container and executed by the servlet engine.
JSP Tag Libraries: These are collections of custom tags that can be used in JSP pages to simplify the creation of dynamic web content.
JavaBeans: These are Java classes that encapsulate business logic and data. They can be used in JSP pages to create dynamic web content.
Filters: These are Java classes that can intercept and modify the requests and responses sent to and from servlets. They can be used to implement security, logging, and other common functionality.
Listeners: These are Java classes that are notified of certain events that occur within the web container, such as a servlet being initialized or a session being created.
In summary, the Servlet architecture provides a powerful and flexible framework for building web applications in Java, by providing a set of components and services to handle HTTP requests and responses, providing a powerful and flexible way to handle web content, and providing a set of components to handle security, logging, and other common functionality.
Well done sir!!This is excellent platform for all java learners,keep it up.ReplyDelete
Wah janab mil hi gaye akhir...Delete