JSP learning notes, servlet & amp; jsp learning notes

Source: Internet
Author: User

JSP learning notes, servlet & jsp learning notes

  • What is JSP?
  • Advantages of JSP
  • Disadvantages of JSP
  • Comparison between JSP and PHP
  • How JSP works
  • Nine built-in JSP objects
  • Three JSP compilation commands
  • Seven action commands of JSP
  • JSP standard tag Library: JSTL
  • EL expression
JSP learning Mind Map 1. What is JSP?JAVA Server Pages (JSP) is a cross-platform dynamic web page technology that embeds JAVA code and JSP tags in static Web Pages ), the JSP engine on the Web server is used to compile and execute embedded JAVA code, and generate page information and return it to the client. Ii. Advantages of JSP(1) Web applications developed by JSP are cross-platform, that is, they can run on Linux and other operating systems. (2) JSP separates webpage logic from Webpage Design and display, and supports Reusable Component-based design, making Web-based application development fast and easy. (3) JSP supports server components. JSP can use sophisticated java beans components to implement complex business functions. (4) JSP has powerful scalability. From only one small. Jar file to Servlet/JSP, to cluster and load balancing by multiple servers, to transaction processing and message processing by multiple applications. (5) scalability of JSP labels. JSP technology allows developers to expand JSP tags and customize the JSP tag library. Therefore, web page makers make full use of the powerful functions of the tag technology compatible with XML, greatly reducing the dependency on the scripting language. the custom tag technology reduces the complexity of making webpages. Iii. disadvantages of JSP(1) The product complexity is greatly increased due to the cross-platform Functions and extreme scaling capabilities. (2) high-efficiency java operation requires a large amount of memory and hard disk space. on the one hand, java runs at high speed through. class file resident memory. on the other hand, hard disk space is also required to store a series. java files and. class file and the corresponding version file. Iv. Comparison between JSP and PHP

 

Project PHP JAVA
Reusability Low High
Development Speed Fast Slow
Easy to maintain Difference Excellent
Portability -Linux Windows, Unix
Unix Security Low High
Development Cost Low High
Multi-layer architecture Difference Excellent
Database Access Inconsistent Interfaces Unified Interfaces
Scalability Difference Excellent
Object-oriented Difference Excellent

 

(1) Compared with PHP, php is the server script language for interpretation and execution. First, PHP is easy to use. The syntax is similar to the C language. JSP should first learn the java syntax and be familiar with some core class libraries, and understand the object-oriented programming method. So java is not as good as php. Java must first be compiled into a bytecode. class file and then interpreted and executed on the Java Virtual Machine. Jsp can generate powerful functions through servlet and JavaBean support. JavaBean is a reusable, cross-platform software component. By using javabean, java code and html can be easily separated, enhancing system functions and software reusability. (2) database access compared to Java access to the database through JDBC, and convenient access to the database through the database drivers provided by different database vendors. Database access interfaces are unified. PHP uses different database access interfaces for different databases, so the versatility of database access code is not strong. (3) compared with the system design architecture, PHP can only implement a simple distributed two-or three-tier architecture, while JAVA is relatively powerful in this respect and can implement a multi-layer network architecture. Using the MVC design model, you can have a more efficient and reasonable system architecture. This makes the system better adaptable to scalability and demand variability, and the more complex the project, the more obvious its advantages. In addition, different layers now have some mature development framework support. Using Java Web development technology, we need to use an object-oriented system design method, while PHP still uses a process-oriented development method. A lot of system analysis and design work is required in the early stage of Java development. (4) cross-platform Java and PHP both have good cross-platform features. Almost all of them can run on Linux, Windows, and other operating systems without any modification. (5) Performance Comparison
  • Arithmetic Operations: JSP is much faster than PHP (PHP is a weak type)
  • String operations: PHP is much faster than JSP (JSP uses Object processing)
  • File Operations: JSP is faster than PHP
  • Database Operations: JSP is much slower than PHP (controversial), but JAVA uses a connection pool and PHP uses a persistent connection (pconnect) with similar performance.
