Series: Servlets general

General discussion on Servlets given indepth like differences, WAR file creation etc.

Java Clear Concepts: 10 Servlets Advantages over ASP

I. Servlets Advantages over ASP are tabulated. Feature Servlet ASP Platform-independent Servelts are platform-independent being written in Java. Works on Windows, a Microsoft product, only. Java Benefits Servlets are written in Java and can take advantage of OOPs, robustness and most important garbage collection in code development. Written in VBScript (or JScript), a Microsoft product, …

Java Clear Concepts: 10 Servlets Advantages over ASP Read More »

What is SingleThreadModel in Servlets?

1. What is SingleThreadModel? It is an interface from javax.servlet package used with Servlets. It is marker interface having no methods. Few servlets may require this to implement. 2. Are Servlets multithreaded? Yes, Servlets are multithreaded being Java supports multithreading. 3. Explain how Servlets are multithreaded? First time when a request comes to a Web …

What is SingleThreadModel in Servlets? Read More »

What is ServletContext interface?

ServletConfig interface is specific to a particular Servlet. It cannot read other Servlet <init-param> data because <init-param> is written within <servlet> tag. What is ServletContext? On the contrary, ServletContext is global. The ServletContext object can be used by all the Servlets under execution in an application. Application comprises of many programs bundled together to achieve …

What is ServletContext interface? Read More »

Servlet vs GenericServlet vs HttpServlet

What is Servlet vs GenericServlet vs HttpServlet? Servlets are platform-independent server-side components, being written in Java. Before going for differences, first let us see how the three Servlet, GenericServlet, HttpServlet are related, their signatures and also at the end similarities. See how to write a Servlet. public class Validation extends HttpServlet To write a servlet, …

Servlet vs GenericServlet vs HttpServlet Read More »

ServletRequest vs HttpServletRequest

When the client clicks the submit button and sends a request to the server to invoke some Servlet (by giving the alias name of Servlet), the Servlet container loads the servlet and calls the service() method. The container also creates objects of ServeltRequest and ServletResponse and passes them to service() method. The ServletRequest receives the …

ServletRequest vs HttpServletRequest Read More »

ServletResponse vs HttpServletResponse

When the client sends a request to the Web server, the server loads the concerned Servlet and executes the service() method. In the process of execution, the container creates objects of ServletRequest and ServletResponse and passes them to the service() method. This is discussed in First Example – Login Screen Validation. ServletResponse vs HttpServletResponse Observe, …

ServletResponse vs HttpServletResponse Read More »

HttpServletRequest vs HttpServletResponse

Both HttpServletRequest and HttpServletResponse are inevitable in each Servlet, the Programmer write. Both exist in Servlet code as parameters to service() method. Even a Novice should be acquainted of these two interfaces. List of Differences HttpServletRequest vs HttpServletResponse Both are interfaces from javax.servlet.http package. They are derived from ServletRequest and ServletResponse interfaces. Both objects are …

HttpServletRequest vs HttpServletResponse Read More »

ServletConfig vs ServletContext

After knowing fully of ServletConfig and ServletContext with examples, explanation and screen outputs, it is time to compare them for still better understanding. ServletConfig vs ServletContext: Following table summarizes the differences between the two. S.No. ServletConfig ServletContext 1 Defined in javax.servlet package. Defined in the same javax.servlet package. 2 Values are placed in web.xml file. …

ServletConfig vs ServletContext Read More »

init-param vs context-param Servlets

Before going into init-param vs context-param, let us know the meaning and purpose of init-param and context-param in Servlet coding. 1. What is <init-param>? portnumber 8888 We have seen the above code in Servlet web xml init param Example using ServletConfig where the "portnumber" and "8888" are accessible for the particular servlet "ReadInitParamValues". Other servlets …

init-param vs context-param Servlets Read More »

RequestDispatcher from ServletRequest vs ServletContext

We have seen two programs with include() and forward() methods of RequestDispatcher. In both the programs, getRequestDispatcher(String path) of ServletRequest is used to obtain an object of RequestDispatcher. Infact, RequestDispatcher object can be obtained in another way also – using javax.servlet.ServletContext. The same getRequestDispatcher(String path) method exist in ServletContext also. That is, the same method …

RequestDispatcher from ServletRequest vs ServletContext Read More »

Can we have Servlet Constructor?

Yes, you can have as per rule. It is executed even before init(). Then why generally we avoid constructor in Servlets. First let us write one simple example and see it is true or not; then we will go into details. Example on Servlet Constructor import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.*; public class ConstructorServlet extends …

Can we have Servlet Constructor? Read More »

Auto Page Refresh Servlets with setHeader()

What is Page Refresh Servlets? Sometimes, user requires latest information on Cricket score or latest Share prices etc. where he is required to refresh the page, say by typing F5 key. To make user’s job easier, Servlet comes with setHeader("refresh", "5") method. With this method, the same Servlet is called for every 5 seconds once …

Auto Page Refresh Servlets with setHeader() Read More »

setContentType() Method Example

We know when a Servlet is loaded, the Servlet container creates an object of Servlet, calls service() method, creates an object of HttpServletRequest and also an object of HttpServletResponse and passes these objects to service() method. The job of HttpServletResponse (referred as response object in this discussion) is to pass data required by the client …

setContentType() Method Example Read More »

Web Status codes List of Common Problems

Before going through, it is advised to read What is Status code in HTTP Servlets? and Classification of Web Status Codes in HTTP. Following table gives you the list of Web status codes. Only commonly occurring are given. Status code Value Description 100 Continue 101 Switching Protocols 102 Processing 200 OK 201 Created 202 Accepted …

Web Status codes List of Common Problems Read More »

What is 404 Not Found Error in Servlets? How to Fix it?

What is 404 Not Found Errror? It is status code of the execution of a Servlet or Web application. More clear in the next explanation. User clicks over a hyperlink, some Servlet is called, executed and the response is sent to the user. Sometimes, the mouse click on the hyperlink calls such an attribute of …

What is 404 Not Found Error in Servlets? How to Fix it? Read More »