A session is used to maintain a conversational state in a web application with the client(browser). A session is identified by an unique ID which is called as a 'session id'. This is generated by the web container and it is managed by the container. A session can be created using a HttpSession object. We can observe the object using the HttpServletRequest interface.
HttpSession session = request.getSession(); (request is a HttpServletRequest)
Now a session is created and we can use it to set attributes for a particular user. If we say for example you are at amazon.com and you add a book to your shopping cart. Then you add another book. So in the shopping cart the cost must be the total of both books. It is the session who helped us to store those values as attributes and get the total later. We can do it in this way.
String value1 = ....//get the value of the first book
session.setAttribute("value1" , value1);
// in a separate page we can get the value
String value1 = (String) session.getAttribute("value1");
//then cast it to Integer and do the calculation with the second books value
using this way we can use many attributes in a particular users session. later we can end the session say the user clicks the signout button using the invalidate() method in the session object.
session.invalidate();
then all the attributes we have used for a user may be garbage collected (eligible).
Monday, November 3, 2008
Thursday, April 3, 2008
Servlet init parameters
these parameters are used to store some dynamic information in web applications.
init parameters can be accessed through the servlet config object where every servlet gets a reference to that.init parameters can only be accessed after calling the init method for that servlet.because the servletconfig is only available after a servlet initialization is done.

