Monday, July 24, 2017

Session

Managing Session in Servlets

We all know that HTTP is a stateless protocol. All requests and responses are independent. But sometimes you need to keep track of client's activity across multiple requests. For eg. When a User logs into your website, not matter on which web page he visits after logging in, his credentials will be with the server, until he logs out. So this is managed by creating a session.
Session Management is a mechanism used by the Web container to store session information for a particular user. There are four different techniques used by Servlet application for session management. They are as follows:
1.    Cookies
2.    Hidden form field
3.    URL Rewriting
4.    HttpSession

What is HttpSession?
HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Any servlet can have access to HttpSession object throughout the getSession() method of the HttpServletRequest object.





Using URL Rewriting for Session Management

If the client has disabled cookies in the browser then session management using cookie wont work. In that case URL Rewriting can be used as a backup. URL rewriting will always work.
In URL rewriting, a token(parameter) is added at the end of the URL. The token consist of name/value pair seperated by an equal(=) sign.




Tuesday, July 18, 2017

Arraylist in data from database

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication39;

/**
 *
 * @author prakash
 */
import java.sql.*;
import java.util.*;
public class JavaApplication39 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try{
            Class.forName("oracle.jdbc.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","oracle");
        Statement stm = con.createStatement();
        String s1="select * from student";
        ResultSet res = stm.executeQuery(s1);
        ArrayList<String> info= new ArrayList<String>();
        while(res.next())
        {
            info.add(res.getString(1)+res.getString(2)+res.getString(3)+res.getString(4));
        }
        for(String s2:info)
        {
            System.err.println(s2);
        }
       
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
     
    }
   
}