Java

Tuesday, 3 April 2012

weekend class meterials(jsp)


TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  






Describe JSP  technology                                          
Write JSP  code using scripting elements
Write jsp code using the page directive
Write jsp code using standard tags
Write sp code using the expression language
Configure the JSP environment in the web.xml file:


JSP technology support separation of presentation and businesss logic as follows:

Web designers can desingners can desing and update pages without learning the java programming lauguage.

Java technology programmers can write presentation layer code without having to be concernewd about web pae design.

Translate the JSP to servlet code
Compoile the servlet to bytecode
Load the servlet class
Create the servlet instance
Call the jspInit() method.
Call the _jspService() method
Call the jspDestroy() method


scriptin elements:

<%--       comment
<%@       directive
<%!          Declaration
<%            scriptet
<%=          expresion


Comment :

<!--     this is an html comment it will show up in the response.-->
<%--   this is a JSP comment it will only be seen the jsp code    -->

Directive:
This is used to refer the exterenal resources to the jsp page.
<%@ DirectiveName [attr=’value’] %>

Ex:
<%@ page session=”false”%>
<%@include file=”home.html” %>



Declaration:
This is used to declare the member variable ,methods to the jsp page.

<%!  Public String name=”madhav” %>
<%!  Public void add()
{
}
%>

<% int i=0; %>

Expresion:
Used to write the expresions.

The current day and time is: <%=new java.util.Date() %>

Example program:



JSP implicit objects:


Variable name
description
request
HttpServletRequest object associated with the request.
Response
HttpServletResponse object associated with the response that is send back to the browser.
Out
JspWriter object associated with the output stream of the response.
Session
HttpSession object associated with the session for the given user of the request variable only meaningful if the JSP page is participation the an HTTP session
Application
ServletContext object for the web application
Config
ServletConfig object associated  with the servlet for this JSP page.
pageContext
pageContext object that encapsulates the environment of a single request for this JSP page.
Page
Page variable is equivalent to the this variable in the java programming language.
Exception
Throwable object that was thrown by some other JSP page variable is only available in a JSP error page.

Example program:

studentform.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>studentform.html</title>
     
      </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="Student.jsp" >
    name  :<input type="text" name="name"/><br>
    id    :<input type ="text" name="id"/><br>
    course:<input type="text" name="course"/><br>
    <input type="submit" value="send"/>
    </form>
  </body>
</html>

Student.jsp:     //jsp file using implicit objects


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   


Oval Callout: JSP implicit object


  </head>
 
  <body bgcolor="gray">
    student infomrmation. <br>
  <%
  String name=request.getParameter("name");
  String id=request.getParameter("id");
  String course=request.getParameter("course");
  out.println("name="+name);
  out.println("<br>");
  out.println("id="+id);
  out.println("<br>");
  out.println("course=" +course);
 
  %>

 
  </body>
</html>



JSP using JavaBeans:


A JavaBeans component is a class with:

Progerties defined with accessors and mutators(get and set methos)
A no-argument constructor
No public instance variable
The class implements the java.io.Serializable interface.

Jsp standared tags:

Supported in every JSP container
Reduce or elimanate scriptlet code
Begin with the jsp:prefix
Only provide limited functionality

useBean:   //jsp standard tag

syntax:

<jsp:useBean id=”beanName” scope=”page|request|session|application”
Oval Callout: this wil act as reference variable to the StudentData object.Class=”className”/>

Ex:
<jsp:useBean id="stu" class="softech.StudentData"></jsp:useBean>

setProperty:   jsp standard tag


<jsp:setProperty name=”beanName” property-expression/>

Ex:
<jsp:setProperty name="stu" property="id"/>
In java:

stu.setId(request.getParameter(“id”));

Example program:

studentform.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>studentform.html</title>
     
      </head>
 
  <body>
    This is my HTML page. <br>
   
    <form action="Student.jsp" >
    name  :<input type="text" name="name"/><br>
    id    :<input type ="text" name="id"/><br>
    course:<input type="text" name="course"/><br>
    <input type="submit" value="send"/>
    </form>
  </body>
</html>

StudentData.java:   //java bean

package softech;

import java.io.Serializable;

public class StudentData implements Serializable {
      private String name;
      private int id;
      private String course;
     
      public StudentData()
      {
           
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public int getId() {
            return id;
      }

      public void setId(int id) {
            this.id = id;
      }

      public String getCourse() {
            return course;
      }

      public void setCourse(String course) {
            this.course = course;

}
}

Student.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Rounded Rectangular Callout: Jsp standard tagOval Callout: JavaBean<html>
  <head>
   
        </head>
  <body bgcolor="gray">


    student infomrmation. <br>
    <jsp:useBean id="stu" class="softech.StudentData"></jsp:useBean>
    <jsp:setProperty name="stu" property="name"/>
    <jsp:setProperty name="stu" property="id"/>
    <jsp:setProperty name="stu" property="course"/>
    <jsp:getProperty name="stu" property="name"/><br>
    <jsp:getProperty name="stu" property="id"/><br>
    <jsp:getProperty name="stu" property="course"/>
  </body>
</html>



TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


No comments:

Post a Comment