If you are using Java Servlets directly, you will have to handle HTTP input and HTML output in Java classes, and you need rich Java programming experience to build complex applications. The addition of JSP allows you to differentiate HTML's expression logic from the complex business logic embedded in the servlets. This means that the expression layer code can be written by experienced script writers, while advanced Java developers can focus on solving more complex problems in servlets and beans.
Whether you have Java programming knowledge, you can use JSP. The JSP contains some server-side tags that enable you to display dynamic data without having to write a single line of Java code. You can access the bean directly to complete the operation, and then use the JSP tag to display the results as dynamic content. You can also use the servlets to generate the results of the bean,servlets operation, and then use the JSP tag to display the results, also do not need to write Java code in the JSP page.
There are three ways to add Java code to your Web page:
1. Using declarations (declaration), you can define global variables or Java methods that can be accessed anywhere on the page. The declaration is contained in the tag <%!...%>.
2. Using scriptlets (script fragment), you can write any logic needed to process the page, which is included in the <%...%> tag.
3, Expressions (expression), included in the <%=...%>. It provides an easy way to display the results of a Java expression. The expression that is appended will be computed and displayed on the page as if you have explicitly written the numeric value of the result of the operation in your code.
In your own code, you can use some of the implied variables (implicit variables) ――jsp The predefined Java objects provided. In addition, you can include non-Java code modules, such as HTML text from other files, by using the JSP directives (directives).
Let's take a closer look at these scripting elements and use them frequently when writing your own JSP scripts.
directives (instruction)
JSP defines three pages of instructions for setting JSP parameters or expanding code. They are page,include and taglib and must be written on the first line of the JSP page. The syntax is as follows:
<%@ directive attribute= "value" ...%>
The page directive allows you to set some basic parameters for a webpage, including setting the parameters of the scripting language used (default Java), Java classes introduced in your script snippet, setting the output buffer, and so on. The complete page instruction parameter table is shown in the 2.8.1 chapter of JSP Specification Version 1.0 (JSP Specification 1.0).
With the include directive, you can include the contents of other files, such as HTML headers and footers that are stored in separate files.
The taglib directive is used to extend the standard set of JSP tags, which is beyond the scope of this article. However, it is also helpful to understand that JSP defines a way to expand its tag set, which is especially important when you are a software vendor and want to extend the original functionality of the JSP without disrupting its compatibility.
Declarations (statement)
With declarations, you can define methods or variables in the JSP page that can be accessed by other code on the same page. In most cases, you might define a method in your own bean. However, sometimes it may be more convenient to define a method within a Web page, especially when the current code is used only for a single page. Whether you define a method or a variable, the declaration is contained in the <%! Within the%> tag.
Note that the declaration does not produce any output within the JSP page. They are used only for definitions, not for generating output results. To produce an output, you should use a JSP expression or a script fragment.
Expressions (expression)
Expressions is a very simple JSP tag that converts the value of a JSP expression defined in <%=%> to a string and sends the value as a dynamic text. Expression is really a shortcut to generating text, and with it you don't have to call the print () method every time you want to display a piece of dynamic text. A typical application is that you can use expressions to display a simple variable value or a method return value in a bean.
For example, the following code will generate the return value of the GetName () method:
In fact, before generating dynamic output, the JSP must convert the return value of the method to a string object in Java. The JSP specification describes in detail what Java types and Java classes are converted into strings in a JSP expression.
Scriptlets (script fragment)
So far you've learned to use directives to introduce any Java class or Java package, you can define page-level methods or variables and use them on the page, and you can also use implicit variables that provide normal web processing functionality. What you can do with the JSP page is up to you, because you can write any Java code you want in the scriptlets (script snippet) as follows:
<% ... code ...%>
By using the import parameter in the page directive, you can invoke all Java APIs from within the script fragment. Because all of the JSP code you write is actually compiled into the Java servlet, it is a Java class in itself, so the language you use is Java, not any modified or edited version. It's like you can write any code in SSJS. Unlike Ssjs, you have the right to use a rich set of Java APIs in your JSP, so there is little limitation.
Implicit Variables (implied variable)
As mentioned earlier, the JSP defines some implied variables (that is, Java objects) for you to use in expressions and script fragments. Table 2-2 in JSP Specification Version 1.0 lists the implied variables that are available in JSP1.0. Some common objects are listed here:
Out object, type Javax.servlet.jsp.JspWriter, that provides access to the offset method, such as the print () method, to generate output in a script fragment.
The request object corresponds directly to the Javax.servlet.http.HttpServletRequest class in Java and has all the properties and methods of the object of that class. For example, to get an incoming value from an HTML form or URL query string, you can call the Request.getparameter () method and get the parameters by name.
The response object corresponds to the Javax.servlet.http.HttpServletResponse class in Java, providing access to the parameters of the HTML response generated by your Web page. Therefore, to add a value to the HTML response header returned by the JSP page, you can invoke the Response.setheader () method to implement it.
Another simple example
In the following example, let's look at the interaction between a form and its JSP table simple handle. Using the scripting elements discussed earlier, I implemented a simple Web site feedback form (see Figure 2) and a JSP table single handle to validate input and conditionally generate output based on feedback.
The table's simple handle will examine the name and comment Bar to determine that they have been filled out, and if any or two of them are blank, the form handle generates an error message, otherwise it will continue to see if the user's opinion matches the preset string. If it matches, it outputs a specific piece of information; otherwise the output "Thank you".
This example assumes that the user input opinion is "I like cheese." (I like cheese) as you can see in the code, this response is customized for the user who fills out this comment. The table's simple handle will return the page shown in Figure 3:
Fig. 3. Table simple Handle output
The text in the picture: Thank you for your feedback! We also like cheese.
This example is very easy to understand. Even if you're just a JavaScript programmer, you should be able to understand it. I would also like to point out some of the features that are not obvious in the JSP specification that are embodied in this example. First, notice that I defined some methods in the Declarations section (part of <% ' ...%>), exactly as you would define methods in a Java class. This is because the JSP engine transforms these methods into the underlying Java Servlets, which is executed by the server when the browser makes a request to the Web page. Therefore, the definition of any variable and method must conform to the standard Java syntax.
It should also be noted that in the code of my script fragment, I separated a if...else statement that spanned two different pieces of script fragments. It's totally legal! Not only is it legitimate, but the intersection of script snippets and static HTML is a good way to generate HTML conditionally, as I've done in this example.
Finally, you can see that I get the value of the form element and assign it to a temporary variable by calling the Request.getparameter () method. This is the standard method for handling values entered from a form or query string.
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.