In general, the performance of PHP and JSP is not very different and each has its own advantages.
(6) To sum up, PHP is suitable for rapid development, small and medium application systems, with low development costs and quick response to changing needs. Java is suitable for developing large-scale application systems. It has broad application prospects and is easy to maintain and reusable. In addition, systems with the same functions developed using Java are more expensive than those developed using PHP. Despite the advantages of Java's mathematical computing and database access, the architecture is also quite perfect, but PHP can easily support high-intensity Web access and quickly develop applications, the number of virtual hosts that support PHP is as high as that of normal, making it much easier to develop a website using PHP much faster and easier than developing a website using Java. The advantages of Java are only applicable to the traditional software project-centric development model, while PHP is more suitable for the customer-centric SaaS development model. Therefore, at present, PHP's advantages in Web website development are entirely caused by the particularity of Web website development, which is not determined by the programming language features. V. Working Principles of JSP JSP working principle JSP execution process when the WEB Container (Servlet Engine) receives an access request with a URL extension of. jsp, it will send the access request to the JSP Engine for processing. The JSP Engine in Tomcat is a Servlet program that is responsible for interpreting and executing JSP pages. The specific workflow is as follows:
(1) When each JSP page is accessed for the first time, the JSP Engine translates it into a Servlet Source program. If a syntax error is found in the JSP file during the conversion process, the conversion process will be interrupted, and output error information to the server and client. (2) If the conversion is successful, the JSP engine uses javac to compile the Java source file into the corresponding one. class file and. the class file is loaded into the memory. (3) create an instance of the Servlet and execute the jspInit () method of the Instance (jspInit () method only once in the Servlet lifecycle ). (4) Create and start a new thread and call the jspService () method of the instance, the Servlet container encapsulates browser requests and responses to the browser into HttpServletRequest and HttpServletResponse objects as parameters and passes them to the jspService () method. (For each request, the JSP Engine creates a new thread to process the request. If multiple clients request the JSP file at the same time, the JSP Engine creates multiple threads, and each client request corresponds to one thread ).

(5) After the jspService () method is executed, the HTML content is returned to the client.

(6) If the JSP file is modified, the server recompiles the file. If you need to re-compile, replace the compilation result with the Servlet in the memory and continue the above process. If the system resources are insufficient at any time, the JSP Engine will remove the Servlet from the memory in an uncertain way. In this case, the jspDestroy () method is called first, and the Servlet instance is marked with "Garbage Collection" for processing.

6. Nine built-in JSP objects

(1) built-in Object Features

  • Provided by JSP specifications, without the need for writers to instantiate.
  • Web Container Implementation and Management
  • All JSP pages can be used
  • It can only be used in expressions or code segments of script elements.
(2) Common built-in objects
Category Object
Output input object Request, response, out
Communication Control Object PageContext, session, application
Servlet object
Page, config
Error Handling object Exception

 

Scope Object
Request Request
Session Session
Page Response, out, pageContext, config, page, exception
Application Application,

 

  • Request object: the client's request information is encapsulated in the request object, through which you can understand the customer's requirements and then respond. It is an instance of the HttpServletRequest class.
  • Response object: This object encapsulates the output returned to the HTTP client and provides the page author with a way to set the response header and status code. It is often used to set HTTP headers, add cookies, set the type and status of response content, and send HTTP redirection and encoded URLs.
  • Session Object: Refers to a session between the client and the server, starting from a WebApplication that the client connects to the server until the client is disconnected from the server. It is an instance of the HttpSession class.
  • Out object: an instance of the JspWriter class, which is a common object for outputting content to the client.
  • PageContext object: The pageContext object provides access to all objects and namespaces on the JSP page, that is, it can access the SESSION on this page, you can also take a property value of the application where the page is located, which is equivalent to the aggregator of all functions on the page.
  • Config object: it is used by the JSP Engine to transmit information to a Servlet during initialization. This information includes the parameters used during Servlet initialization (consisting of attribute names and attribute values) and server-related information (by passing a ServletContext object)
  • Page Object: refers to the current JSP page, representing the class object generated by the JSP file that is running
  • Exception object: The exception object generated during JSP file running. This object cannot be directly used in common JSP files, instead, it can only be used in JSP files that use "<% @ page isErrorPage =" true "%>"
  • Application Object: implements data sharing between users and stores global variables. It starts from the start of the server until the server is closed. During this period, this object will always exist. In this way, the object can be connected between users or between users, you can perform operations on the same attribute of this object. Operations on this object attribute anywhere will affect access by other users. The startup and shutdown of the server determine the life of the application object. It is an instance of the ServletContext class.
