Servlet - A Java program loaded and executed by the Web Server, in contrast to applets which are handled by a Web Browser. Hence servlets are called `server-side applets' [ Moss.2 ] JSDK ( Java Servlet Development Kit ) - A part of JDK1.2 onwards [ Moss.4 ] |
Advantages
|
<html><body> <form method="POST" action="/servlet/properties" enctype="x-www-form-encoded"> <input name="input" type="submit" value="test servlet"> </body></html> |
file.shtml [Moss.65]
<servlet name="servletname" code="servlet.class"> codebase="codebase" init_param1="pvalue"> <param name="pname" value="pvalue"> </servet> SSI servlet invoked only if shtml suffix is requested. |
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class hiworld extends HttpServlet { // 1. Override HttpServlet.doGet() and/or doPost() methods public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 2. HttpServletResponse setContentType() is used resp.setContentType("text/html"); // 3. java.io.PrintWriter writes response using HTTP output stream PrintWriter pw = new PrintWriter( resp.getOutputStream() ); pw.println("<html><body>"); pw.println("<h1> Hi World </h1>" + "</body></html>"); pw.close(); } } |
PrintWriter pw = resp.getWriter(); [ for JSDK2.0, Zeiger.2.1 ] PrintWriter pw = new PrintWriter(resp.getOutputStream() ); [ Moss.38 ] ServletOutputStream out = resp.getOutputStream(); [ Zeiger, 2.1 ]
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class track extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1. Get session object, create one if non-existent (true) HttpSession hsess = req.getSession(true); // 2. Get Number of Hits Integer numofhits = (Integer) hsess.getValue("hitcount.txt"); if (numofhits == null) { numofhits = new Integer(1); } else numofhits += numofhits + 1; // 3. Put incremented hit-value back hsess.putValue("hitcount.txt",numofhits); PrintWriter pw = resp.getWriter(); resp.setContentType("text/html"); pw.println("<html><body>"+ "You have visited this page"+ numofhits + " times " pw.println("Your Session ID is : "+ hsess.getID(); "</html></body>"); pw.close(); } } |
import javax.servlet.*; import javax.servlet.http.*; public class cookies extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { Cookie cookys[] = req.getCookies(); java.io.PrintWriter pw = resp.getWriter(); resp.setContentType("text/html"); pw.println("<html><body>"); if ( ( cookys == null ) || ( cookys.length == 0 ) ) { pw.println("No cookies found"); } else { pw.println("<h1> Cookies found </h1>"); for (int i=0; i < cookys.length; i++ ) { Cookie c = cookies[i]; pw.println("<tr><td>"+c.getName()+"</td>"+ "<td>"+c.getValue()+"</td>"+ "<td>"+c.getComment()+"</td>"+ "<td>"+c.getMaxAge()+"</td></tr>"+ "</table></center>"); } } pw.println("</body></html>"); } } |
import javax.servlet.*; import java.io.*; public class ssi extends GenericServlet { // 1. Override service method public void service(ServletRequest req, ServletResponse resp) { try{ PrintWriter pw = new PrintWriter( resp.getOutputStream() ); String pvalue = req.getParameterValue("pname"); pw.println("<html>&`lt;body>"); pw.println(""+pvalue+""); pw.println("</html></body>"); } catch(ServletException se) { } catch(IOException ioe) { } } } |
<html><body> <servlet code="javaservlets.samples.ssi"> <param name="pname" value="Value of Parameter"> <servlet> <p> Then some text here. </body></html> |
import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Force Server Loading instead of Cache Loading by Client resp.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); // Get Session User HttpSession hsess = req.getSession(true); String suser = (String) hsess.getValue("users.txt"); if (suser == null) { // Force browser to prompt for login resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // Following page displayed if user presses `Cancel' pw.println("<html><body>"+ "<h1> Invalid User </h1>"+ "<body><html>"); } } protected String validUser(HttpServletRequest req) { // decoding operations from base64 } protected boolean validateUser(String suser, String pwd) { boolean valid = false; if ( (suser !- null) && (pwd != null) ) { if (user.equals("samar")) { valid=true; } } return valid; } } |
SSL ( Secure Sockets Layer ) - Encryption system which sits on top of all socket communivation, encrypting outgoind data and decrypting incoming ones.
import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class LoginApplet extends Applet { Label msg; public void init() { String servlet = getParameter("servlet"); setLayout(new BorderLayout(0,5)); Panel p = new Panel(); TextField userfld = new TextField(10); p.add(userfld); TextField pwdfld = new TextField(10); pwdfld.setEchoCharacter('*'); p.add(pwdld); Button b = new Button("Login"); add("South",login); } public boolean handleEvent(Event e) { String user = userfld.getText(); String pwd = pwdfld.getText(); boolean valid = false; valid = validate_user(user, pwd); if (!valid) { msg.setText("Invalid User - try again."); } else { msg.setText("Welcome, you are valid !"); } } protected boolean validate_user(String user, String pwd) { boolean valid = false; // Obtain servlet name which will validate password String codeBase = getCodeBase(); String servlet = getParameter("servlet"); // Get Server URL java.net.URL url = new java.net.URL(codeBase + servlet); // Attempt Connection to host java.net.URLConnection con = url.openConnection(); // Initialise connection con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(byteout); dout.writeUTF(hsessId); // Send User and Password dout.write(user); dout.write(pwd); // Flush data to buffer dout.flush(): // some more steps left out DataInputStream din = new DataInputStream(con.getInputStream() ); valid = din.readBoolean(); if(valid) { nextDoc = din.readUTF(); } din.close(); return valid; } } |
Tunneling - Method of using the stateless HTTP to make remote calls over the internet in contrast to RMI, which uses TCP/IP. [ Moss.213 ]
import java.io.*; public class TunnelApplet { public static void main(String args[]) { try{ // Get the server URL java.net.URL url = new java.net.URL(args[0]); // Attempt to connect to host and initialise connection java.net.URLConnection con = url.openConnection(); con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(byteout); System.out.println("Writing test data "); dout.writeBoolean(true); dout.writeByte(1); dout.writeChar(2); dout.writeInt(4); dout.writeFloat(5); dout.writeDouble(6); dout.writeUTF("Hello, Karl"); dout.flush(); // flush output to the buffer // Get our output to be sent byte buf[] = byteout.toByteArray(); // Inform server of content type and length of data buffer con.setRequestProperty("Content-type application/octet-stream"); con.setRequestProperty("Content-length "+buf.length); // Get output stream to the server and send our data buffer DataOutputStream datout = new DataOutputStream(con.getOutputStream() ); datout.write(buf); datout.flush(); datout.close(); System.out.println(" Reading response "); DataInputStream din = new DataInputStream(con.getInputStream() ); boolean booleanval = din.readBoolean(); byte byteval = din.readByte(); char charVal = din.readChar(); int intval = din.readInt(); float flatval = din.readFloat(); String strgval = din.readUTF(); din.close(); System.out.println(" Data read : "+ booleanval + byteval + ( (int) charval ) + intval + floatval + doubleval + strgval } catch(Exception e) { e.printStackTrace() ); } } } |
import java.io.*; public class TunnelServlet { public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Get the input stream DataInputStream din = new DataInputStream(req.getInputStream()); // Create buffer ByteArrayOUtputStream byteaos = new ByteArrayOutputStream(); // Create the output stream to be used to write data to buffer DataOutputStream dout = new DataOutputStream(byteaos); // Read data from client boolean booleanval = din.readBoolean(); byte byteval = din.readByte(); char charval = din.readChar(); int intval = din.readInt(); float floatval = din.readFloat(); double doubleval = din.readDouble(); String stringval = din.readUTF(); // Write data to internal buffer dout.writeBoolean(booleanval); dout.writeByte(byteval); dout.writeChar(charval); dout.writeInt(intval); dout.writeFloat(floatval); dout.writeDouble(doubleval); dout.writeUTF(stringval); // Flush contents of the output stream to the byte array dout.flush(); // Get buffer that is holding our response byte90 buf = byteOut.toByteArray(); // Notify client of how much data is being sent resp.setContentLength(buf.length); // Send the buffer to the client ServletOutputStream servletout = resp.getOutputStream(); servletout.write(buf); servletout.close(); } } |
The steps are the same as in ordinary JDBC. "Instead of printing the information to the standard output device (the console) we'll need to format the html that wil be sent back to the client." [ Moss.176 ]
Services [Moss.11]
|
Internal Servlets [Moss.29]
|
Thus, using Reflection, process can be automated, eg. applet-servlet communication.