Database Access Using Lightweight Applets
Example 5 is an HTML file you can use to display OrganizationDDLBApplet.
Example 6, Login, is a servlet you can use to set the authenticated flag in the
current session to enable the use of SqlServlet.
Example 5. OrganizationDDLBApplet.html
<html>
<head>
<title>Organization</title>
</head>
<body BGCOLOR="#BFBFBF">
<applet
code="OrganizationDDLBApplet.class"
codebase="http://dssw2k01:8080/learn/applet/"
height="550"
width="750" >
<!-- Tell the applet where its peer is located -->
<param
name="servlet"
value="http://dssw2k01:8080/learn/servlet/OrganizationDDLBServlet" />
<param
name="BGCOLOR"
value="#BFBFBF" />
</applet>
</body>
</html>
Example 6. Login
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Get the current session or create a new one
HttpSession session = request.getSession();
// Get the authentication flag
String authenticated =
(String)session.getAttribute("authenticated");
// If it's valid then remove it
if (authenticated != null) {
session.removeAttribute("authenticated");
}
// Get the userid and password
String userid =
request.getParameter("userid");
String password =
request.getParameter("password");
PrintWriter out =
response.getWriter();
response.setContentType("text/html");
// Display a login screen if needed
if (userid == null || password == null) {
out.println("<html>");
out.println("<head>");
out.println("<title>Login</title>");
out.println("<body>");
out.println("<form action\"../servlet/Login\"
method=\"post\">");
out.println("Please enter your useid and password
then submit to log-in:<br/>");
out.println("Userid: <input name=\"userid\"
size=\"30\" type=\"text\"><br/>");
out.println("Password: <input name=\"password\"
size=\"30\" type=\"password\"><br/>");
out.println("<input type=\"submit\"");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
else {
// Add authetication here!!!
session.setAttribute("authenticated", "");
out.println("<html>");
out.println("<head>");
out.println("<title>Login</title>");
out.println("<body>");
out.println("<h3>You have logged-in successfully.</h3>");
out.println("</body>");
out.println("</html>");
}
}
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
Conclusion
Using lightweight applet technology, you can bring the full weight of Java's
GUI capability, and the advantages of client-side dynamic database access, to
your Web applications. Because the technology employs HTTP to communicate with
a database via SqlServlet, lightweight applets perform well yet remain small
enough to have acceptable download times.
You can get a copy of the source code for this article
here.
For more information on applets, look at Learning Java. For HTTP communications read Java
I/O. For servlets, check out the
totally excellent Java Servlet Programming. And for more information on Oracle's
implementation of JDBC, check out my book, Java Programming with Oracle
JDBC.
Donald Bales
is a Computer Applications Consultant
specializing in the analysis, design, and programming of
distributed systems; systems integration; and data warehousing.
Return to ONJava.com.