Monday, 1 November 2010

About HttpSession:

About HttpSession:
Assume
1)open browser
2)Request sent to servlet
3)Servlet calls request.getSession() server creates HttpSession object on the server side and gives response back and along with response it sends the session id
(you can think as reference to the HttpSession object created in the above step)
4)We request to another servlet or same servlet or another jsp, this time
browser sends the session id that it received in the above step to server when
you make request it happens in the background...
5)Now if you call request.getSession, server won't create new HttpSession
object, it identifies the existing session object by matching the session id
sent by browser..
6)When user calls logout button we write code session.invalidate() and
the httpsession object on the server will be destroyed
Now if you open new browser and make request... in the response
server creates another httpsession object and all the above steps
repeats....
What is the use of it ?
If you store any object in request scope when you send new request
the previous request object wont be there.
If you store any object in session scope you can get that object in
all the requests....
Real time comparison:
What if you don't have httpsession
1)You visited flipkart you went to the page where shoes were displayed
and you ordered 1 pair of shoes
2)Then you visited Cell phone related page and bought one
3)And then you tried to pay how would you feel if it shows only cell
This is what happens if you use request scope to store the items
purchased.The things you stored wont' be available in second request
as you know request object life is short... once the response came back
request object no more exists...
So here if you use HttpSession to store the objects you bought then
when you check out it displays all the items you bought
Imagine the bad things that can happen if you don't logout.
Imagine you logged in to your bank site and didn't logout then
if some other user came to the internet center that session object
might be still valid and that user can transfer money from your account.
That is the reason we have to logout...Banks generally write code to
expire the session if user doesn't use the site for 1 minute or 2 minutes
so that others may not take advantage if you don't log out

1)If you open a browser and close the browser generally it is the httpsession scope. But some times what happens is even if you open another browser in internet explorer some times both first and second browser takes the same httpsession object ... If you go to file->New session then only other httpsession object gets created... when you call request.getSession

But if you open one IE browser and Mozilla browser then you can clearly understand that.... HttpSession object gets created for each browser.... So when you open one IE browser and Mozilla browser 2 httpsession object gets printed...Best way to understand is by printing httpsession.... then you can see if same content is printed then you can think as same session



some more examples: HttpSession


Example program for HttpSession

Thursday, 17 June 2010

Displaying Date

Steps:

Maintain Folder Structure


GoTo your folder DisplayDate
under src folder
create java file DisplayingDate.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayingDate extends HttpServlet
{
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  PrintWriter pw=response.getWriter();
  Date today=new Date();
  pw.println("<html>"+<body><h1>Today Date is</h1>");
  pw.println("<b>"+today+"</b></body>"+"</html>");
 }
}

Compile this program.
javac DisplayingDate.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayingDate extends HttpServlet
{
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  PrintWriter pw=response.getWriter();
  Date today=new Date();
  pw.println("<html>"+"<body><h1>Today Date is</h1>");
  pw.println("<b>"+today+"</b></body>"+"</html>");
 }
}

Again compile:
javac DisplayingDate.java

We got errors that packages does not exist
package  javax.servlet.
package  javax.servlet.http.
Because we have not set the class path.
So set the class path
E:/DisplayDate/src>set classpath=C:\Program Files\Apache Software Foundation\Tomcat 8.0\lib\servlet-api.jar;
now compile the program
javac DisplayingDate.java

Now ok.
Go to WEB-INF folder
open web.xml file
<web-app>
 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>DisplayDate</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/DateDisplay</url-pattern>
</servlet-mapping>
</web-app>

-->Save the file
Deploy your project (folder) DisplayDate into Servler
Go to apache tomcat server folder




Start the server:


Go to any browser:
and type your local host with port no
http://localhost:8080/





type user name and password; (at the time of installing tomcat server we should set the username and password.)
here my username is: root
password is root.










Tuesday, 15 June 2010

How many ways to develop servlet programs

There are three ways to develop

1. Take Java class implementing javax.servlet.Servlet(Interface) and provide implementation to all the 5 methods of that interface

2. Take a java class extending from javax. servlet.GenericServlet(class) and provide implementation to public void service(-,-) method.

3. Take a Java class extending from javax.servlet.HttpServlet(class) and override one of the seven (7) doXXX(-,-) methods.

How many types of servlets?

There is a possibilities of developing 'n' types of servlets like httpServlet, ftpservlet, smtpServlet and etc,...for all these protocals are specific servlet classes.
GenericServlet is the super class contains common properties and logics. 

Monday, 14 June 2010

Web Application Folder Structure






Work flow of Servlet

Work flow of Servlet

The server checks if the servlet is requested for the first time.
If yes, web container does the following tasks:
loads the servlet class.
instantiates the servlet class.

calls the init() method passing the ServletConfig object
else
calls the service() method passing request and response objects

The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.

How web container handles the servlet request?
The web container is responsible to handle the request. Let's see how it handles the request.
maps the request with the servlet in the web.xml file.
creates request and response objects for this request
calls the service method on the thread

The public service method internally calls the protected service method
The protected service method calls the doGet method depending on the type of request.
The doGet method generates the response and it is passed to the client.
After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.

What is written inside the public service method?
The public service method converts the ServletRequest object into the HttpServletRequest type and ServletResponse object into the HttpServletResponse type. Then, calls the service method passing these objects. Let's see the internal code:

public void service(ServletRequest req, ServletResponse res) 
        throws ServletException, IOException 
    { 
        HttpServletRequest request; 
        HttpServletResponse response; 
        try 
        { 
            request = (HttpServletRequest)req; 
            response = (HttpServletResponse)res; 
        } 
        catch(ClassCastException e) 
        { 
            throw new ServletException("non-HTTP request or response"); 
        } 
        service(request, response); 
    } 


What is written inside the protected service method?
The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:

protected void service(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException 
    { 
        String method = req.getMethod(); 
        if(method.equals("GET")) 
        { 
            long lastModified = getLastModified(req); 
            if(lastModified == -1L) 
            { 
                doGet(req, resp); 
            }  
    .... 
    //rest of the code 
        } 
    } 



You may like the following posts:
Web Container

How to access your Web Application in other systems

I would like to make available to other to access my web application:

1. Once your web application is ready
2. Go to command prompt
3. type ifconfig (press enter)

Note down your Computer  IP Address. for example: IPv4 Address. . . . . . . . . . . : 192.168.0.112
Start your server to Run your Application. (Here I'm using Tomcat Server)





 So your local web portal link is:
Go to Any browser :
192.168.0.112:8080/ServRequest/
Syntax: your system ip address:port number/url-name