|
|
|
Web Components
•Servlets
•Java Server Pages (JSP)
•Tags and Tag Libraries
What’s a Servlet?
•Java’s answer to CGI programming
•Program runs on Web server and builds pages on the fly
•When would you use servlets?
–Data changes frequently e.g. weather-reports
–Page uses information from databases e.g. on-line stores
–Page is based on user-submitted data e.g search engines
–Data changes frequently e.g. weather-reports
–Page uses information from databases e.g. on-line stores
–Page is based on user-submitted data e.g search engines
Servlet Class Hierarchy
•javax.servlet.Servlet
–Defines methods that all servlets must implement
•init()
•service()
•destroy()
•javax.servlet.GenericServlet
–Defines a generic, protocol-independent servlet
•javax.servlet.http.HttpServlet
–To write an HTTP servlet for use on the Web
•doGet()
•doPost()
•javax.servlet.ServletConfig
–A servlet configuration object
–Passes information to a servlet during initialization
•Servlet.getServletConfig()
•javax.servlet.ServletContext
–To communicate with the servlet container
–Contained within the ServletConfig object
•ServletConfig.getServletContext()
•javax.servlet.ServletRequest
–Provides client request information to a servlet
•javax.servlet.ServletResponse
–Sending a response to the client
Basic Servlet Structure
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello World extends HttpServlet {
// Handle get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request – access incoming HTTP headers and HTML form data
// response - specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter(); //out - send content to browser
out.println("Hello World");
}
}
Servlet Life Cycle
•Loading and Instantiation
•Initialization
•Request Handling
•End of Service
Session Tracking
•Typical scenario – shopping cart in online store
•Necessary because HTTP is a "stateless" protocol
•Session Tracking API allows you to
–look up session object associated with current request
–create a new session object when necessary
–look up information associated with a session
–store information in a session
–discard completed or abandoned sessions
Session Tracking API - I
•Looking up a session object
–HttpSession session = request.getSession(true);
–Pass true to create a new session if one does not exist
•Associating information with session
–session.setAttribute(“user”,request.getParameter(“name”))
–Session attributes can be of any type
•Looking up session information
–String name = (String) session.getAttribute(“user”)
Session Tracking API - II
•getId : –the unique identifier generated for the session
•isNew : –true if the client (browser) has never seen the session
•getCreationTime : –time in milliseconds since session was made
•getLastAccessedTime : –time in milliseconds since the session was last sent from client
•getMaxInactiveInterval : –# of seconds session should go without access before being invalidated . –negative value indicates that session should never timeout
Javax.Servlet Interface Classes
Servlet Genericservlet
ServletRequest ServletInputStream
ServletResponce ServletOutputStream
ServletConfig ServletException
ServletContext UnavailableException
SingleThreadModel -
Javax.Servlet.Http Classes
HttpServletRequest Cookie
HttpServletResponse HttpServlet
HttpSession HttpSessionBindingEvent
HttpSessionContext HttpUtils
HttpSessionBindingListener -
Exceptions
ServletException
UnavailableException
|
| Author: Deepu 19 Mar 2008 | Member Level: Diamond Points : 2 |
Good work Aparanjitha.Keep going with your postings.You have done a great home work.good job keep it up.
|
| Author: Aparanjitha 19 Mar 2008 | Member Level: Gold Points : 3 |
thanks for ur suggestions and encouragement keep on reading my resoucres and give me some more advises . As you said before i want to gain knowledge in all fields and place resources that will be help ful to non cs . Thank you
|
| Author: Deepu 19 Mar 2008 | Member Level: Diamond Points : 3 |
Sure,I will go on reading your postings.If you go on accepting my advices i will give more advises for your development in the site.Keep going.But just keep in mind that your postings are going to be read by hundreds of people throughout the world.
|
| Author: Aparanjitha 19 Mar 2008 | Member Level: Gold Points : 2 |
thnx for your reply to my msg. i mine it that my postings will be read by hundreds of people . so im sure that i will place good resources
|