the ServletConfig interface have a method for getting init parameters
this is how we do that
getServletConfig.getInitParameter("Admin-email") ;
init parameters can be accessed through the servlet config object where every servlet gets a reference to that.init parameters can only be accessed after calling the init method for that servlet.because the servletconfig is only available after a servlet initialization is done.
- how to access init parameters
the ServletConfig interface have a method for getting init parameters
this is how we do that
getServletConfig.getInitParameter("Admin-email") ;
Dealing with the response
this is a sample class written by me to show how to deal with the responses.
package connection;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//setting the content type
response.setContentType("text/html");
//response.setContentType("application/jar");
//we give the response page's content via this method
//whether it is a html one or appication download or some thing else
//to write data in to a character stream
PrintWriter out = response.getWriter();
//to add a new header
response.addHeader("name","value");
//to replace an existing header
response.setHeader("name", "new value");
//to redirect to a new page some where else
response.sendRedirect("www.cmb.ac.lk");
//to direct another portion of the web application
RequestDispatcher rd=request.getRequestDispatcher("test.jsp");
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
package connection;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//setting the content type
response.setContentType("text/html");
//response.setContentType("application/jar");
//we give the response page's content via this method
//whether it is a html one or appication download or some thing else
//to write data in to a character stream
PrintWriter out = response.getWriter();
//to add a new header
response.addHeader("name","value");
//to replace an existing header
response.setHeader("name", "new value");
//to redirect to a new page some where else
response.sendRedirect("www.cmb.ac.lk");
//to direct another portion of the web application
RequestDispatcher rd=request.getRequestDispatcher("test.jsp");
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
Wednesday, March 19, 2008
Dealing with a request
sample servlet that have written using netbeans.i have use only the doPost method.normally in netbeans they forward the request to the processRequest() method in the servlet.so these things can be done in that method olso.no difference.
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//to get a parameter
String s= request.getParameter("what ever parameter sent from the previouse page");
//to get multiple parameters
String []s1= request.getParameterValues("para name");
//geting a header
String header= request.getHeader("name");
//get cookie info
Cookie []c= request.getCookies();
//to get session information
HttpSession session= request.getSession();
//to get clients port
getRemotePort();
}
//have to implement a method to get clients port
private void getRemotePort() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//to get a parameter
String s= request.getParameter("what ever parameter sent from the previouse page");
//to get multiple parameters
String []s1= request.getParameterValues("para name");
//geting a header
String header= request.getHeader("name");
//get cookie info
Cookie []c= request.getCookies();
//to get session information
HttpSession session= request.getSession();
//to get clients port
getRemotePort();
}
//have to implement a method to get clients port
private void getRemotePort() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Request & Response
now we have come to know what are requests and responses.these derive from two interfaces
the request comes from
now lets consider about the response.there are two interfaces which will be generating the response to the client
ServletResponse
the request comes from
- ServletRequest
- HttpServletRequest
- ServletRequest
- getAttribute(String)
- getLocalPort()
- getParameter()
- getParameterNames()
- HttpServletRequest
- getCookies()
- getHeader(String)
- getSession()
- getMethod()
now lets consider about the response.there are two interfaces which will be generating the response to the client
- ServletResponse
- HttpServletResponse
ServletResponse
- getWriter()
- setContentType()
- addHeader()
- addCookie()
- sendError()
Monday, March 17, 2008
Servlet basics
when we consider about the basics of a servlet we have to find the origin of it.
the Servlet interface comes here.which is javax.servlet.Servelt.we have some usefull method definitions there.some of them are
the Servlet interface comes here.which is javax.servlet.Servelt.we have some usefull method definitions there.some of them are
- service(ServletRequest, ServletResponse)
- init(ServletConfig)
- destroy()
- getInitParameter()
- getInitParameterNames()
- service(HttpServletRequest,HttpServletResponse)
- doGet(HttpServletRequest,HttpServletResponse)
- doPost(HttpServletRequest,HttpServletResponse
servlet config and servlet context
the ServletConfig object contain the deploy time information of a servlet and the ServletContext will be accessed through this object.and per servlet there will be only one ServletConfig object.the servletContext is used to access web application parameters.and per web application there will be one ServletContext.the doGet() and the doPost()
post is more secured and it has a body where the get does not have.we can use a limited number of parameters with the get.post cannot be bookmarked and is used to send data to process.get is used to get some resourse.
Sunday, March 16, 2008
The servlet life cycle
before learning about servlets lets discuss about the life cycle of the servlet.
when a client request something the request is directed to the container.the container makes two objects,which are
the thread comes in to action in one of the two ways mentioned below.
then the service() method redirect the request to a relevant method which may be doGet() or doPost() according to the request. we will learn about these methods in future.
after executing one of these methods the generated response is directed to the browser by the container.and the threads work is also over.
so the thread goes to dead state if it is a newly created one and it will go back to pool if it was taken form the thread pool.
now there is no need of the request and response objects.so they are eligible for the garbage collector to be collected.
and when the servlet is no longer needed the destroy() method of the servlet is called and the servlet moves to the dead state.this is also called only once.
**for every request we make a new thread comes to the action.**
when a client request something the request is directed to the container.the container makes two objects,which are
- request object
- response object
the thread comes in to action in one of the two ways mentioned below.
- a thread pool provides one of the threads it have
- a new thread is created(new Thread();)
then the service() method redirect the request to a relevant method which may be doGet() or doPost() according to the request. we will learn about these methods in future.
after executing one of these methods the generated response is directed to the browser by the container.and the threads work is also over.
so the thread goes to dead state if it is a newly created one and it will go back to pool if it was taken form the thread pool.
now there is no need of the request and response objects.so they are eligible for the garbage collector to be collected.
and when the servlet is no longer needed the destroy() method of the servlet is called and the servlet moves to the dead state.this is also called only once.
**for every request we make a new thread comes to the action.**
Thursday, March 13, 2008
The difference between a web container and a web server
web container is also called as the application server. as you know tomcat is a popular web container.is there a difference between a web server and a web container?
actually yes.
a web server usually serves html pages and images.they are static things unless they contain java script in them.
but when using jsp's and servlets we must use an application server to support them.because it is a software that supports multiple operations in a dynamic environment.
when we request something through our browser(eg:button click) it comes to the web server abd the server redirect that request to the container.
the container manages the whole other things.such as threading,security,communication and life cycle management and so on.
when the request is completed the container forwards the generated response to the web server.then the server displays through the browser.we will learn more in the Servlet life cycle.
actually yes.
a web server usually serves html pages and images.they are static things unless they contain java script in them.
but when using jsp's and servlets we must use an application server to support them.because it is a software that supports multiple operations in a dynamic environment.
when we request something through our browser(eg:button click) it comes to the web server abd the server redirect that request to the container.
the container manages the whole other things.such as threading,security,communication and life cycle management and so on.
when the request is completed the container forwards the generated response to the web server.then the server displays through the browser.we will learn more in the Servlet life cycle.
Subscribe to:
Posts (Atom)