Javaweb (i) JSP principle, composition

Source: Internet
Author: User
Tags html comment

JSP (Java sever pages): A technique for developing dynamic Web pages (resources).

A JSP page is a page that runs on a server, and a JSP page is a class. Writing a JSP is like writing html,jsp allows Java code to be written on the page, and allows developers to get web-like request,response such as the page to interact with the browser.

JSP principle:

the execution process of the JSP:

(1) The client sends a request (requests);
(2) JSP Container to translate JSP into servlet source code;
(3) The generated servlet source code is compiled and loaded into memory execution;
(4) Send the result response (response) to the client.

There is little difference between JSP and servlet execution, but it is only necessary to compile the first time the JSP page executes.
Most people will assume that the performance of JSP and servlet is much different, in fact, the performance difference in execution only in the first time. Since the JSP is executed for the first time, it will be compiled into the servlet's class file, which is xxx.class, and when repeated calls are executed, the servlet generated for the first time is executed without having to re-compile the JSP into a servlet. So, in addition to the first compilation, it takes a long time for the JSP and servlet to execute almost the same speed.
When executing a JSP Web page, it is usually divided into two periods: the translation period (translation time) and the request period (requested time).

1. The JSP file is first translated by the server into a Java file (Servlet), and the translated Java file in Tomcat is in the application directory of the corresponding name in Work/catalina/localhost under Tomcat.
2. Compiling into a Java (Servlet) file
3. Run the. class file
Tomcat Global configuration file $catalina_home/conf/web.xml
The Web. XML in the Conf directory under Tomcat is the server global configuration file, where you want to close the list display called
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
Modified to True

the components of a JSP:

1, instruction
Page directive

<%@ page ..........%>

The include directive is introduced statically, introducing other Web pages, which are equivalent to composing one of two pages. A request object that is public.

Page jumps can be added directly behind the page? The variable name = value (test.jsp?name=admin) is passed as a parameter, and this static inclusion is not available.
TAGLIB directive---standard tag library (JSTL), taglib directives for importing tag libraries in JSP pages

ErrorPage---Define the page to jump when the current page goes wrong--if the current page is wrong, jump to that page, note that there is iserrorpage= "true" in the alert interface where the error occurred

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "errorpage=" error.jsp "%>
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"Iserrorpage="true"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><H1>Alert page with Error</H1><H3><%=exception.getmessage ()%></H3></Body></HTML>

2, expression
<%= Variable name%>

<%=sum%>

3, Script
<% Code Content%>

<% for        (int i=1;i<=5;i++) {        sum=sum*i;    } %>

The Java statement for a single script fragment can be incomplete, but the combined Java statement in the entire page must be complete.

4, statement
<%! Code Declaration%>

Can be used to define a static block of code, member methods, and variables that a JSP page translates into a servlet program. Can be defined individually in a declaration, or in the same declaration. Nine large built-in objects cannot be used in a declaration.

<%! int sum=1; %>

5, Notes

JSP annotations: <%--...--%> the conversion phase disappears and can only be seen by developers

Java annotations://,/**/,/***/, compile phase disappears

HTML comment:<!--...--, will not disappear, can also be seen in the page

6, some static content

The HTML content in the JSP is called the template element, and the JSP template element defines the basic skeleton of the Web page, which defines the structure and appearance of the page.

Javaweb (i) JSP principle, composition

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.