Java Web Basics: How JSP works and basic concepts

Source: Internet
Author: User
Tags java web

JSP Introduction
    • The JSP full name is Java Server Pages, and the servlet is a technology defined by sun that is used to develop dynamic Web resources, which solves the problem of complex and difficult to maintain servlet output stream layout. JSP is a perfect blend of HTML template elements and Java code, through the JSP write service-side output becomes as easy as writing HTML pages.
    • While JSP simplifies output typesetting by covering HTML template elements and provides different JSP tags for managing different types of Java code, it also defaults to encapsulating 9 large implicit objects to help improve the efficiency of development.
    • JSP is essentially a servlet, and when a user requests a JSP, the JSP engine (tomcat embedded) automatically translates the JSP code into a servlet and populates the HTML template element and JSP tag content into the service method in the servlet. Output the response stream to the customer through the service method. The servlet engine also defaults to encapsulating the JSP's 9-large implicit object into the service method when the conversion is in progress.
    • Due to the good layout of JSP, the actual development of Web applications is usually in the form of Jsp+servlet , JSP is mainly responsible for the presentation of data, servlet responsible for business logic and data calculation. This approach enables good code decoupling and hierarchical management, and also facilitates parallel development of UI staff and back-end personnel.
JSP works the nature of the JSP is the Java Servlet, which translates the JSP into the appropriate Servlet through the Tomcat JSP engine and accepts and responds to customer requests through the service method.

Example: Tomcat container's own index.jsp corresponding servlet

    • The servlet is generated by default and the contents of the JSP are automatically populated into the _jspservice method
      • Java code is populated directly into the method
      • The HTML template is populated into the method via Out.write ("HTML template/N"), which forms the output stream
    • The _jspservice method has the request & response parameter to automatically receive the request and response objects that Tomcat transmits
    • In addition to request,response as input parameters, the JSP engine also automatically generates references to other objects within the _jspservice method and initializes them, which are called the default 9 large implicit objects of the JSP, and can be referenced directly through the object name in the JSP page:
      1. HttpSession session
      2. Servletcontxt Application
      3. ServletConfig Config
      4. PageContext PageContext The current page context for JSP label pass parameters
      5. The Object page represents the current page
      6. JspWriter equivalent to Printwrite, you can output text directly to the browser
      7. Exception
JSP page composition
    1. All elements that satisfy the HTML specification-jsp template elements (translated directly into Out.write content)
    2. <%%> JSP script fragment, Java code block (Java code literal translation)
    3. <%=%> jsp expression (translated directly into Out.print)
    4. <%----%> JSP Comment, this comment is not displayed in the browser
    5. <%@% > JSP directives, including Page/include/taglib
    6. JSP tags
    7. JSP nine large built-in objects: Request/response/session/application/config/page/out/pagecontext/exception
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"Import= "Java.util.*"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >//defines a class method that can only be defined by JSP declarations, global variables, and static code blocks     Public voidtestjspdeclarition () {System.out.println ("This is a JSP declaration"); }%> <%--jsp Comment, this comment does not output to the browser, in the JSP should use this comment instead of HTML comments <!--Xxx--&gt, to avoid exporting garbage data to the client caused by waste--%>The current system time is:The <%--script expression, which can write code that follows the Java specification, can appear multiple times in the JSP and is translated into a code snippet for the service method in the corresponding servlet--%> <%testjspdeclarition (); Date D=NewDate ();        Out.write (D.tolocalestring ()); String Jspdefindedvar= "Hello,i am a var generated by JSP";  for(inti = 0; I < 2; i++) {    %> <br>test Script fragment syntax and multiple script fragment combinations<%        }    %> <br> <br>View the variable values defined by the previous JSP script fragment:<%--script expression, used to output data to the browser, not end, only for data output--%> <%=jspDefindedVar%> <br> <br>View the parameter variables passed by the servlet:<%=request.getattribute ("Servlettransfervar")%></body>

JSP script FragmentUsed to write and run multiple lines of code in a JSP, which is "translated into" the service method, so you cannot nest methods
    • <%%> can only appear strictly following the Java syntax of the code, each line must be;
    • There can be more than one script fragment in a JSP page, a text can be embedded between different script fragments, HTML tags or other JSP elements, and script fragments from one page to another can be accessed. Essentially, these script fragments are translated into code in the same Jspservice method, and the variables in the method are accessible to each other, but follow the principle of first-defined access.
    • A single script fragment can be incomplete in a JSP, but multiple sequential script fragments must be full Java code.
JSP Declaration
    • <%! all code written in the%> JSP page is translated by default into the Servlet's service method, in addition to the JSP declaration.
    • The Java code in the JSP declaration is translated into the Servlet's service method and is often used to define a static block of code that the JSP translates into a servlet program, a member variable, or a method. JSP script fragments cannot be used to define methods, only JSP declarations can be used to define methods.
    • JSP declaration must meet Java code specification
    • The default nine implicit objects of the JSP are the method variables declared in the Servlet service method, which cannot be used in JSP declarations.
JSP Script ExpressionUsed to output program data to the client<%= variable or expression%>, the expression cannot be followed;. JSP Annotations
    • <%--%>, need to distinguish with HTML comments, in practice should use more JSP comments less HTML comments.
    • <!— —, HTML annotations are parsed into the browser, causing unnecessary junk data to be exported.
JSP directives

JSP directives are designed for the JSP engine, which does not directly produce visible output, but is used to tell the JSP engine how to handle the basic syntax of the jsp,jsp instruction:<%@ directive Property name = "Value"%>.

Common JSP Directives include: page directive/ JSP page directives, typically used for encoding and package management , regardless of where it appears on the page, its scope is the entire page, in practice often placed at the top of the JSP page, the focus includes the following:
    • Language= "Java", programming language is Java
    • Contenttype= "text/html; Charset=utf-8 ", output the character encoding to the browser
    • Pageenconding= "UTF-8", the encoding of the JSP page
    • Import= "", introduced Java package, JSP introduced by default Java package only Java.lang and javax.servlet.*, the rest of the package needs to be used in the JSP page, you need to import in the page directive, otherwise all compiled.
    • Session= "True/false", whether to get the session automatically. Even if this is set to False, the session object can still be obtained by request.getsession ()
    • Buffer= "None/kbsize",
    • Isthreadsafe= "True/false"
    • Autoflush= "True/false"
    • Errorpage= "Relative_url", positioning the page after the failure of the automatic jump page, to avoid throwing 500 of the poor user experience.
JSP include directivesTo combine multiple JSPs, especially for header and footer applications.

Java Web Basics: How JSP works and basic concepts

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.