Explain about ServletConfig Interface
- ServletConfig a ServletConfig object is used to obtain configuration data when it is loaded.
- There can be multiple ServletConfig objects in a single web application.
- This object defines how a servlet is to be configured is passed to a servlet in its init method.
- Most servlet containers provide a way to configure a servlet at run-time (usually through flat file) and set up its initial parameters.
- The container, in turn, passes these parameters to the servlet via the ServetConfig.
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
<init-param>
<param-name>driverclassname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
<init-param>
<param-name>dburl</param-name>
<param-value>jdbc:odbc:MySQLODBC</param-value>
</init-param>
</servlet>
</web-app>
publicvoidinit()
{
ServletConfig config = getServletConfig();
String driverClassName = config.getInitParameter(“driverclassname”);
String dbURL = config.getInitParameter(“dburl”);
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(dbURL,username,password);
}
What is the importance of init() method in Servlet?
The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.
The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started.
When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet.
The init method definition looks like this:
public void init() throws ServletException
{
// Initialization code…
}
What are the life-cycle methods of JSP
Life-cycle methods of the JSP are:
- jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
- _jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann’t be overridden.
- jspDestroy(): The container calls this when its instance is about to destroyed.
The jspInit() and jspDestroy() methods can be overridden within a JSP page.
What is difference between custom JSP tags and beans
Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:
- the tag handler class that defines the tag’s behavior
- the tag library descriptor file that maps the XML element names to the tag implementations
- the JSP file that uses the tag library
When the first two components are done, you can use the tag by using taglib directive:
<%@ taglib uri=”xxx.tld” prefix=”…” %>
Then you are ready to use the tags you defined. Let’s say the tag prefix is test:
MyJSPTag or
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags to declare a bean and use to set value of the bean class and use to get value of the bean class.
<%=identifier.getclassField() %>
Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:
Custom tags can manipulate JSP content; beans cannot.Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans.Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.
What is JSP page
A text-based document containing static text and JSP elements that describes how to process a request to create a response. A JSP page is translated into and handles requests as a servlet.
A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.
Can a JSP page process HTML FORM data
Yes. However, unlike Servlet, you are not required to implement HTTP-protocol specific methods like doGet() or doPost() within your JSP page. You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression as.
Is there a way to reference the “this” variable within a JSP page? Yes, there is. Under JSP 1.0, the page implicit object is equivalent to “this”, and returns a reference to the Servlet generated by the JSP page.
Advantages of JSP over Servlet.
- Efficient: With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N simultaneous request to the same CGI program, then the code for the CGI program is loaded into memory N times. With servlets, however, there are N threads but only a single copy of the servlet class. Servlets also have more alternatives than do regular CGI programs for optimizations such as caching previous computations, keeping database connections open, and the like.
- Convenient: Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such utilities.
- Powerful: Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can’t). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations.
- Portable: Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server.
- Inexpensive: There are a number of free or very inexpensive Web servers available that are good for “personal” use or low-volume Web sites. However, with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. Nevertheless, once you have a Web server, no matter the cost of that server, adding servlet support to it (if it doesn’t come preconfigured to support servlets) is generally free or cheap.
What is ServletContext
- ServletContextInterface defines methods that a servlet can use to communicate to the Container.
- ServletContext Parameters are specified for entire application and are available for all servlets.
- ServletContext is also calledapplication object.
- ServletContext is used to obtain information about environment on which a servlet is running.
- There is one instance object of the ServletContext interface associated with each Web application deployed into a container.
- In cases where the container is distributed over many virtual machines, a Web application will have an instance of the ServletContext for each JVM.
- Servlet Context is a grouping under which related servlets run. They can share data, URL namespace, and other resources. There can be multiple contexts in a single servlet container.
How service() method will handle requests
The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Here is the signature of this method:
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
// code….
}
- The service () method is called by the container and service method invokes doGe, doPost, doPut, doDelete, etc. methods as appropriate.
- So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.
- The doGet() and doPost() are most frequently used methods with in each service request. Here are the signature of these two methods.
What is a servlet
- Servlet is server side component, a servlet is small pluggable extension to the server.
- Servlets are used to extend the functionality of the java-enabled server.
- Servlets will be loaded in theAddress space of web server.
- Servlet can be loaded in 3 ways
- When the web sever starts.
- You can set this in the configuration file.
- Through an administration interface.