Technology
http://www.bratandblekning.com Top Blogs JSP ~ JAVA TUTORIAL

JSP

What Is a JSP Page?

A JSP web page is a written text papers that contains two kinds of text: fixed information, which can be indicated in any text-based framework (such as HTML, SVG, WML, and XML), and JSP elements, which build powerful material.
The suggested data file expansion for the source data file of a JSP web page is .jsp. The site can be consisting of a top data file that contains other data files that contain either a finish JSP web page or a fragment of a JSP web page. The suggested expansion for the source data file of a fragment of a JSP web page is .jspf.
The JSP elements in a JSP web page can be indicated in two syntaxes, conventional and XML, though any given data file can use only one framework. A JSP web page in XML framework is an XML papers and can be controlled by resources and APIs for XML records. 

A Simple JSP Page Example

The website in Figure out 5-1 is a kind that allows you to decide on a location and reveals the time frame in a way appropriate to the location.

Figure 5-1 Nearby Date Form

The source rule for this example is in the tut-install/javaeetutorial5/examples/web/date/ listing. The JSP web page,index.jsp, seems to be below; it is a common combination of fixed HTML markup and JSP elements. If you have developed websites, you are probably acquainted with the HTML papers framework claims (<head>, <body>, and so on) and the HTML claims that make a kind (<form>) and a selection (<select>).
The collections in strong in the example rule contain the following kinds of JSP constructs:
A web page instruction (<%@page ... %>) places the material kind came back by the web page.
Tag collection directives (<%@taglib ... %>) transfer customized tag collections.
jsp:useBean is a conventional factor that makes products containing a selection of places and initializes an identifier that factors to that product.
JSP appearance terminology appearance (${ }) recover the value of product qualities. The are used to set customized tag feature principles and make powerful material.
Custom labels set a varying (c:set), iterate over a selection of location titles (c:forEach), and conditionally place HTML written text into the reaction (c:if, c:choose, c:when, c:otherwise).
jsp:setProperty is another conventional factor that places the value of products residence.
A operate (f:equals) assessments the equal rights of an feature and the present product of a selection. (A built-in == owner is usually used to analyze equal rights.)
Here is the JSP page:
<%@ web page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core
"
         prefix="c" %>
<%@ taglib uri="/functions" prefix="f" %>
<html>
<head><title>Localized Dates</title></head>
<body bgcolor="white">
<jsp:useBean id="locales" scope="application"
    class="mypkg.MyLocales"/>

<form name="localeForm" action="index.jsp" method="post">
<c:set var="selectedLocaleString" value="${param.locale}" />
<c:set var="selectedFlag"
     value="${!empty selectedLocaleString}" />
<b>Locale:</b>
<select name=locale>
<c:forEach var="localeString" items="${locales.localeNames}" >
<c:choose>
    <c:when test="${selectedFlag}">
        <c:choose>
            <c:when
                 test="${f:equals(selectedLocaleString, localeString)}" >
                <option selected>${localeString}</option>
            </c:when>
            <c:otherwise>
                <option>${localeString}</option>
            </c:otherwise>
        </c:choose>
    </c:when>
    <c:otherwise>
        <option>${localeString}</option>
    </c:otherwise>
</c:choose>
</c:forEach>
</select>
<input type="submit" name="Submit" value="Get Date">
</form>

<c:if test="${selectedFlag}" >
    <jsp:setProperty name="locales"
        property="selectedLocaleString"
        value="${selectedLocaleString}" />
    <jsp:useBean id="date" class="mypkg.MyDate"/>
    <jsp:setProperty name="date" property="locale"
        value="${locales.selectedLocale}"/>
    <b>Date: </b>${date.date}</c:if>
</body>
</html>
To set up the time frame program with NetBeans IDE, adhere to these steps:
Start the Application Hosting server.
In NetBeans IDE, select File→Open Venture.
In the Begin Venture discussion, get around to:
tut-install/javaeetutorial5/examples/web/
Select the time frame directory.
Select the Begin as Primary Venture examine box.
Click Begin Venture.
In the Projects tab, right-click the time frame project, and select Undeploy and Deploy.
To set up the time frame program with the Ant device, adhere to these steps:
In a international airport screen, go to tut-install/javaeetutorial5/examples/web/date/.
Type ant. This management will create any necessary compilations, duplicate data files to the tut-install/javaeetutorial5/examples/web/date/build/ listing, and make a WAR data file.
Start the Application Hosting server.
Type ant set up.
To run the example, do the following:
Set the personality development in your web browser to UTF-8.
Open your web browser to http://localhost:8080/date.
You will see a combination box whose records are places. Select a location and just click Get Date. You will see the time frame indicated in a way appropriate for that location.
Some of the figures might not show effectively if you do not have the appropriate terminology data files set up on your device. Seek advice from the customer information or online help for your os to find out how you can set up these terminology data files.

The Example JSP Pages
To demonstrate JSP technological innovation, this section rewrites each servlet in the Duke’s Guide store program offered in The Example Servlets as a JSP web page (see Desk 5-1).

