JApplet
. Eliminate
the main() and frame specific code (the sizing,
the default close, the call to show()).
JApplet
class have four methods you can override
<APPLET CODE=MyApplet.class" WIDTH=300 HEIGHT=300 >
To address these issues, Web server developed the ability to run certain sorts of programs and scripts 'in-process' by multithreading the Web server. Now each script runs in its own thread but can share things like database connections (which are expensive to set up).
Since Java programs, unlike C, requires a Java runtime, most Web servers do not run servlets directly. The are a few Web server, like Apache Tomcat which specialize in server-side Java (at the expense of standard Web page performance). Often a dual strategy is adopted, one server for Java pages and one for static and traditional CGI.
init
and service
.
init()
method is call once, the first time the servlet
is loaded into the Web server. It should be used for one-time initialization.
service()
method is called once for each HTTP request.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServTest extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Test</title></head>"); out.println("<body><h1>Hello ADU servlet</h1>"); out.println("</body></html>"); } }
This problem is addressed at the protocol level by remembering some information associated with the current Web session which can be used to tag session information on the server. There are two ways to keep this information, the first is to build it into the Web pages sent down and the URLs sent back. This is known as URL-rewriting. The second method uses cookies, small files stored on the client's machine to identify the current user and session. The details of the session management protocol are irrelevent here. Only the fact that it is possible is important.
Session
object.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServTest extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); Integer count = (Integer)session.getAttribute("count"); if(count == null) count = new Integer(0); session.setAttribute("count",new Integer(count.intValue() + 1)); out.println("<html><head><title>Test</title></head>"); out.println("<body><h1>Hello ADU servlet</h1>"); out.println("count = " + count); out.println("</body></html>"); } }
Probably the first example of this technology was Microsoft's Active Server Pages (ASP). Other examples are Allaire's ColdFusion and PHP (the open source favorite. Java's entry into this are is called JSP, JavaServer Pages.
<html> <head><title>JSP Test</title></head> <body> <h1>Hello ADU</h1> It's <%= new java.util.Date().toString() %> </body> </html>