answersLogoWhite

0

Methods in the Servlet Context

Apart from setting and getting custom attributes used for our application, the context also contains various methods that we can use to retrieve specific information about the application and other aspects. They are:

• getAttributeNames() - Returns an Enumeration object containing the attribute names available within this servlet context.

• getContext(String uripath) - Returns a ServletContext object that corresponds to a specified URL on the server.

• getInitParameter(String name) - Returns a string containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

• getInitParameterNames() - Returns the names of the context's initialization parameters as an Enumeration of string objects, or an empty Enumeration if the context has no initialization parameters.

• getMajorVersion() - Returns the major version as an int of the Java Servlet API that this Servlet Container supports.

• getMimeType(java.lang.String file) - Returns the MIME type as a string of the specified file, or null if the MIME type is not known.

• getMinorVersion() - Returns the minor version as an int of the Servlet API that this Servlet Container supports.

• getNamedDispatcher(String name) Returns a RequestDispatcher object that acts as a wrapper for the named servlet.

• getRealPath(String path) - Returns a string containing the real path for a given virtual path.

• getRequestDispatcher(String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

• getResource(String path) - Returns a URL to the resource that is mapped to a specified path.

• getResourceAsStream(String path) - Returns the resource located at the named path as an InputStream object.

• getServerInfo() Returns the name and version as a String of the Servlet Container on which the servlet is running.

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Basic Math

How do JSP methods get translated to Servlet methods?

A JSP gets converted into a Servlet for execution and hence the methods in a JSP are similar to the ones in a Servlet.Scriptlets and Expressions will end up in the body of doGet or doPostDeclarations will end up as instance variablesjspInit() and jspDestroy() will get translated to Servlet's init() and destroy() methods


Why doesn't a servlet have a main method in it?

A servlet does not have a main method because it is designed to run within a servlet container, such as Apache Tomcat, which manages its lifecycle. Instead of a main method, servlets implement specific methods like doGet() and doPost() to handle HTTP requests and responses. The container invokes these methods in response to client requests, allowing the servlet to focus on processing web interactions rather than standalone execution.


If Servet Inteface is implement in userdefind servlet than in what method can you write business logic?

In a user-defined servlet that implements the Servlet interface, you can write business logic primarily in the doGet() and doPost() methods. These methods handle HTTP GET and POST requests, respectively, and are where you can process input, interact with databases, and generate responses. Depending on the application’s requirements, you might also use helper methods to organize your business logic more effectively.


Which method must to override in Servlet?

There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet


What is doPost in servlet?

doPost() is the method in the servlet that is used to service the Http requests submitted using the Post option. HTML forms can be submitted in two ways using the Get method and the Post method. You can specify the method while declaring the form itself. So you will have two methods in the servlet doGet() and doPost() each for one of them

Related Questions

How do JSP methods get translated to Servlet methods?

A JSP gets converted into a Servlet for execution and hence the methods in a JSP are similar to the ones in a Servlet.Scriptlets and Expressions will end up in the body of doGet or doPostDeclarations will end up as instance variablesjspInit() and jspDestroy() will get translated to Servlet's init() and destroy() methods


Servlet life cycle?

The javax.servlet.Servlet interface defines the three methods known as life-cycle method. public void init(ServletConfig config) throws ServletException public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy() First the servlet is constructed, then initialized wih the init() method. Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet. There are two more methods..... public abstract String getServletInfo() public abstract ServletConfig getServletConfig()


Why doesn't a servlet have a main method in it?

A servlet does not have a main method because it is designed to run within a servlet container, such as Apache Tomcat, which manages its lifecycle. Instead of a main method, servlets implement specific methods like doGet() and doPost() to handle HTTP requests and responses. The container invokes these methods in response to client requests, allowing the servlet to focus on processing web interactions rather than standalone execution.


If Servet Inteface is implement in userdefind servlet than in what method can you write business logic?

In a user-defined servlet that implements the Servlet interface, you can write business logic primarily in the doGet() and doPost() methods. These methods handle HTTP GET and POST requests, respectively, and are where you can process input, interact with databases, and generate responses. Depending on the application’s requirements, you might also use helper methods to organize your business logic more effectively.


Which method must to override in Servlet?

There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet


What is HttpServletRequest?

ServletRequest provides the functionalities to the classes to access the HTTP requests. The servlet container creates an HttpServletRequest object and an HttpServletResponse object, and passes them as arguments to the servlet's service methods such as doGet, doPost, etc.


Why servlet has no constructor?

Because it is not a regular Java Class that is executed by a JVM. It is a special Java class that is executed by the Web Container which initializes and loads the servlet and the service methods get executed everytime it receives a request.


How does Apache httpd work?

Tomcat is a servlet container, and the following is how a servlet container works The init, service, and destroy methods are the servlet's lifecycle methods. The init method is called once by the servlet container after the servlet class has been instantiated to indicate to the servlet that it being placed into service. The init method must complete successfully before the servlet can receive any requests. A servlet programmer can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, this method is normally left blank. The service method is then called by the servlet container to allow the servlet to respond to a request. The servlet container passes a javax.servlet.ServletRequest object and a javax.servlet.ServletResponse object. The ServletRequest object contains the client's HTTP request information and the ServletResponse encapsulates the servlet's response. These two objects enable you to write custom code that determines how the servlet services the client request. The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or when the servlet container needs some free memory. This method is called only after all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls destroy, it will not call the service method again on this servlet. The destroy method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, and threads) and make sure that any persistent state is synchronized with the servlet's current state in memory. For Better Picture, Visit below article full of images to clear the concept : http://shivasoft.in/blog/java/servlet/how-container-handles-the-servlet-request/


What are types of servlet?

Http servlet and Generic servlet


What is the difference between servletcontext and servletconfig?

ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container.ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.You can specify param-value pairs for ServletContext object in tags in web.xml file.The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.By Santanu


How can an IP address for a network device be assigned to a particular network?

Router(config)# hostname AdminAdmin(config)# enable secret cangetinAdmin(config)# interface fa0/1Admin(config-if)#ip address 192.168.1.22 255.255.255.248Router(config)# hostname AdminAdmin(config)# enable password cangetinAdmin(config)# interface fa0/1Admin(config-if)# ip address 192.168.1.22 255.255.255.248Router(config)# hostname AdminAdmin(config)# enable secret cangetinAdmin(config)# interface fa0/0Admin(config-if)# ip address 192.168.1.22 255.255.255.248Router(config)# hostname AdminAdmin(config)# enable password cangetinAdmin(config)# interface fa0/0Admin(config-if)# ip address 192.168.1.22 255.255.255.248


Generic Servlet and HTTP Servlet?

javax.servlet.GenericServletSignature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.SerializableGenericServlet defines a generic, protocol-independent servlet.GenericServlet gives a blueprint and makes writing servlet easier.GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.GenericServlet implements the log method, declared in the ServletContext interface.To write a generic servlet, it is sufficient to override the abstract service method.javax.servlet.http.HttpServletSignature: public abstract class HttpServlet extends GenericServlet implements java.io.SerializableHttpServlet defines a HTTP protocol specific servlet.HttpServlet gives a blueprint for Http servlet and makes writing them easier.HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.