Session management in servlet

Session: The session is a time interval devoted to an activity. Session Tracking: Session Tracking or session management is a way of maintaining the state of the user. Need of Session Tracking: As we discussed in earlier tutorials we are using HTTP to complete request-response cycle. HTTP is a stateless protocol which means when a … Read more

Servlet Hello World Example using annotation

ServletAnnotationExample.java import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This servlet program is used to print “Hello World” on * client browser using annotations. */ @WebServlet(“/HelloWorld”) public class ServletAnnotationExample extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor public ServletAnnotationExample() { } protected void … Read more

Servlet context parameters and ServletContext interface

Context parameters: Context parameters refer to the initialization parameters for all servlets of an application. <context-param> attribute is used to define a context parameter. <context-param> attribute has two main sub attributes <param-name> and <param-value>. The <param-name> contains the name of the parameter and <param-value> contains the value of the parameter. ServletContext interface: The servletContext interface … Read more

Servlet Init parameters and ServletConfig interface

Init parameters: Init parameters refer to the initialization parameters of a servlet or filter. <init-param> attribute is used to define an init parameter. <init-param> attribute has two main sub attributes <param-name> and <param-value>. The <param-name> contains the name of the parameter and <param-value> contains the value of the parameter. ServletConfig interface: The ServletConfig interface is … Read more

Servlet sendRedirect

sendRedirect() is the method of the HttpServletResponse interface which is used to redirect response to another resource. Syntax: response.sendRedirect(relative url); Difference between sendRedirect and RequestDispatcher.              sendRedirect           RequestDispatcher Creates a new request from the client browser for the resource. Accept relative URLs so control can go inside or outside the server. The new URL … Read more

Servlet RequestDispatcher interface

RequestDispacher is an interface that provides the facility to forward a request to another resource or include the content of another resource. RequestDispacher provides a way to call another resource from a servlet. Another resource can be servlet, jsp, or html. Methods of RequestDispacher interface: 1. forward(ServletRequest request, ServletResponse response): This method forwards a request … Read more

load-on-startup in web.xml

load-on-startup: The load-on-startup is the sub-attribute of the servlet attribute in web.xml. It is used to control when the web server loads the servlet. As we discussed the servlet is loaded at the time of the first request. In this case, response time is increased for the first request. If load-on-startup is specified for a … Read more

Servlet welcome-file-list

welcome-file-list: The welcome-file-list attribute of the web.xml file is used to define the list of welcome files. Sample code of welcome-file-list attribute in web.xml: <web-app> //other attributes <welcome-file-list> <welcome-file>home.html</welcome-file> <welcome-file>welcome.html</welcome-file> </welcome-file-list> //other attributes </web-app>   How it works: The first web server looks for the welcome-file-list if it exists then it looks for the file … Read more

Java HttpServlet Class

HttpServlet class: HttpServlet class extends the GenericServlet. It is protocol-dependent. public abstract class HttpServlet extends GenericServlet HttpServlet class is in javax.servlet.http package (javax.servlet.http.HttpServlet). Methods of HttpServlet class: 1. service(ServletRequest req,ServletResponse res): Dispatches the requests to the protected service method. It converts the request and response object into HTTP type before dispatching the request. Syntax: public void service(ServletRequest req, ServletResponse res) … Read more