JSP technology in the eyes of a developer _ MySQL

Source: Internet
Author: User
The assumervertmpages (JSP) technology provides us with a simple method for creating dynamic web pages and simplifies the construction of web programs. This article provides a comprehensive introduction to JSP technology from the perspective of a developer (and provides some JSP instance programs ). Foreword the assumerverpages technology allows web developers and designers to easily create and maintain the assumervertm Pages (JSP) technology, which provides us with a simple method to establish dynamic web Pages, it also simplifies the construction of web programs. This article provides a comprehensive introduction to JSP technology from the perspective of a developer (and provides some JSP instance programs ).
Preface
The assumerver Pages technology allows web developers and designers to easily create and maintain dynamic web Pages, especially the current commercial systems. As part of JavaTM technology, JSP can quickly develop web-based, platform-independent applications. JSP separates the user interface from the system content, allowing the designer to change the layout of the entire webpage without changing the underlying dynamic content.
So what is the assumerver Page? Simply put, a JSP webpage contains executable application code that can generate dynamic content in an HTML webpage. This application may contain JavaBeanTM, JDBCTM objects, Enterprise Java BeanTM (EJB) and Remote Method Invocation (RMI) objects, and all parts can be easily accessed from the JSP webpage. For example, a JSP webpage can contain static text and images displayed in HTML code, or call a JDBC object to access the database. when the webpage is displayed on the user interface, it will contain static HTML content and find the corresponding dynamic information from the database.

In JSP web pages, to separate user interfaces from applications, you can consider performing a very convenient authorization task between web designers and developers. It also allows developers to build flexible code, making it easy to update and reuse. Because JSP pages can be automatically compiled as needed, web designers can change the expression code without re-compiling the application logic. This also makes JSP more flexible than Java servlet (which is an extension of the ever Pages function) to generate dynamic web content.

JSP and Servlet
If you have used Java servlet, you will know that servlet allows you to build a dynamic web page, which contains data obtained from the server's Java object. However, you also need to know that the servlet method for generating web pages is to embed HTML tags and express code in Java classes. This means that you need to modify and recompile the servlet source file to change the expression code. Because the designers who design HTML pages may not be the same person as the developers who write servlet code, updating servlet-based web applications becomes very tricky.

Enter assumerver Page is an extension of Servlet API. In fact, JSP web pages can be used before being compiled into servlet, so they also have all the advantages of servlet, including accessing Java APIs. JSP is embedded into the servlet for the general expression of the application code, so they can be seen as a "thorough" servlet.

JSP Web pages mainly provide a high-level method to establish servlet, which also brings other advantages. Even if you have compiled servlets for web applications, using JSP still has many advantages:

JSP web pages can be easily combined with static templates, including HTML or XML fragments and code that generates dynamic content.

JSP web pages can be dynamically compiled into servlet upon request, so web page designers can easily update the expression code. If necessary, the JSP page can also be pre-compiled.

To call JavaBean components, JSP labels can fully manage these components to prevent Web designers from complicate applications.

Developers can provide customized JSP tag libraries.

Webpage designers can change and edit the fixed Template part of the webpage without affecting the application. Similarly, developers do not need to edit pages one by one, but only need to make reasonable changes to components.

Generally, JSP allows developers to distribute functional applications to many web designers. These designers do not need to know the Java programming language or any servlet code, so they can concentrate on writing HTML code, and programmers can concentrate on building objects and applications.

Create a JSP page
Roughly speaking, JSP and HTML (or XML) web pages are very similar-both contain text encapsulated with tags (defined between "<> ). When HTML tags are processed by your browser to display webpages, JSP Tags can generate dynamic content through web server processing. These JSP labels can define individual operations, such as calling JavaBean using a method or containing standard Java code blocks (also called scriptlet ), these code blocks can be executed when a webpage is accessed.

The declaration is similar to the variable declaration in Java. it is also used to define variables for subsequent expressions or scriptlet needs. The declaration is defined in <%! And %>. In the above example, "int" declares and gives a corresponding value (AM or PM) for the current time ):

<%! Int time = Calendar. getInstance (). get (Calendar. AM_PM); %>

The expression can be a variable or constant that is inserted in the data returned by the web server and defined with <% = and %>. In the above example, the expression will call the JavaBean component and insert the result data to the page:

<% = Clock. getDayOfMonth () %>

<% = Clock. getYear () %>

Scriptlet is embedded into the JSP page as a Java code block. The Scriptlet code is inserted into the servlet generated by the page one by one, which is defined between <% and %>. In the above example, the scriptlet can generate a corresponding time and greeting based on the current user:

<%

If (time = Calendar. AM ){

%>

Good morning

<%

}

Else {

%>

Good afternoon

<%

}

%>

The comments are similar to those in HTML. when executed, the JSP Engine removes the comments. This means that the JSP comment is not returned to the user's browser. Unlike HTML annotations Between tags, JSP comments are defined between <% -- and -- %>. For example:

<% -- Check for AM or PM -- %>
Use customized labels
Although you can embed Java code on the JSP page and execute it on the server, JSP also supports using custom tags to insert dynamic content, it has a mechanism that allows you to insert your own HTML-like tags into JSP pages. In other words, your JSP page can generate dynamic content using the simple tag syntax for inserting Java code. However, custom tags are not very useful.

