ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
Servlets
1.
2.
Servlets are part of the Java2EE specification.
Servlets are modules that run on the server,
enabling you to extend the server’s functionality.
Servlets work within a Web server environment,
and they are a key component of server side
Java development. Servlets are an effective
replacement for CGI scripts.
3.
Can be deployed into distributed server
environments.
Servlets are platform- and server-independent.
Servlets are easy to develop and follow a
standard API.
Servlets are extensible. Java Server Pages (JSP)
build on top of the Servlet API.
A servlet is a server resource, providing access to
other server resources, such as other servlets,
EJBs, JSPs, JDBC, and so on.
4. Method
Description
GET
The client requests information from the given
URL.
HEAD
Similar to GET, except the body is not retrieved.
POST
Client adds info to URI (HTML forms)
PUT
Used to place documents on the Server.
DELETE
Client deletes resource of URI.
6.
The Servlet API defines a standard interface for
handling request and response between the
browser and the Web server.
The Servlet API is composed of two packages:
javax.servlet - javax.servlet.GenericServlet
javax.servlet.http - javax.servlet.HttpServlet
9. Can use ServletOutputStream or PrintWriter to send data
back to the client.
1. reference the stream from the Response parameter:
ServletOutputStream out =response.getOutputStream();
2. get a reference to the writer from the Response
parameter:
PrintWriter out = response.getWriter();
3. Then write the output to the stream:
out.println(“<HTML>Inside HTML</HTML>”);
4. Finally, close the writer:
out.close();
10.
MIME – Multiple Internet Mail Extension
Identifies extension of each file in the
HTTPResponse
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
11. import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
public class srvltJust extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException {
res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“<HTML>”);
out.println(“<HEAD><TITLE>Servlet</TITLE></HEAD>”);
out.println(“<BODY>”);
out.println(“<H1>This is a just a Servlet!</H1>”);
out.println(“</BODY></HTML>”);
out.close();
12.
13.
14. Parameters
public String getParameter(String name)
public Enumeration getParameterNames()
public String[] getParameterValues(String name)
Content
public int getContentLength() - returns the length, in
bytes. -1 is returned if the length is not known.
getContentType() - returns the request’s MIME type
of the content (null if it’s not known).
getCharacterEncoding() - returns the name character
encoding style of the request.
19. API for persistent cookie is,
javax.servlet.http.Cookie
To create a new cookie,
Cookie cookie(String name, String value)
Eg: Cookie cookie = new Cookie("ID", "123");
To get all available cookies,
req.getCookies()
To send back the cookie name
response.addCookie(cookie_name)
20.
A servlet with getSession( ) method retrieves the current
HttpSession object
Eg: public HttpSession HttpServletRequest.getSession(boolean )
Set properties by,
public void HttpSession.setAttribute(String name, Object value)
Eg: session.setAttribute(“name”, id);
Get properties by,
public void HttpSession.setMaxInactiveInterval(int secs)
21.
Get current session id by,
public String getId()
Whether it is a new cookie or referenced,
public boolean isNew
Start of a session
public long getCreationTime()
Last session activity
public long getLastAccessedTime
Session invalidating by,
public void invalidate()
Removing attribute by,
public void removeAttribute(String name)