View RSS Feed


Java Server Pages

  1. JSP implicit Session object

    by , 11-20-2011 at 05:19 PM (My Java Tips)
    Session Object is another implicit JSP object that denotes the data associated with a specific session of a user. It is associated to Javax.servlet.http.httpsession.

    The connection between client and the server is provided by the session object. It maintains states when there are multiple page requests. If the user is navigating between multiple pages, and you need to store some data associated with that user, then session object helps you in this.

    Some useful session ...
  2. Handling Cookies in JSP

    by , 11-20-2011 at 05:04 PM (My Java Tips)
    Cookies are text files that are downloaded onto a visitor's hard drive to store the visitor's actions in order to better
    customize their following visits. We may handle cookies in JSP and I will show you how to do that.

    JSP provides an implicit response object which is used to add cookies. To add a cookie to the response, addCookie method is used.
    We may store useful information in cookies for later use and this method is perfect for our needs.
    Java Code:
    Java EE , JSP
  3. Checking if Session is new

    by , 11-19-2011 at 06:26 PM (My Java Tips)
    Sessions are used to keep track of the user in applications. Specifically talking about web applications (JSP), we may store objects in the session as well for using them later. Sometimes we wish to know if the session is new or not. This post will show how to do that.

    If a session is new, then isNew method of session object will return true otherwise false. When a server creates a session and client has not yet acknowledged the session, the server marks it with new. When the client ...
    Tags: isnew, sessions Add / Edit Tags
    Java EE , JSP
  4. Using pagging techniques

    by , 10-30-2011 at 05:59 PM (My Java Tips)
    When displaying long lists on the web, we use paging techniques. In this post, I will show how to do that.

    Normally we display 10 or 15 results at a time and the user can scroll through the list. If list is a long one, then it does not make an sense to loat it completely. Loading the complete list in session and then loop through the results is not a nice approach. An approach can be to use pagination tags which means fetching and displaying the results one page at a time. Pagination ...
    Java EE , JSF , JSP
  5. JSP Tags

    by , 05-25-2011 at 09:32 AM (My Java Tips)
    JSP tags are like HTML tags and they do not use <%. Instead they use < character.

    JSP tags can have a "start tag", a "tag body" and an "end tag". As you may have guessed, the start and end tag both use the tag name, enclosed in < and > characters. Just has HTNL tags, the ending JSP tag starts with a / character after the < character. The tag names have an embedded colon character : in them, the part before the colon describes the type ...
    Tags: jsp tags Add / Edit Tags
  6. Writing a file from JSP

    by , 05-24-2011 at 01:45 PM (My Java Tips)
    You may want to write a file from JSP page. For example, user fills a form and you want to persist the details on a text file on server. Now you need to know how to write text file from JSP.

    Remember, always give the path from root. In the given snippet, nameOfTextFile contains the file path. We will use FileOutputStream to write to the file.

    Java Code:
    <%@ page import="*"  %>
    String str = "form data goes here";
  7. Reading files from JSP

    by , 05-24-2011 at 01:44 PM (My Java Tips)
    Working in Servlets/JSP, you might have encountered The javax.servlet.ServletContext API. It provides methods to access resources in general.

    • getResource(String resource): will returns a URL for the resource in the web application.
    • getResourceAsStream(String resource): will returns an InpuStream for the resource in the web application
    • getResourcePaths(String path): will a directory-like listing of all the paths to resources within the web application.

    An example ...
  8. Using Java Beans in JSP

    by , 05-24-2011 at 01:42 PM (My Java Tips)
    JavaBeans are classes written in the Java programming language conforming to a particular convention.

    Consider the scenario: you are writing a JSP and you wish to write pure Java code in it. You may use scriptlets within JSP.

    Java Code:
    // Java code goes here
    Using scriptlets means that your JSP will contains business logic as well which is not a good design approach. Ideally you would like to keep business logic separately ...
  9. Fetching parameters from web.xml

    by , 05-24-2011 at 01:30 PM (My Java Tips)
    The file named web.xml is part of your web applications. You may use it for different purpose. Sometimes you wish to define some parameters for your web application in it. Obviously, you need to use this in your application. I will briefly tell you how to do this.

    At first, let me give you an example. We have a web application that needs to send its id and license code to some authentication server before doing some business operation. The id and license code are static and you may ...

    Updated 05-24-2011 at 01:34 PM by Java Tip

    JSP , Servlet , XML