Table 5-1 Duke’s Guide store Example JSP Pages
Function
JSP Pages
Enter the bookstore.
bookstore.jsp
Create the bookstore advertising.
banner.jsp
Browse the guides offered on the market.
bookcatalog.jsp
Add a magazine to the purchasing trolley solution application.
bookcatalog.jsp and bookdetails.jsp
Get information on a particular book.
bookdetails.jsp
Display the purchasing trolley solution application.
bookshowcart.jsp
Remove one or more guides from the purchasing trolley solution application.
bookshowcart.jsp
Buy the guides in the purchasing trolley solution application.
bookcashier.jsp
Receive an recommendation for the buy.
bookreceipt.jsp
The information for the bookstore program is still managed in a data source and is utilized through tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDBAO.java. However, the JSP webpages accessibility BookDBAO through the JavaBeans element tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java. This category allows the JSP webpages to use JSP elements developed to work with JavaBeans elements (see JavaBeans Component Style Conventions).
The execution of the data source vegetable follows. The vegetable has two example variables: the present book and the information accessibility product.
package database;
public category BookDB {
    personal Sequence bookId = "0";
    personal BookDBAO data source = null;

    community BookDB () brings Exemption {
    }
    community gap setBookId(String bookId) {
        this.bookId = bookId;
    }
    community gap setDatabase(BookDAO database) {
        this.database = database;
    }
    community Guide getBook()
         brings Exemption {
        come back (Book)database.getBook(bookId);
    }
    ...
}
This edition of the Duke’s Guide store program is structured along the Model-View-Controller (MVC) framework. The MVC framework is a commonly used structural strategy for entertaining programs that markets performance among program things so as to reduce the level of combining between the things. To accomplish this, it separates programs into three layers: design, perspective, and owner. Each part manages particular tasks and has obligations to the other layers:
The design symbolizes company information, along with company reasoning or features that regulate accessibility and adjustment of this company information. The style informs opinions when it changes and allows the perspective question the design about its condition. It also allows the owner accessibility program performance exemplified by the design. In the Duke’s Guide store program, the purchasing trolley solution application and data source accessibility product contain the company reasoning for the program.
The perspective provides the material of a design. It gets information from the design and recognizes how that information should be offered. It up-dates information demonstration when the design changes. A perspective also sends customer feedback to a owner. The Duke’s Guide store JSP webpages framework the information saved in the session-scoped purchasing trolley solution application and the page-scoped data source vegetable.
The owner describes program activities. It dispatches customer demands and chooses opinions for demonstration. It translates customer information and charts them into activities to be conducted by the design. In a web program, customer information are HTTP GET and POST demands. A owner chooses the next perspective to show in accordance with the customer communications and the result of the design features. In the Duke’s Guide store program, the Dispatcher servlet is the owner. It investigates the demand URL, makes and initializes a session-scoped JavaBeans element (the purchasing cart), and dispatches demands to perspective JSP webpages.
Note - When applied in a web program, the MVC framework is often termed as a Model-2 framework.
In inclusion, this edition of the program uses several customized labels from the JavaServer Pages Standard Tag Library (JSTL):
c:if, c:choose, c:when, and c:otherwise for circulation control
c:set for establishing scoped variables
c:url for development URLs
fmt:message, fmt:formatNumber, and fmt:formatDate for offering locale-sensitive information, figures, and dates
Custom labels are the recommended procedure for doing a extensive range of powerful handling tasks, such as obtaining data source, using business solutions such as e-mail and internet directories, and applying circulation management. In previously editions of JSP technological innovation, such tasks were conducted with JavaBeans elements together with scripting elements. Although still available in JSP 2.0 technological innovation, scripting elements makes JSP webpages more challenging to sustain because they mix demonstration and reasoning, something that is frustrated in web page design. 
Finally, this edition of the example contains an applet to produce a powerful electronic time in the advertising.
To set up and run the program using NetBeans IDE, adhere to these steps:
Perform all the features described in Accessing Databases from Web Applications.
In NetBeans IDE, select File→Open Venture.
In the Begin Venture discussion, get around to:
tut-install/javaeetutorial5/examples/web/
Select the bookstore2 directory.
Select the Begin as Primary Venture examine box and the Begin Needed Projects examine box.
Click Begin Venture.
In the Projects tab, right-click the bookstore2 project, and select Undeploy and Deploy.
To run the program, open the bookstore URL http://localhost:8080/bookstore2/books/bookstore.
To set up and run the program using Ant, adhere to these steps:
In a international airport screen, go to tut-install/javaeetutorial5/examples/web/bookstore2/.
Type ant. This management will create any necessary compilations, duplicate data files to the tut-install/javaeetutorial5/examples/web/bookstore2/build/ listing, and make a WAR data file and duplicate it to thetut-install/javaeetutorial5/examples/web/bookstore2/dist/ listing.
Start the Application Hosting server.
Perform all the features described in Developing a Data Source in the Application Hosting server.
To set up the example, kind ant set up. The set up focus on results a URL for operating the program. Neglect this URL, and instead use the one proven in the next phase.
To run the program, open the bookstore URL http://localhost:8080/bookstore2/books/bookstore.
To understand how to set up the example, make reference to the implementation descriptor (the web.xml file), such as the following configurations:
A display-name factor that recognizes the name that resources use to recognize the program.
A context-param factor that recognizes the JSTL source program platform name.
A audience factor that recognizes the ContextListener category used to make and eliminate the data source accessibility.
A servlet factor that recognizes the Dispatcher servlet example.
A set of servlet-mapping elements that map Dispatcher to URL styles for each of the JSP webpages in the program.
Nested within a jsp-config factor are two jsp-property-group elements, which determine the preludes and coda to be involved in each web page. 
Figure 5-2 reveals the bookcatalog.jsp web page from the Duke’s Guide store program. This web page reveals a record of all the guides that are available for buy.

Figure 5-2 Guide Catalog
Screen capture of Duke's Bookstore book catalog, with titles, authors, prices, and
If the information in your webpages appear as post of the kind ??? Key ???, the likely cause is that you have not offered the appropriate source program platform name as a perspective parameter.

No comments:

Post a Comment