With the SHTMLServlet, we demonstrate how to make the Web application server dynamically generate part of its HTML file. Using the servlet ...
With the SHTMLServlet, we demonstrate how to make the Web application server dynamically generate part of its HTML file. Using the servlet tag technique, the server converts a section of an HTML file into a dynamic portion each time the document is sent to the client. This dynamic portion invokes an appropriate servlet, and inserts the response of that server in the HTML page that is sent to the Web client. Initialization and other servlet parameters can be passed through the tag syntax, similar to the way an applet’s parameters are set in the HTML. Here, the Web browser is calling the servlet indirectly, through the SHTML page. The Web server is responsible for including the output of the specified servlet in the HTML response.
The HTML syntax is as follows:
<servlet> name="myServlet" code="package.classname" </servlet>
CODE A shows the HTML that we used to call our servlet, and CODE B shows the servlet whose response is dynamically included between the tags. Notice that this servlet only generates a part of the total response back to the Web client.
CODE A
CODE A
<HTML><BODY>
<H2>Start of SHTML Servlet Example, the following lines are from the servlet:
</H2> <HR>
<SERVLET name="SHTMLServlet”
CODE="itso.servjsp.servletapi.SHTMLServlet"> </SERVLET>
<HR> <H2>ENd of servlet include</H2>
</HTML></BODY>
<H2>Start of SHTML Servlet Example, the following lines are from the servlet:
</H2> <HR>
<SERVLET name="SHTMLServlet”
CODE="itso.servjsp.servletapi.SHTMLServlet"> </SERVLET>
<HR> <H2>ENd of servlet include</H2>
</HTML></BODY>
The <SERVLET> tag has been replaced with <jsp:include> in JSP 1.0. See “Calling a servlet from a JSP” for examples of how to invoke a servlet from a JSP, which is a more modern technique to accomplish the same purpose.
CODE B
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SHTMLServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<HR><H4>Servlet API Example - SHTMLServlet</H4>");
out.println("<H4>Basic included servlet...</H4><HR>");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SHTMLServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<HR><H4>Servlet API Example - SHTMLServlet</H4>");
out.println("<H4>Basic included servlet...</H4><HR>");
}
}