VII. Three JSP compilation commands

JSP compilation commands are messages that notify the JSP Engine. They take effect when compiling JSP into Servlet. The processing commands can usually be replaced with Java scripts, which is the standardized way of writing JSP scripts. It does not generate output directly. All compilation commands have default values.

Syntax format: <% @ compile command name attribute name = "property value"... %>

(1) page command: this command is for the current page and is valid for the entire page. It is mainly used to set some parameters, such as contentType and language.

(2) include command: used to specify to include another page (static inclusion). Before converting jsp to servlet, execute the content in inclue file first and then convert it, in the end, it will only be converted into a single servlet.

(3) taglib command: used to define and access custom tags.

8. Seven action commands of JSP

The action commands are different from the compilation commands. The compilation commands notify the Servlet engine to process messages, while the action commands are only script actions during running.

(1) forward Command: Used to forward the page response to another page. It can be forwarded to static HTML pages, dynamic JSP pages, or Servlet in the container.

When the forward request is executed, the client's request parameters are not lost.

When the forward Command is executed to forward the request, the address of the user request is not changed, but the page content is completely changed to the content on the target page of the forward.

(2) include command: it is a dynamic include command, which is also used to import a page. It does not import the compilation command of the include page, but simply inserts the body content of the imported page into this page.

The difference between static include and dynamic include: static include refers to the parent page and the contained page. After the code is merged, it is translated into a servlet and fed back to the foreground, form an HTML page (compilation occurs ). Dynamic include is to compile the parent page and the contained page separately, translate it into a servlet separately, and splice it into an HTML page at the front end (when a request occurs ).

(3) useBean command: used to create a BEAN instance on the JSP page and specify its name and scope.

(4) getProperty command: obtains the BEAN property value, converts it to a string, and inserts it into the output page.

(5) setProperty command: used to set the attribute value in Bean.

(6) plugin command: Indicates executing an applet or Bean. If possible, you need to download a Java Plug-in to execute it.

(7) param command: used to set the parameter value.

9. JSP standard tag Library: JSTL

The tag function library of JSP is mainly divided into five categories:

JSTL

Prefix name

URI

Example

Core tag Library

C

Http://java.sun.com/jsp/jstl/core

<C: out>

I18N format tag Library

Fmt

Http://java.sun.com/jsp/jstl/fmt

<Fmt: formatDate>

SQL tag Library

SQL

Http://java.sun.com/jsp/jstl/ SQL

<SQL: query>

XML tag Library

Xml

Http://java.sun.com/jsp/jstl/xml

<X: forEach>

Function tag Library

Fn

Http://java.sun.com/jsp/jstl/functions

<Fn: split>

Core tag Library:

Function category

Tag Name

Expression operation

Out, set, remove, catch

Process Control

If, choose, when, otherwise

Iterative operation

ForEach and forTokens

URL operation

Import, param, url, redirect

10. EL expression

All EL types start with $ {and end.

EL provides the. and [] operators to navigate data. $ {SessionScope. user. sex} equals to $ {sessionScope. user ["sex"]}.

. And [] can also be used together: $ {sessionScope. shoppingCart [0]. price}

 

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.