Posts

Showing posts with the label J2EE

J2EE: What are cookies?

Cookies are nothing more than a little piece of data (name/value String pairs) exchanged between client and server. Server sends the cookie to the client, and the client returns the cookie when the client makes another request. Cookies don't require user interaction, the cookie exchange is automatic. A session cookie lives only as long as session, once the client quits his browser, the cookie disappears. That's how "JSESSIONID" cookie works. But you can tell a cookie to stay alive even after the browser shuts down. How to get the cookies from HttpServletRequest? getCookies() method returns all the cookies present in request header. There is not getCookie() method. How to add a cookie to HttpServletResponse? addCookie() method adds the cookie to response header. How to create a cookie? You can do everything with cookie with the help of Cookie class. for creating a new cookie Cookie cookie = new Cookie("username", "Sreenath"); cookie....

J2EE: Useful methods of HttpSession

1. How to get the session id from HttpSession? you can retrieve session id by calling getId() on the HttpSession object. HttpSession session  = request.getSession(); sessoin.getId(); // returns session id. 2. How to know whether the session is new or old? To know whether the session is new or old, you can use isNew() on HttpSession object. It returns true, if the session is created during the current request, which means the user's browser has not yet received the session id. HttpSession session = request.getSession(); boolean isSessionNew = session.isNew(); 3. How to change the maximum inactive interval time for a session? setMaxInactiveInterval() enables you to change the inactivity window. This overrides the value you set in in web.xml for that specific session. you can make it shorter or longer based on your requirement.  Why do u want to change inactive interval? Lets consider some scenario, where you want to increase the inactivity interval for some ...

JSP Implicit Objects

request javax.servlet.http.HttpServletRequest response javax.servlet.http.HttpServletResponse session javax.servlet.http.HttpSession out javax.servlet.jsp.JspWriter application javax.servlet.ServletContext exception javax.servlet.jsp.JspException page java.lang.Object pageContext javax.servlet.jsp.PageContext config javax.servlet.ServletConfig

How to configure a JSP file in web.xml?

Image
In java web applications, you can directly access jsp files without any configuration. For example, http://localhost:8080/SimpleWebapp/Index.jsp, in this link we are requesting index.jsp directly without any config. But if you have placed your jsp files inside WEB-INF directory(see in the image - Welcome.jsp), you can't access directly. For this you need to configure the jsp in web.xml. Let's see how to do, 1. In web.xml, define both servlet & servlet-mapping tags. 2. Inside servlet( ) tag, add your jsp location inside the tag. "< jsp-file>/WEB-INF/Welcome.jsp< /jsp-file>" 3. Complete configuration is < servlet> < servlet-name> Welcome< /servlet-name> < jsp-file> /WEB-INF/Welcome.jsp< /jsp-file> < /servlet> < servlet-mapping> < servlet-name> Welcome< /servlet-name> < url-pattern> /Welcome< /url-pattern> < /servlet-mapping> The result is showed ...

In Detail : The Request Object In Servlets

     The request object encapsulates all information from the client request. The information from the clients is transmitted to the server in the form of HTTP headers and the message body of the request through the HTTP protocol. What are the HTTP Protocol Parameters ?       Request parameters for the servlet are nothing but strings sent by the client to a servlet container as part of its request. When the request is an HttpServletRequest object, and conditions set out are met, the container populates the parameters from the URI query string and POST-ed data. The parameters are stored as a set of name-value pairs. Multiple parameter values can exist for any given parameter name. The following methods of the ServletRequest interface are available to access parameters. getParameter() getParameterNames() getParameterValues() getParameterMap() The getParameterValues method returns an array of String objects containing all the parameter v...

Bored Of Simple Servlet Life Cycle, Here Is, In Detail , The Servlet Life Cycle

       A servlet goes through the well defined life cycle that defines how the sevlet is loaded , instantiated, and is initialized, handles requests from clients, and how the servlet is taken out of the service. This whole life cycle is expressed in the API by the init, service, and destroy methods of the javax.servlet.Servlet interface that all the servlets must implement directly or indirectly through the GenericServlet or HttpServlet abstract class. Loading and Instantiation :      Who is responsible for loading and instantiating servlets ? Well , the servlet container will take care about these things. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request. When the servlet engine is started, needed servlet classes must be located by servlet container. The servlet container loads the servlet classes using the normal Java class loading facili...

How to Implement Servlets

The Servlet Interface :       The Servlet interface is the central abstraction of the Java Servlet API. All servlets implement this interface either directly, or more commonly, by extending a class that implements the interface. The two classes in the Java Servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, Developers will extend HttpServlet to implement their servlets. Request Handling Methods :       The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet. The Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads. HTTP Specific Request Handling Methods :       The client can make the request to the web server using HTTP protocol. The HttpServlet abstract subclass adds addition...

Introduction To Servlets

What is a Servlet ?             A servlet is a java technology based Web component, managed by a container, that generates dynamic content. Servlets run on the servers, hence is the name Servlets. Like other Java technology-based components, servlets are platform - independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology - enabled Web server. Containers, sometimes called servlet engines, are Web server extensions that provide servlet functionality. Servlets interact with Web clients via a request / response paradigm implemented by the servlet container. What is a Servlet Container ?         The servlet container is a part of a Web server or application server that provides the network services over which requests and responses are sent, decodes MIME - based requests, and formats MIME - based responses. A servlet container also contains and manage...