Introduction to JSP. JavaServer Pages (JSP) is a technology based on the Java language and enables the development of dynamic web sites. JSP was. JSP and Servlet in J2EE Architecture. Java Servlet A Java program that extends the functionality of a Web server, generating dynamic content and interacting. Result. • Thanks for ordering Core Web Programming JSP developers still need to know servlet programming in PDF at Java Developer's Connection).
|Language:||English, Spanish, Portuguese|
|Genre:||Business & Career|
|ePub File Size:||23.79 MB|
|PDF File Size:||14.35 MB|
|Distribution:||Free* [*Sign up for free]|
Java Server Pages (JSP) is a server-side programming technology that JSP have access to the entire family of Java APIs, including the JDBC API to access. JSP: The Complete Reference Part I The Web Programming Environment. 1 The Web Marketplace. Part IV JSP and Other Web Components. Servlet and JSP Programming with IBM WebSphere Studio and VisualAge for Java. May SG International Technical Support Organization.
In Java server pages JSP, the execution is much faster compared to other dynamic languages. Java server pages JSP are always compiled before its processed by the server as it reduces the effort of the server to create process. The request is processed by a view layer which is JSP and then to servlet layer which is java servlet and then finally to a model layer class which interacts with the database. It is very much convenient to modify the regular HTML. We can write the servlet code into the JSP. It is only intended for simple inclusions which can use form data and make connections. JSP can also include the database connections into it.
For example, a servlet can query for the major and minor version numbers of the servlet API that is supported by the Web container. The javax.
ServletContext interface is implemented by a service inside the Web container, and accessed by the servlet when necessary. When writing a servlet, you can obtain a reference to the ServletContext via the ServletConfig object that is passed to the servlet when it is initialized. The code in Listing 2.
A very useful feature of the application object is that it enables you to store objects by name. These stored objects are accessible by any JSP that is running as part of the application, and are therefore useful to share data between the pages. The example pages in Listing and Listing illustrate how one page can create an object that is visible to any other page that is in the same application.
The second and third lines of Listing create a String object that contains a message, and line 11 then creates a hyperlink to another page that retrieves the shared object and displays it. The following line 12, then, embeds the retrieved value into the current page: The output from the JSP in Listing is shown in Figure.
The code that is generated when you use the application object is shown in Listing for the first page. In , the second line retrieves the shared object, and the following line embeds its String value into the page: out. The exception Object When writing application software, it is of the utmost importance to consider not just what you expect the user to do, but also what you do not expect! So a Web application does not simply crash, it is important to write code that deals with unexpected situations during execution.
The application programmer needs to decide at design-time what error-handling logic to put into place. There are several ways that you can deal with such circumstances when writing the application code. For example, the C programming language provides a global variable to track errors.
An alternative would be to check a return value from functions, as in the COM programming style, to determine if a function has executed correctly. The downside of using either of the two techniques just mentioned is that there is no way to force the application programmer to use them!
The Java programming language provides an exception-handling mechanism that is compiler-enforced except for exceptions that are subclasses of java. RuntimeException, of course.
This mechanism is extensible in that you can write your own exception classes that are usually subclasses of java. You can then instantiate these objects when necessary, and throw them to indicate that an error has occurred in your application code. The way in which you can provide error handling in JSP is by writing error pages.
You saw the page directive in Table that can have attributes including errorPage and isErrorPage. An error page is simply a Web page that informs users that errors have occurred in a Web application.
During the development process, it is common for many pages to propagate errors to a single error-handling page that can prettify the exception and display it in a way that is helpful during the debugging process.
When the Web application is deployed, it is also common to have multiple pages that use a common error page. This means that the error page should be sophisticated enough to deal with errors from multiple sources, and that the multiple sources are related in such a way that having a common error page is a logical choice The JavaServer Page in Listing uses the page directive on the first line to specify that any exception that occurs during the processing of the page should be passed to the processError.
When you click the Generate Exception! When this value is submitted, the scriptlet throws a java. Any exception could be thrown at this point, including one that you write. A NullPointerException was chosen for this example simply to show the syntax without cluttering the example with extra code As soon as the exception is thrown, it is passed to the processError. The reason for this becomes obvious when you examine the source code for the generated servlet.
NullPointerException ; The first line uses the page directive to indicate that this is an error-handling page and, therefore, the implicit exception object should be made available. This page is to tell you what you did wrong. Bear in mind that it is possible to view the processError. The out Object You can send output back to the client by using the out object.
You will notice from Listing that the out variable is of type JspWriter. This class is a subclass of java.
Writer, and provides a set of print methods that enable you to display primitives and String objects. The class inherits a set of write methods from its parent class, and in many of the examples in this chapter you see that the generated servlet uses both print and write methods.
The print method tends to be used for anything that you embed into the page, whereas the write method is used to output the static text parts of the JSP. However, if you look at the source code for the JspWriterImpl class that ships with Tomcat, you can see that the print methods are implemented by delegating to the write methods. The JSP pages are easier to maintain than Servlet because we can separate designing and development.
It provides some additional features such as Expression Language, Custom Tags, etc. We can use all the features of the 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. In Servlet technology, we mix our business logic with the presentation logic.
The Servlet code needs to be updated and recompiled if we have to change the look and feel of the application.
Moreover, we can use EL, implicit objects, etc. Initialization the container invokes jspInit method. Destroy the container invokes jspDestroy method. After that, Servlet page is compiled by the compiler and gets converted into the class file.