Creating a custom tag is much more complicated than using a simple scriptlet in a JSP webpage, because it takes several steps to connect your Java component with the JSP code. However, custom tags are easy to distribute and reuse. Custom tags are supported in JSP creation tools.

In the following example, custom tags are used for JSP pages that generate dynamic content. Note: In this example, we no longer need to introduce Java classes, declare variables, or write any Java code:

  

<% @ Taglib uri = "/tlds/menuDB. tld" prefix = "menu" %>

Today's Menu

  

Lunch



<% @ Include file = "lunch_menu.html" %>

  

Our Special of the Day



  

  

From the preceding statement, we can see that this page is much simpler than the previous scriptlet example, because it does not contain the initialization object and execute the corresponding method. However, JSP web page code is only a part. for each custom tag, it also includes the following three components:

(1) web pages that contain custom tags. for example, the preceding code snippet uses the custom tags of insertCatchOfDay. Before using custom tags, you must specify Taglib Directive on the page to provide the location of the tag library descriptor (defined for tags. When you execute custom tags, the web page also typically defines one or more tag attributes (for example, "meal" in this example) to determine the dynamic content.

(2) tag library descriptor. It is an XML file that defines a custom Tag and connects it to the Tag Handler. A Tag library descriptor contains different attributes of a Tag, the name (location) of the relevant Tag Handler, and other information that the JSP Engine needs to process custom tags.

(2) Tag Handler. It is a Java class that performs operations jointly with custom labels. For example, in the insertCatchOfDay label above, Tag Handler is the Java class that executes the database query to obtain the corresponding menu item.

We have seen a JSP page using custom tags. let's take a look at the other two components.

Tag Handler

Tag handler is a Java class similar to servlet. However, the servlet can execute Servlet interfaces and be executed by html get or POST requests. Tag handler can also execute a Tag interface (javax. servlet. jsp. tag) and execute it when the custom Tag is processed by the JSP Engine.

If custom tags contain attributes, tag handler must define these attributes and each get/set method. For example, when defining the tag handler of the custom label above insertCatchOfDay, we must define the "meal" attribute and the get and set methods related to it:

Private String meal = null;

Public void setMeal (String s ){

Meal = s;

}

Public String getMeal (){

Return meal;

}

Tag Library Descriptor

If you are dealing with Java technology all the time and do not know about XML, the tag library descriptor component may seem unfamiliar. But you do not need to worry, because you do not need to learn a new programming language. The tag library descriptor only uses tag syntax similar to HTML to define the names and attributes of custom tags, which is more like defining an object.

The following tag library descriptor defines the insertCatchOfDay tag. Note: This file defines the custom Tag name, attributes, and related Tag Handler classes:

  

  

  

   InsertCatchOfDay

   Com. sun. CatchOfDayHandler

  

Queries menu database for the catch of the day.

  

  

   Meal

  

  

  

Like the name of the defined attribute, the Tag library descriptor can also define the data type and specify its attribute (whether required or not). before the Tag Handler is executed, it allows the JSP Engine to perform some error checks. There are other information. for example, to use JSP to create a tool, the library name and version number can also be included in the tag library.

More examples
In the following example, the first example uses the HTTP request object (HttpServletRequest) on the JSP page) to determine the version of your browser and return the corresponding content from one of the three HTML pages:

  

<% @ Page language = "java" info = "Example JSP #1" %>

  

  

<%! String agent; %>

<%

Agent = request. getHeader ("User-Agent ");

If (agent. startsWith ("Mozilla/4.0 "){

%>

<% -- Return content for 4.0 browsers -- %>

<% @ Include file = "ver4.html" %>

<%

}

Else if (agent. startsWith ("Mozilla/3.0 "){

%>

<% -- Return content for 3.0 browsers -- %>

<% @ Include file = "ver3.html" %>

<%

}

Else {

%>

<% -- Return content for other/unknown browsers -- %>

<% @ Include file = "other.html" %>

<%

}

%>

  

  

Note: this page can be accessed directly without declaring or initializing an HTTP request object. Request and response (HttpServletResponse) objects can be implicitly used in JSP pages. Like servlet, JSP pages can obtain parameter values from HTML forms using request objects.

  

<% @ Page language = "java" info = "Example JSP #2" %>

  

  

<% @ Include file = "header.html" %>

  

<%! String selections [], info; %>

  

Here are your current selections:



<%

  

Selections = request. getParameterValues ("items ");

If (selections! = Null ){

%>


    <%

    For (int x = 0; x <selections. length; x ++ ){

    %>


  • <% = Selections [x] %>: <% = db. getInfo (selections [x]) %>

    <%

    }

    %>
<%

}

Else {

%>

  

(No items selected)



<%

}

%>

  


<% @ Include file = "footer.html" %>

  

  

In this example, after each parameter value is read, the JavaBean component queries the required information. Using Bean in JSP web pages can easily return dynamic web content from the database.

Conclusion
If you are looking for a web program that can easily establish a connection to the server's Java components, the assumerver Page is exactly what you need. In addition to EJBs, RMI, JDBC, and JavaBean, separating HTML code and web programs makes it easier to organize and run JSP web pages. In fact, web designers can create JSP pages without the help of Java developers, so you don't have to worry about creating web pages and writing HTML code anymore.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.