Tuesday, 27 November 2012

J2ee Tutorial 10 :- Life cycle of jsp and explanation of scriplets,expressions and declarations in jsp

//Base concept of jsp

<%@ page language="java" %>
<html>
<head>
<title>JavaServer Page Lifecycle</title>
</head>
<body bgcolor="#FFFFFF">
<h1>To illustrate JavaServer Page lifecycle</h1>
<p>Look at your server console for System.out.println() output.</p>
</body>
</html>
<%!

public void init() {
  // Illegal?
  System.out.println("Actually OK to override GenericServlet's init() convenience method");
}

public void jspInit() {
  try {
    throw new Exception();
  } catch (Exception e) {
    StackTraceElement[] st = e.getStackTrace();
    System.out.println(st[0]);
  }
 
  // Show inheritance hierarchy and implementing interfaces
  Class c = this.getClass();
  System.out.println("\nClass " + c.getName());
  while ((c = c.getSuperclass()) != null) {
    System.out.println("subclass of " + c.getName());
    Class[] ifaces = c.getInterfaces();
    for (int i = 0; i < ifaces.length; i++) {
      if (i == 0) {
        System.out.print(" which implements interfaces: "
        + ifaces[0]);
      } else {
        System.out.print(", " + ifaces[i].getName());
      }
    }
    System.out.println();
  }
}


public void jspDestroy() {
  try {
    throw new Exception();
  } catch (Exception e) {
    StackTraceElement[] st = e.getStackTrace();
    System.out.println(st[0]);
  }
}

%>

<%
  // The scriptlet code below finds its way into the _jspService() method
  try {
    throw new Exception();
  } catch (Exception e) {
    StackTraceElement[] st = e.getStackTrace();
    System.out.println(st[0]);
  }

%>

//explanation of scriplets,expressions and declarations in jsp
<%!
private String convert(int miles) {
  java.text.DecimalFormat formatter = new java.text.DecimalFormat("###.##");
  String value = formatter.format(1.6 * miles);
  return value;
}
%>
<%
int[] mileValues = {1,2,3,5,10,15,20,50,100,200,500};
%>
<html>
<head><title>Miles to Kilometers</title></head>
<body>
<table border="1">
  <tr>
    <th><b>Miles</b></th>
    <th><b>Kilometers</b></th>
  </tr>
<% for (int i = 0; i < mileValues.length; i++) { %>
  <tr>
    <td><%=mileValues[i]%></td>
    <td><%=convert(mileValues[i])%></td>
  </tr>
<% } %>
</table>
</body>
</html>

Output:-



No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...