This tutorial teaches JSP by progressing from very simple examples to complex examples. Advanced learners may want to do a quick review of the introductory. JSP – This technology is used to create dynamic web application same like Servlet technology Before learning of this tutorial you must be knowledge of Servlet. This tutorial introduces the fundamentals of JavaServer Pages (JSP) technology version and provides an update to the original tutorial.
|Published (Last):||21 August 2017|
|PDF File Size:||2.11 Mb|
|ePub File Size:||4.80 Mb|
|Price:||Free* [*Free Regsitration Required]|
Instead of static contents that are indifferent, Java Servlet was introduced to generate dynamic web contents that are customized according to users’ requests e. It is even worse to maintain or modify that HTML page produced. Programmers, who wrote the servlet, may not be a good graphic designer, while a graphic designer does not understand Java programming. The main page is written in regular HTML, while special tags are provided to insert pieces of Java programming codes. The business programming logic and the presentation are cleanly separated.
This allows the programmers to focus on the business logic, while the web designer to concentrate on the presentation.
JSP is based on Servlet. In fact, we shall see later that a JSP page is internally translated into a Java servlet. Whatever you can’t do in servlet, you can’t do in JSP. JSP is more convenience than servlet for dealing with the presentation, not more powerful. JSP is meant to compliment Servlet, not a replacement. JSPs are used for the view, which deals with presentation. Uspx “Java Servlet”, if necessary. Apache Tomcat Server http: You need to install Tomcat to try out JSP. You can run these examples by launching Tomcat, and issue URL http: First of all, create a new web application aka web context called ” hellojsp ” in Tomcat, by creating a directory ” hellojsp ” under Tomcat’s ” webapps ” directory i.
The file type of “. To run this JSP script, launch the Tomcat server. Check the console message to confirm that hellojsp has been deployed:. This clearly illustrates that JSP like servlets are server-side programs, that are executed ttutorial the server.
The output is then sent to the client browser as the response message. A typical Java servlet as shown below contains three groups of methods: The service methods takes two arguments: A PrintWriter object called out is used for writing out the response message to the client over the network.
The generated servlet for ” first. The relevant part of the generated servlet is extracted below:. Subsequent accesses to the this JSP page will be much faster, because they will be re-directed to the translated tuorial compiled servlet directly no JSP-to-servlet translation and servlet compilation needed againunless the JSP page has been modified. JSP pre-defined seven variables, that are available to the script writer.
Introduction to JavaServer Pages
It is often tutorrial to retrieve the query parameters in the request message. A Writer JspWriter object used to write response message to the client over the network socket, via methods print or println.
You can use session ‘s jwpx to pass information between pages tugorial this session, via methods getAttribute “name” and setAttribute “name”, object. A ServletContext object retrieved via getServletContextwhich maintains information about this web context web application.
You can use the application ‘s attributes to pass information between JSP pages and servlets, via methods getAttribute “name” and setAttribute “name”, object. A ServletConfig object, obtained via getServletConfig. Can be used to access the elements of this page. The translated servlet shows these seven variables are allocated and initialized as follows with re-arrangements:.
In a servlet, you need to use out. Pieces of Java codes written by programmer are embedded into the HTML file to perform the business logic. The presentation and business logic can be cleanly separated. Write the following JSP script and save as ” echo.
This example displays a form with checkboxes, and echos your selections. A JSP expression is used to insert the resultant value of a single Java expression into the response message. The Java expression will be placed inside a out. Hence, the expression will be evaluated and resultant value printed out as part of the response message. Any valid Java expression can be used. There is no semi-colon at the end of the tutoiral. JSP scriptlets allow you to implement more complex programming logic. The Java codes must be syntactically correct, tutoria, Java statements terminated by tutirial semi-colon.
In the translated servlet, the above will be inserted into the service method as follows:. The source codes of scriptlets are only available in the server, and not sent to the client.
That is, scriptlets are safe and secure! JSP declarations can be used to define variables and methods for the class. The syntax of the JSP directive is:. This directive generates proper import statements at the top of the servlet class. The ” page ” directives are also used to set the MIME type, character set of the response message. These information will be written to the response message’s header.
The ” page session ” directive allows tutoral to designate whether this page belongs to the session. The default is “true”. Setting to “false” could reduce the server’s load, if session tracking is not needed in your application.
Other ” page ” directives include: The ” include ” directive lets you insert the unprocessed content of an external file. You can use include directive to include navigation bar, copyright statement, logo, etc. You can use the taglib directive to tell Tomcat what libraries to load and ispx they are. The purpose of JSP actions is to specify activities to be performed when a page is processed.
Creating a JSP Document
There are eight JSP standard actions forwardincludeuseBean, getPropertysetPropertyplugintextelement and five sub-actions attributebodyfallback tuforial, paramand params.
The destination page can ttutorial the new parameters via request. Tomcat clears the output buffer upon executing a forward action. To execute another page and append its output to the current page.
Create a new bean instance. Bean is frequently used in a HTML form, to capture the value of the selected request parameters such as usernameand pass it over to the other processing pages or server-side programs, depending on its scope. Simple type conversion from String will be carried out.
I modified the codes slightly to fit my style. You can run the “number guess” by issuing URL http: Let’s run our examples in web context ” hellojsp ” created earlier.
We now need to create the proper directory structure for a web application and put the files at the right place.
First create a directory ” hellojsp ” under ” webapps “. Take note that the yutorial names are case-sensitive. Write the main page ” numguess. If you need to place codes into the init and destroy methods of the servlet, e. For example, the following JSP script creates a shared database Connection and Statement for accessing a database in the initfor all the requests.
You can use a custom tag in your JSP page as follows:. A TLD file contains tag descriptor s, which map a custom tag to a tag handler class. Create a TLD file called ” mycustomtag. Currently, this TLD file contains no tag descriptor.
Introduction to JavaServer Pages
Starting from JSP 1. Write the Tag Handler Class: Create tutoral simple tag handler class called ” HelloTag. Write a Tag Descriptor: Next, create a tag descriptor for the HelloTagby editing the ” mycustomtag. This is a standalone tag without body content.
The Taglib directive’s attribute ” uri ” locates the TLD ” mycustomtag. Alternatively, tutoeial can specify the TLD filename, i. It also sets the prefix of the custom tag. A simple tag handler implements javax. SimpleTag interface, which declares five abstract methods:. Instead of implementing the SimpleTag interface, you could extend the adaptor class SimpleTagSupportwhich provides default implementation to all the abstract methods, and override the desired methods as in the earlier example.
An optional attribute required is false called ” name ” is declared. It accepts thtorial request-time expression values” rtexprvalue.