Web Components: Technology Consulting
Web Components: Technology Consulting
Technology Consulting
Pramati Technologies
Agenda
• 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?
– 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
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()
Servlet Class Hierarchy…
• 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.*;
1. Request
HelloServlet
3.
init(ServletConfig)
4.
Client Servlet Engine Initialization
6. Response
5. service(ServletRequest,
ServletResponse) Initialized
HelloServlet
Another Simple Servlet
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>\n" +
"<HEAD><TITLE>HelloWWW</TITLE></HEAD>\n" +
"<BODY>\n" + "<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
Form Example
HTML Post Form
<FORM ACTION=“/servlet/hall.ThreeParams”
METHOD=“POST”>
First Parameter: <INPUT TYPE="TEXT" NAME="param1"><BR>
Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR>
Third Parameter: <INPUT TYPE="TEXT" NAME="param3"><BR>
<CENTER>
<INPUT TYPE="SUBMIT">
</CENTER>
</FORM>
Reading Parameters
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(… +"<UL>\n" +
"<LI>param1: " + request.getParameter("param1") + "\n" +
"<LI>param2: " + request.getParameter("param2") + "\n" +
"<LI>param3: " + request.getParameter("param3") + "\n" +
"</UL>\n" + …);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
}
Servlet Output
Reading All Params
• Enumeration paramNames = request.getParameterNames();
– parameter names in unspecified order
• String[] paramVals =
request.getParameterValues(paramName);
– Array of param values associated with paramName
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);
Welcome!
</body>
</html>
Role of the Container
Hello.jsp
2. Read
Translation
phase
1. Request
Hello.jsp
HelloServlet.java
Server with
Client JSP Container 3. Generate
6. Response
4. Compile
5. Execute
Request
HelloServlet.class
Processing
phase
JSP Elements
• Directive Elements
– Info about the page
– Remains same between requests
– E.g., scripting language used
• Action Elements
– Take action based on info required at request-time
• Standard
• Custom (Tags and Tag Libraries)
• Scripting Elements
– Add pieces of code to generate output based on conditions
Directives
• Global information used by the “JSP engine”
• Of form <%@ directive attr_ list %>
• Or <jsp: directive. directive attr_ list />
– Directive could be
• Page
• Include
• Taglib
– E. g.,
• <%@ page info=“ written by DevelopMentor” %>
• <jsp: directive. page import=“ java. sql.*” />
• <%@ include file=“\ somefile. txt” %>
• <%@ taglib uri= tags prefix=“ foo” %>
Actions Within a JSP Page
• Specifies an action to be carried out by the “JSP engine”
• Standard or custom
– Standard must be implemented by all engines
– Custom defined in tag libraries
• Standard actions ‘scoped’ by ‘jsp’ namespace
• Have name and attributes
<jsp: useBean id=“ clock” class=“ java.util.Date” />
<ul> The current date at the server is:
<li> Date: <jsp: getProperty name=“clock” property=“date” />
<li> Month: <jsp: getProperty name=“clock” property=“month” />
</ul>
Standard JSP Actions
• jsp:useBean
• jsp:getProperty
• jsp:setProperty
• jsp:include
• jsp:forward
• jsp:param
• jsp:plugin
Scriptlets
• Of form <% /* code goes here*/ %>
– Gets copied into _ jspService method of generated servlet
CODE: OUTPUT:
<% int j; %> <value> 0</ value>
<% for (j = 0; j < 3; j++) {%> <value> 1</ value>
<value> <value> 2</ value>
<% out. write(""+ j); %>
</ value><% } %>
Declarations (<%! … %>)
• Used to declare class scope variables or methods
<%! int j = 0; %>
• Gets declared at class- level scope in the generated servlet
implements
Tag TagSupport
Interface class
extends extends
implements
BodyTag BodyTagSupport
Interface class
Simple Tag Example
<%@
<%@ taglib
taglib uri=“/WEB-INF/mylib.tld”
uri=“/WEB-INF/mylib.tld” prefix=“test”
prefix=“test” %>
%>
<html><body
<html><body bgcolor=“white”>
bgcolor=“white”>
<test:hello
<test:hello name=“Robert”
name=“Robert” />
/>
</body>
</body> </html>
</html>
mylib.tld
<taglib> ……
<tag><name>hello</name>
<tagclass>com.pramati.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute><name>name</name></attribute>
</tag>
</taglib>
How Tag Handler methods are
invoked
<prefix:tagName
attr1=“value1” ------------ setAttr1(“value1”)
attr2=“value2” ------------ setAttr2(“value2”)
> ------------ doStartTag()
This tags's body
</ prefix:tagName>------------ doEndTag()