JSP Scriptlets Instance Tutorial

Source: Internet
Author: User
Tags date now html tags

Scriptlets
We have seen how embedded Java expressions in the JSP page put them between the <% =%> character sequence.
But it's hard to do the show just to put the inside of the Java expression in HTML.

JSP technology can also let you write a block of Java code within the JSP technology. You do this by putting your Java code between the "% and%" characters (just like expressions, but no sign = start order). )

This block of code is called "Scriptlet". itself, a scriptlet will not help any HTML (although it can, as we will see downstairs.) The scriptlet contains Java code that is invoked every time the JSP technology is executed.

The following is a revised version of JSP technology from the previous section, adding Scriptlet.

<HTML>
<BODY>
<%
This is a scriptlet. Notice that "date"
Variable we declare here are available in the
Embedded expression later on.
SYSTEM.OUT.PRINTLN ("Evaluating date Now");
Java.util.Date Date = new Java.util.Date ();
%>
Hello! The time was now <%= date%>
</BODY>
</HTML>

If you run the example above, you will find the output from the "System.out.println" server log. This is a convenient way to do simple debugging (some servers also have technical debugging JSP technology in the IDE.) View your server's files to see if it provides such a technology. )
According to itself is a scriptlet will not produce HTML. If scriptlet is to produce HTML, it can use a variable called "Go". This variable does not need to be announced. It has been in advance for scriptlets, plus some other variables. The following example shows how scriptlet can produce HTML output.

<HTML>
<BODY>
<%
This scriptlet declares and initializes "date"
SYSTEM.OUT.PRINTLN ("Evaluating date Now");
Java.util.Date Date = new Java.util.Date ();
%>
Hello! Www.111cn.net

<%
This scriptlet generates HTML output
Out.println (string.valueof (date));
%>

Here, instead of using an expression, we are creating the HTML directly to print the "Go" variables. In the "Go" variable type Javax.servlet.jsp.JspWriter.
Another very useful predefined variable is "request". This is the type Javax.servlet.http.HttpServletRequest

"Requirements" on server-side processing means the browser and server between transactions. When someone clicks or enters a URL, the browser sends a "please" to the server and displays the returned data. As part of this work "requirements", various data, including file browsers want from the server, if the request is coming from an urgent submit button, the information of the user has entered the form field.

The JSP's "request" variable is used to obtain information from the requesting browser. For example, you can find the name of the client host (if any, otherwise the IP address will be refunded.) Let's modify the code as shown:

<HTML>
<BODY>
<%
This scriptlet declares and initializes "date"
SYSTEM.OUT.PRINTLN ("Evaluating date Now");
Java.util.Date Date = new Java.util.Date ();
%>
Hello! The time is now www.111cn.net

<%
OUT.PRINTLN (date);
OUT.PRINTLN ("<br>your machine ' s address is");
Out.println (Request.getremotehost ());
%>
</BODY>
</HTML>

A similar variable is "reaction". This can be used to influence the response being sent to the browser. For example, you can call Response.sendredirect (Anotherurl), send a response to a browser, and it should load a different URL. This reaction will actually go all the way down the browser. The browser will issue different requirements to "Anotherurl". This is a different from some of the other JSP mechanisms that we will encounter because it includes another page or a forwarded browser to another page.
Practice: Write JSP technology to the output of the entire route, "Hello!" Now is the time ... "But use Scriptlet full string, which includes HTML tags
Related Article

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.