Presentation Transcript
Slide 1:HTTPSERVLET CLASS METHODS DO NOT
OVERIDE
service()
getServletConfig()
getServletContext()
The three methods which stated above
should not be overridden as their default
implementation is more than enough to suffice.
Slide 2:SERVLET CONFIG CLASS
ServletConfig object is used to pass information to the Servlet during it's initialization.
Servlet can obtain information regarding initialization parameters and their values
using different methods of ServletConfig class.
Slide 3:SERVLETCONFIG METHODS
getInitParameter(String paramName) Returns value of the given
parameter. If value of parameter could not be found in web.xml file
then a null value is returned.
getInitParameterNames() Returns an Enumeration object containing all the names of initialization parameters provided for this Servlet.
getServletContext() Returns reference to the ServletContext object for this Servlet. It is similar to getServletContext() method provided by HttpServlet class.
getServletName() Returns name of the Servlet as provided in the
web.xml file or if none is provided then returns complete class path tothe Servlet.
Slide 4:A “HELLO WORLD” SERVLET
public class HelloServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("CIAO MONDO!");
}
}
Slide 5:A “HELLO WORLD” HTML SERVLET
protected void doGet(HttpSerrrvletRequest request,
HttpServletResponse response) throwsServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String docType=" \n“>;
out.prntln(docType);
out.pintln("\n" +" CIAO MONDO HTML \n" +"\n" +" CIAO MONDO \n" +"" +
"");
}
Slide 6:A “HELLO WORLD” SERVLET WITH INIT
public void init(ServletConfig config) throws
ServletException
{
// TODO Auto-generated method stub
super.init(config);
String sRipetizioni=config.getInitParameter("ripetizioni");
ripetizioni=Integer.parseInt(sRipetizioni);
}
Servlet Session Tracking :Servlet Session Tracking
Persistent information :Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Information about the session
A session starts when the user logs in or otherwise identifies himself/herself, and continues until the user logs out or completes the transaction (for example, makes a purchase)
Information about the user
User information must generally be maintained much longer than session information (for example, remembering a purchase)
This information must be stored on the server, for example on a file or in a database
Server capabilities :Server capabilities Servlets, like Applets, can be trusted or untrusted
A servlet can use a unique ID to store and retrieve information about a given session
User information usually requires a login ID and a password
Since servlets don’t quit between requests, any servlet can maintain information in its internal data structures, as long as the server keeps running
A trusted servlet can read and write files on the server, hence can maintain information about sessions and users even when the server is stopped and restarted
An untrusted servlet will lose all information when the servlet or server stops for any reason
This is sometimes good enough for session information
This is almost never good enough for user information
Session tracking :Session tracking HTTP is stateless: When it gets a page request, it has no memory of any previous requests from the same client
This makes it difficult to hold a “conversation”
Typical example: Putting things one at a time into a shopping cart, then checking out--each page request must somehow be associated with previous requests
The server must be able to keep track of multiple conversations with multiple users
Session tracking is keeping track of what has gone before in this particular conversation
Since HTTP is stateless, it does not do this for you
You have to do it yourself, in your servlets
You can do this by maintaining a session ID for each user