Servlets

HTTP Session, HttpSession Methods in Java

Before reading this, it is advised to read the nature of HTTP protocol and what are session, session tracking and JSESSIONID. To maintain HTTP session in Servlets, the Java API comes with javax.servlet.http.HttpSession interface. The methods of this interface are useful to do with session management. session The HttpSession object is capable well to track …

HTTP Session, HttpSession Methods in Java Read More »

getScheme() Method Example

getScheme() method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest. With getScheme() method, the Servlet Programmer can know what protocol (like HTTP) is used by the client from HTML file to call the Servlet. Let us see what Java API says about this method. public java.lang.String getScheme(): Returns the name of the …

getScheme() Method Example Read More »

What is Session, Session Tracking, Session Management?

For easy understanding, the notes is given in Question/Answer format. 1. What is a Session in Servlets? The interactive time between client and server on a single connection is known as a session. Or The period of time between connection establishment and connection closing between client and server is known as a session. Or Session …

What is Session, Session Tracking, Session Management? Read More »

Stateless and Connectionless HTTP Protocol

About HTTP Protocol What is HTTP (HyperText Transfer Protocol) Protocol? A protocol sets standards for communication between systems, client to server or among servers. HTTP is specially meant to communicate between Client and Server using Web (or Internet). It was developed by W3C (World Wide Web Consortium) around in the year 1995. HTTP is an …

Stateless and Connectionless HTTP Protocol 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 »

sendRedirect() Method Example in Servlet

We have seen earlier, the usage of include(), forward and their 16 differences. Now let us see how to use sendRedirect() method. The sendRedirect(String URL) is defined in javax.servlet.http.HttpServletResponse interface. Let us see what Java API says about sendRedirect() method signature: void sendRedirect(String location) throws IOException: Sends a temporary redirect response to the client using …

sendRedirect() Method Example in Servlet Read More »

16 Differences include vs forward Decide when to use which?

The javax.servlet.RequestDispatcher interface comes with only two methods of include() and forward(). These methods are discussed very clearly with example code, illustrative figures and explanation in RequestDispatcher include Example and RequestDispatcher forward Example. It is advised to go through these two programs before learning the differences. RequestDispatcher is used whenever the Programmer would like dispatch …

16 Differences include vs forward Decide when to use which? Read More »

RequestDispatcher forward() Example

Communication between the Servlets is an important task to the Programmer. To achieve this, we studied reading private data of a servlet and reading global data by all servlets. Now let us see how to pass data between two servlets (one-to-one) and for this Servlet API comes with javax.servlet.RequestDispatcher interface. When to use RequestDispatcher interface? …

RequestDispatcher forward() Example Read More »

getServerPort() Method Example

getServerPort() method is defined in ServletRequest interface from javax.servlet package and inherited by HttpServletRequest. With getServerPort() method, the Servlet Programmer can know what port number is used by the client from HTML file to call the Servlet. Let us see what Java API says about this method. int getServerPort(): Returns the port number to which …

getServerPort() Method Example 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 »

ServletContext Example context-param

Before going ServletContext Example, see the difference between <init-param> and <context-param>, both written in web.xml file. 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 in the container cannot access …

ServletContext Example context-param Read More »

Read HTML Form Data in 3 Styles with Servlets

One of the activities of a Servlet Developer is to read HTML form data sent by the client by clicking over Submit button. The data sent by the client lands finally in ServletRequest/HttpServletRequest object passed as parameter to service() method. To retrieve the data from ServletRequest object, the ServletRequest interface comes with three methods mostly …

Read HTML Form Data in 3 Styles with Servlets Read More »

getParameterNames() Example Servlets

One of the aspects of Servlets is reading the HTML/Applet data sent by the client like login data etc. For this, three methods exist in ServletRequest interface – getParameter(String), getParameterNames() and getParameterValues(String). These methods are inherited by HttpServletRequest interface. Following are the method signatures as defined in javax.servlet.ServletRequest interface. public java.lang.String getParameter(java.lang.String name): Returns the …

getParameterNames() Example Servlets Read More »

Read HTML Filelds getParameterValues() Example

getParameterValues(): One of the aspects of Servlets is reading the HTML/Applet data sent by the client like login data etc. For this, three methods exist in ServletRequest interface – getParameter(String), getParameterNames() and getParameterValues(). These methods are inherited by HttpServletRequest interface. First let us see what Servlet API says about these methods. These are defined in …

Read HTML Filelds getParameterValues() Example Read More »

getRemoteAddr() vs getRemoteHost()

Both getRemoteAddr() vs getRemoteHost() are defined in ServletRequest interface and serves the Servlet Programmer to retrieve the client system particulars from where the request came. They differ slightly in the output. 1. getRemoteAddr() returns a string containing the IP address (in format like IPiv, 127.0.0.1) of the client. 2. getRemoteHost() returns a string containing the …

getRemoteAddr() vs getRemoteHost() Read More »