+ All Categories

JSP

Date post: 26-Sep-2015
Category:
Upload: anon255995442
View: 216 times
Download: 3 times
Share this document with a friend
Description:
Jsp notes
Popular Tags:
14
JSP It provides more functionality than servlet such as expression language, jstl etc Easier to maintain than servlet because we can separate designing and development There are many advantages of JSP over servlet. They are as follows: 1) Extension to Servlet JSP technology is the extension to servlet technology. We can use all the features of servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy. 2) Easy to maintain JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic. 3) Fast Development: No need to recompile and redeploy If JSP page is modified, we don't need to recompile and redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application. 4) Less code than Servlet In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code. Moreover, we can use EL, implicit objects etc. Life Cycle of JSP pages Scripting elements The scripting elements provides the ability to insert java code inside the jsp. There are threetypes of scripting elements: scriptlet tag
Transcript
  • JSP

    It provides more functionality than servlet such as expression language, jstl etc

    Easier to maintain than servlet because we can separate designing and development

    There are many advantages of JSP over servlet. They are as follows:

    1) Extension to Servlet

    JSP technology is the extension to servlet technology. We can use all the features of servlet in

    JSP. In addition to, we can use implicit objects, predefined tags, expression language and

    Custom tags in JSP, that makes JSP development easy.

    2) Easy to maintain

    JSP can be easily managed because we can easily separate our business logic with presentation

    logic. In servlet technology, we mix our business logic with the presentation logic.

    3) Fast Development: No need to recompile and redeploy

    If JSP page is modified, we don't need to recompile and redeploy the project. The servlet code

    needs to be updated and recompiled if we have to change the look and feel of the application.

    4) Less code than Servlet

    In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code.

    Moreover, we can use EL, implicit objects etc.

    Life Cycle of JSP pages

    Scripting elements

    The scripting elements provides the ability to insert java code inside the jsp. There are threetypes of scripting elements:

    scriptlet tag

  • expression tag

    declaration tag

    1.5.1. JSP SCRIPTLET TAG

    A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:

    1.5.2. JSP EXPRESSION TAG

    The code placed within expression tag is written to the output stream of the response. So you

    need not write out.print() to write data. It is mainly used to print the values of variable or

    method.

    Syntax of JSP expression tag

    1.

    Note: Do not end your statement with semicolon in case of expression tag.

    1.5.3. JSP DECLARATION TAG

    The JSP declaration tag is used to declare fields and methods.

    The code written inside the jsp declaration tag is placed outside the service() method of auto

    generated servlet.

  • 1.7. JSP DIRECTIVES

    The jsp directives are messages that tells the web container how to translate a JSP page into the

    corresponding servlet.

    There are three types of directives:

    page directive

    include directive

    taglib directive

    1.page directive-

    The page directive defines attributes that apply to an entire JSP page.

    import contentType extends info buffer language isELIgnored isThreadSafe autoFlush

    session pageEncoding errorPage isErrorPage

    2.include directive-

    The include directive is used to include the contents of any resource it may be jsp file, html file

    or text file. The include directive includes the original content of the included resource at page

    translation time (the jsp page is translated only once so it will be better to include static

    resource).

    Advantage of Include directive

    Code Reusability

    Today is:

    3.taglib directive

    The JSP taglib directive is used to define a tag library that defines many tags.

  • JAVA BEAN

    A Java Bean is a java class that should follow following conventions:

    It should have a no-arg constructor.

    It should be Serializable.

    It should provide methods to set and get the values of the properties, known as getter and setter methods.

    Simple example of java bean class

    //Employee.java

    package mypack;

    public class Employee implements java.io.Serializable{

    private int id;

    private String name;

    public Employee(){}

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

    public int getId(){return id;}

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

    public String getName(){return name;}

    }

    USEBEAN ACTION TAG

    The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the

    Bean class is already created, it doesn't create the bean depending on the scope. But if object

    of bean is not created, it instantiates the bean.

    SYNTAX OF JSP:USEBEAN ACTION TAG

    CUSTOM TAGS

    Custom tags are user-defined tags. They eliminates the possibility of scriptlet tag and separates

    the business logic from the JSP page.

    The same business logic can be used many times by the use of costom tag.

  • Advantages of Custom Tags

    The key advantages of Custom tags are as follows:

    1. Eliminates the need of srciptlet tag The custom tags eliminates the need of scriptlet tag

    which is considered bad programming approach in JSP.

    2. Separation of business logic from JSP The custom tags separate the the business logic

    from the JSP page so that it may be easy to maintain.

    3. Reusability The custom tags makes the possibility to reuse the same business logic again

    and again.

    =For creating any custom tag, we need to follow following steps:

    1. Create the Tag handler class and perform action at the start or at the end of the tag.

    2. Create the Tag Library Descriptor (TLD) file and define tags

    3. Create the JSP file that uses the Custom tag defined in the TLD file

    TAG HANDLER CLASS(MyTagHandler.java)

    public class MyTagHandler extends TagSupport{

    public int doStartTag() throws JspException {

    JspWriter out=pageContext.getOut();//returns the instance of JspWriter

    try{

    out.print("ABC");

    }catch(Exception e){}

    }

    }

    TLD FILE (MyTagHandler.tld)

    today

    upes.bao.MyTagHandler

    JSP FILE(MyTagHandler.jsp)

    NAME:

  • STRUTS

    The Struts framework is used to develop MVC (Model View Controller) based web applications.

    model1 architecture.

    1. Browser sends request for the JSP page

    2. JSP accesses Java Bean and invokes business logic

    3. Java Bean connects to the database and get/save data

    4. Response is sent to the browser which is generated by JSP

    Advantage of Model 1 Architecture

    - Easy and Quick to develop web application

    Disadvantage of Model 1 Architecture

    -Navigation control is decentralized since every page contains the logic to determine the

    next page. If JSP page name is changed that is referred by other pages, we need to

    change it in all the pages that leads to the maintenance problem.

    -Time consuming You need to spend more time to develop custom tags in JSP. So that we

    don't need to use scriptlet tag.

    -Hard to extend It is better for small applications but not for large applications.

    model 2 architecture(MVC architecture)

    Advantage of Model 2 (MVC) Architecture

    Navigation control is centralized Now only controller contains the logic to determine the next page.

  • Easy to maintain

    Easy to extend

    Easy to test

    Better separation of concerns

    Disadvantage of Model 2 (MVC) Architecture

    We need to write the controller code self. If we change the controller code, we need to

    recompile the class and redeploy the application.

    STRUTS validation

    index.jsp

    1.

    2.

    3.

    4.

    5.

    6.

    RegisterAction.java

    public class RegisterAction extends ActionSupport{

    private String name,password;

    6. public void validate() {

    7. if(name.length()

  • 8. addFieldError("name","Name can't be blank");

    9. if(password.length()

  • JPA(JAVA PERSISTANCE API)

    The process of mapping Java objects to database tables and vice versa is called "Object-relational

    mapping" (ORM). The Java Persistence API (JPA) is one possible approach to ORM.

    Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and

    vice versa. JPA permits the developer to work directly with objects rather than with SQL statements.

    The JPA implementation is typically called persistence provider. JPA can be used in Java-EE and Java-

    SE applications.

    The JPA implementation is typically called persistence provider. JPA can be used in Java-EE and Java-

    SE applications. The mapping between Java objects and database tables is defined via persistence

    metadata. The JPA provider will use the persistence metadata information to perform the correct

    database operations. JPA typically defines the metadata via annotations in the Java class.

    Alternatively the metadata can be defined via XML or a combination of both. A XML configuration

    overwrites the annotations. The following description will be based on the usage of annotations. JPA

    defines a SQL-like Query language for static and dynamic queries. Most JPA persistence provider

    offer the option to create automatically the database schema based on the metadata.

    JNDI

    The Java Naming and Directory Interface (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java programming language. It is defined to be independent of any specific directory service implementation.

  • The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services.

    It includes three service providers for the following naming/directory services:

    Lightweight Directory Access Protocol (LDAP)

    Common Object Request Broker Architecture (CORBA) Common Object

    Services (COS) name service Java Remote Method Invocation (RMI)

    JNDI is divided into five packages:

    javax.naming javax.naming.directory javax.naming.event javax.naming.ldap

    javax.naming.spi

    JMS JMS(JAVA MESSAGING SYSTEM), is a messaging system that enables asynchronized distributed communication that is loosely coupled.

    2.1.MESSAGE-ORIENTED MIDDLEWARE (MOM) Message-oriented middleware is a software application or messaging agent that provides facilities for creating, sending, receiving, and reading messages synchronously or asynchronously between system components. A client which produces the message is called a producer and the client which receives the message is called a consumer. All the messages sent are stored in a particular location which is called a destination.

  • 2.2. JMS API The Java Message Service is a Java API that allows applications to create, send, receive, and read messages. The JMS API defines a common set of interfaces and associated semantics that allow programs written in the Java programming language to communicate with other messaging implementations. The JMS API enables communication that is not only loosely coupled but also,

    Asynchronous: A JMS provider can deliver messages to a client as they arrive; a client does not

    have to request messages in order to receive them.

    Reliable: The JMS API can ensure that a message is delivered once and only once. Lower levels of

    reliability are available for applications that can afford to miss messages or to receive duplicate messages.

    2.5.JMS MESSAGING MODELS The JMS API supports two models: Point-to-Point (PTP) Model

    In point-to-point messaging model, a producer creates and sends a message which will be received

    only by a single consumer.

    In point-to-point model, the message destination is called Queue.

    PTP characteristics,

    o Each message has only one consumer. o A sender and a receiver of a message have no timing dependencies. The receiver can fetch the message whether or not it was running when the client sent the message. o The receiver acknowledges the successful processing of a message.

    Publish/Subscribe Model

  • In publish/subscribe model, any number of clients can subscribe to a message destination and

    when a client sends (publishes) a message to the destination, that message is received by all clients who have subscribed to that destination.

    In this model, the message producer is called publisher and the consumers are called subscribers.

    Also the message destination is called Topic.

    Publish/Subscribe model characteristics,

    o Each message can have multiple consumers. o Publishers and subscribers have a timing dependency. A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.

  • EJB

    Enterprise Javabeans(EJB) are special server based components of J2EE architecture that are used for building the

    business logic and data handeling functionality in an application.

    Session Bean(Pic from copy)

    Session Bean are a component of EJB that handle business logic of the application.There are many session beans and

    each handle a subset of business logic.They are altogether responsible for a group of related functionalities.

  • Types-:

    Stateless

    Statefull

    Entity Bean (Pic from copy)

    Entity Beans are JavaBeans backed by a persistent database.They are used to manage data collected from memory and

    data stored to database.An Entity Bean inserts, updates and deletes data while maintaning integrity of the data.

    Types-:

    Component Managed(CMP)

    Bean Managed (BMP)

    2.7.MESSAGE DRIVEN BEAN A message-driven bean (MDB) is an enterprise bean that allows Java EE applications to process messages asynchronously. Message-driven beans have the following characteristics.

    They execute upon receipt of a single client message.

    They are invoked asynchronously.

    They are relatively short-lived.

    They do not represent directly shared data in the database, but they can access and update

    this data.

    They can be transaction-aware.

    They are stateless.


Recommended