0% found this document useful (0 votes)
43 views2 pages

Servlet and JSP

The document provides an overview of key concepts related to servlets and JSPs. It discusses what the servlet container provides, the lifecycle methods of servlets including init(), service(), and doGet()/doPost(), how requests are handled by one thread per request. It also summarizes servlet initialization, the ServletConfig and ServletContext objects, idempotent vs non-idempotent requests, default form submission method, request and response parameter methods, MIME types, and forwarding requests internally in the server.

Uploaded by

Lahiru Ruhunage
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Servlet and JSP

The document provides an overview of key concepts related to servlets and JSPs. It discusses what the servlet container provides, the lifecycle methods of servlets including init(), service(), and doGet()/doPost(), how requests are handled by one thread per request. It also summarizes servlet initialization, the ServletConfig and ServletContext objects, idempotent vs non-idempotent requests, default form submission method, request and response parameter methods, MIME types, and forwarding requests internally in the server.

Uploaded by

Lahiru Ruhunage
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Servlet and JSP – Bullet Points

What does the Container give you? Communications support, Lifecycle


Management, Multithreading Support,
Declarative Security, JSP Support
Servlet lifecycle methods Init() - Gives you a chance to initialize your
servlet before handling any client requests.
Service (req, res) - This method looks at the
request, determines the HTTP method (GET,
POST, etc.) and invokes the matching doGet(),
doPost(), etc. on the servlet.
doGet() and doPost() - This is where your code
begins! This is the method that’s responsible
for whatever the heck your web app is sup-
posed to be DOING.
Is it one thread per client or one thread per One thread per request. The Container doesn’t
request? care who makes the request—every incoming
request means a new thread/stack.
When init and service methods call? servlet’s service() method will not run until the
servlet is fully initialized.
Servlet initialization A servlet moves from does not exist to
initialized (which really means ready to service
client requests), beginning with a constructor.
But the constructor makes only an object, not a
servlet. To be a servlet, the object needs to be
granted servletness.
A ServletConfig object - One ServletConfi g object per servlet.
- Use it to pass deploy-time information
to the servlet (a database or enterprise
bean lookup name, for example) that
you don’t want to hard-code into the
servlet (servlet init parameters).
- Use it to access the ServletContext.
- Parameters are confi gured in the
Deployment Descriptor.
A ServletContext - One ServletContext per web app. (They
should have named it AppContext.)
- Use it to access web app parameters
(also confi gured in the Deployment
Descriptor).
- Use it as a kind of application bulletin-
board, where you can put up messages
(called attributes) that other parts of the
application can access (way more on
this in the next chapter).
- Use it to get server info, including the
name and version of the Container, and
the version of the API that’s supported.
Idempotent request It means you can do the same thing over and over
again, with no unwanted side effects! (e.g GET)
Non- Idempotent request Do the same thing over and over again, not good.
(e.g POST method)
Default form method if not mentioned GET
Request parameter methods request.getParameter(“color”);
request.getHeader(“User-Agent”);
HttpSession session = request.getSession();
request.getMethod();
request.getInputStream();
Response parameter methods setContentType();
getOutputStream(); for ServletOutputStream
getWriter(); for PrintWriter
setHeader() ;overwrites the existing value
addHeader(); adds an additional value
response.sendRedirect(“http://www.oreilly.com”);
: this will redirect the client

RequestDispatcher view =
request.getRequestDispatcher(“result.jsp”);
view.forward(request,response); : this will
redirect the server

MIME types Text/html, application/pdf, video/quicktime


image/jpeg, application/jar, application/x-zip

You might also like