26) What is the difference between the doGet and doPost methods?
doGet is called in response to an HTTP GET request. This happens when users click on a link, or enter a URL into the browser's address bar. It also happens with some HTML FORMs (those with METHOD="GET" specified in the FORM tag).
doPost is called in response to an HTTP POST request. This happens with some HTML FORMs (those with METHOD="POST" specified in the FORM tag).
Both methods are called by the default (superclass) implementation of service in the HttpServlet base class. You should override one or both to perform your servlet's actions. You probably shouldn't override service().
27) What is the difference between encodeRedirectUrl and encodeURL?
encodeURL and encodeRedirectURL are methods of the HttpResponse object. Both rewrite a raw URL to include session data if necessary. (If cookies are on, both are no-ops.)
encodeURL is for normal links inside your HTML pages.
encodeRedirectURL is for a link you're passing to response.sendRedirect(). It has slightly different syntax requirements too gory to get into here.
28) Can I use System.exit() in servlets?
Gack! No no no no no...
At best, you'll get a security exception. At worst, you'll make the servlet engine, or maybe the entire web server, quit. You don't really want to do that, huh? :-)
29) I am opening a single JDBC connection in my init() method. Do I need to synchronize on the Connection or the Statement object?
You shouldn't have to. If your JDBC driver supports multiple connections, then the various createStatement methods will give you a thread-safe, reentrant, independent Statement that should work OK, even if other requests/threads are also accessing other Statements on the same Connection.
Of course, crossing your fingers never hurts... Many early JDBC drivers were not re-entrant. The modern versions of JDBC drivers should work OK, but there are never any guarantees.
Using connection pooling will avoid the whole issue, plus will lead to improved performance. See this FAQ for more information.
30) How can I determine the name and version number of the servlet or JSP engine that I am using?
From within a servlet, you can invoke the ServletContext.getServerInfo() method as follows:
String thisServer= getServletConfig().getServletContext().getServerInfo();
If you are using JSP, you can use this expression:
<%= application.getServerInfo() %>
31) How can I get the absolute URL of a servlet/JSP page at runtime?
You can get all the necessary information to determine the URL from the request object. To reconstruct the absolute URL from the scheme, server name, port, URI and query string you can use the URL class from java.net. The following code fragment will determine your page's absolute URL:
String file = request.getRequestURI();
if (request.getQueryString() != null) {
file += '?' + request.getQueryString();
}
URL reconstructedURL = new URL(request.getScheme(),
request.getServerName(),
request.getServerPort(),
file);
out.println(URL.toString());
32) Why do GenericServlet and HttpServlet implement the Serializable interface?
GenericServlet and HttpServlet implement the Serializable interface so that servlet engines can "hybernate" the servlet state when the servlet is not in use and reinstance it when needed or to duplicate servlet instances for better load balancing. I don't know if or how current servlet engines do this, and it could have serious implications, like breaking references to objects gotten in the init() method without the programmer knowing it. Programmers should be aware of this pitfall and implement servlets which are stateless as possible, delegating data store to Session objects or to the ServletContext. In general stateless servlets are better because they scale much better and are cleaner code.
33) How does one choose between overriding the doGet(), doPost(), and service() methods?
The differences between the doGet() and doPost() methods are that they are called in the HttpServlet that your servlet extends by its service() method when it recieves a GET or a POST request from a HTTP protocol request.
A GET request is a request to get a resource from the server. This is the case of a browser requesting a web page. It is also possible to specify parameters in the request, but the length of the parameters on the whole is limited. This is the case of a form in a web page declared this way in html:
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.