Posts

Showing posts with the label JavaEE

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 ...

JAVA EE: Some frequenty/important methods to remember in HttpServletRequest

setAttribute(): setting attribute value in HttpServetRequest is same as putting key/value pair in HashMap, except the key is always String and value can be any Object.  1. request.setAttribute("Products", "Products"); 2. request.setAttribute("Products",this.products); getRequestDispatcher(): if u want to forward the request to another jsp page or servlet in the same web application, you can use this method.   1. request.getRequestDispatcher("/WEB-INF/jsp/view/viewCart.jsp").forward(request, response); getSession() : How can u get the session in java we b application? you can ge t the session in the fo l lowing way. its re turntype is HttpSession.  request.getSession();   getSession() always retu rns a HttpSession. If the session is already exists , it retu rns that session else it creates a new session and returns it.  getSession(false) retu rns a Ht tpSession , if the session is a lready exists and returns null if not. ...