8
Servlets Part 3

Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Embed Size (px)

Citation preview

Page 1: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

ServletsPart 3

Page 2: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Topics

• Session Tracking• ServletToServletCommunication-Servlet Chaining• ServerSideIncludes• AppletToServlet

Page 3: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Server-Side Includes and Request Forwarding

• Server Side includes (SSI) allow you to embed servlet calls within an HTML document using <servlet> tag.

• By convention, these documents should use the .shtml extension – indicating to the server that the HTML document includes SSI directives and should be passed to a specialized SSI servlet for processing.

• This special servlet (called SSIncludesServlet in Java Web Server) parses the <servlet> tags.

• When the <servlet> tag is included, the SSIncludesServlet loads the servlet specified by the <servlet> tag and includes the servler output in the HTML page returned to the client.

Page 4: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Server-Side Includes and Request Forwarding

Syntax of the servlet tag

<servlet name=SERVLET_NAME

code=CLASS_NAME

codebase=CLASS_LOCATION_URL

INIT_PARAM1=VALUE1

INIT_PARAM2=VALUE2

INIT_PARAM3=VALUEn >

<param name=SERVLET_PARAM1 value=VALUE1>

<param name=SERVLET_PARAM1 value=VALUE2>

<param name=SERVLET_PARAM1 value=VALUEn>

</servlet>

Page 5: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Server-Side Includes and Request Forwarding

HTML Page using SSI to add a header and a footer

<HTML><HEAD><TITLE>Servlet API </TITLE></HEAD>

<BODY>

<servlet code=“Header_Servlet”</servlet>

<P> Some of these servers support Java Servlet API

Web Logic, IBM Visual Age..

<BR>

<servlet code=“Footer_Servlet”</servlet>

</BODY>

</HTML>

Page 6: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Servlet Chaining

• Servlet chaining is the process of passing the output of one servlet to the input of another.

• This feature is similar to piping in UNIX.• The client request is sent to the first servlet in the chain.• Once the request has been processed by the servlet, it

passes it soutput to the next servlet in the chain.• This process continues through any number of servlets

until the last servlet’s output is returned to the client.• Servlet chain configuration is server-specific.

Page 7: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Applet To Servlet

• A servlet can generate a page with parameter values set to be passed to the embedded applet.

• Example:public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = new PrintWriter ( response.getOutputStream()); out.write("<HTML><HEAD><TITLE> HTML Manipulation Example</TITLE> </HEAD><BODY>"); out.write("This applet receives data from the server via its PARAM tag"); out.write("<applet code=\"SimpleApplet\" width=\“300\" height=\“200\">"); out.write("<param name=\"Data\" value=\""; ); out.write("\">"</APPLET></BODY></HTML>); out.close(); }

Page 8: Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet

Applet To Servlet

• See files MemberServlet.java and MemberApplet.java