J2EE technical specifications (iv) -- JSP Basics

Source: Internet
Author: User

I. jsp Overview

A. Dynamic Content Template

B. Use custom Java code to expand html

C. compiled into servlets

D. divide the work into content production and program development

(1) jsp request processing

JSP requests are processed by Weblogic Server servlet named jspservlet, including:

A. Convert JSP into servlet

B. Compile the converted servlet into a class file.

C. Execute JSP

(2) Application Design Model

A. jsp specifications describe some application design models

B. The four models are:

A. Simple Model (Simple Model)

B. Forward Delegation Model

C. Include Delegation Model

D. decouple Model

Ii. jsp syntax

There are two JSP syntax forms:

(1) standard syntax

(2) XML syntax

Iii. jsp scriptlets

(1) Use <% and %> to add scriptlets containing executable Java code

(2) sciptlets can use Stream Control

(3) All sciptlets in a given JSP must be a complete Java code logical block.

Let's take a look at two examples:

Example of If statement:

<% if (userIsLogged){%>  <H1>Welcome</H1>you are logged in <%}else{%>  <H1>Waring</H1>you must log in<%}%>

Browser output:

JSP will greet the user based on whether the user is logged on

For example:

<% for(int j=3;j<=3;j<=1;j--){ %>  <H<%=j%>>jsps are great!!!    </H<%=j%>><%}%>

Browser output:

JSPs are great !!!

JSPs are great !!!

JSPs are great !!!

Iv. jsp expressions (expression)

(1) Use <% = and %> to execute Java expressions

(2) Any Java expression is valid.

(3) The expressions content is usually returned to the client.

Expressions references variables, methods, and objects:

V. jsp declarations (Declaration)

(1) Use <%! And %> declare class-level variables and Methods

(2) This can be referenced by the entire JSP code.

Example of declaring variables and methods:

<%! Double [] Price = {12.3, 23.4, 34.5}; // declare a double array and return the doubles Method Double gettotal () {double Total = 0.0; for (Int J = 0; j <prices. length; j ++) {total + = Prices [J]; // This can reference return total from expressions, scriptlets, and other declarations; }}%>

5. jsp actions

(1) Action is the JSP Command executed during the request.

(2) The following table lists the six Action tags for JSP extension.

Example of instantiating a JavaBean using usebean action:

<jsp:useBean id="shoppingCart" class="com.bea.Shoopingcart"/><% shoppingCart.addItem(new CD("U2"));%><%=shoppingCart.getCount()%>

Vi. jsp ctictives

(1) Directives is the JSP Command executed during compilation.

(2) The following three direves VES can change JSP Behavior

Directives example:

<% @ Page import = "mypackge. myclassa "%> references a class <% @ page errorpage =" myerrowpage. JSP "%> when an exception occurs, the JSP page <% PAGE extends =" myjspsuperclass "%> the class to be inherited <% @ include file =" myheaderfile.html "%> contains a header file <% @ taglib uri = mycustomtags. TLD prefix = custom %> load custom tags

(3) Eight built-in objects:

A. The request object encapsulates the data from the client to the server. The data includes the data submitted by the customer and some client configuration information. For example, the customer's IP address and host name. The getattribute method and setattribute method pages are used to obtain server data and add data to the request. You can use getparameter to view request parameters. You can also view the request type and HTTP header of the request.
B. Response encapsulation of the respose object from the server to the client. It is used to write server content to the client. The getwriter method is commonly used. Print data on the client.
C. The out object is used to print data to the client. It is like a response pen. Execute a specific print action.
D. The session object provides sessions for the server and client. A session is like a conversation. The dialog content is the user's status and authentication information.
E. Application object. Is a public session. It is opposite to session. Session is unique to each user. Application is the data shared by these users.
F. config provides some servlet configuration information. Valid only on the current page.
G. pagecontext provides all the attributes and methods required for JSP program execution. For example, session application config out. Valid range: Current page. This is like a large container of these objects.
H. Page is equivalent to this in the Java class. It is of little use. Indicates the current page.

(4) use built-in objects

A. httpservletrequest

<% String account=request.getParameter("accountNumber");%>Your account number is<%=account%>

B. servletcontext

<% String table=application.getInitParameter(tableName);   String sql="SELECT * FROM"+tableName;   ResultSet rs=Statement.executeQuery(sql);%>

C. servletconfig

<p> This Web Application requires Version<%=config.getInitParameter("WLS-Version")%>To run correctly.<p>

(5) scope of the object

A. pagecontext, request, session, and application objects all provide methods for storing objects within the scope of their functions.

B. Scope defines the access between their inventory cycle and JSPs

Object
Life Cycle
Where to access
Pagecontext
Until response is returned to the user or request is passed to a new page Current page
Request Until response is returned to the user Current page and all included or transferred pages
Session Same as the user's session Current request and any subsequent requests from the browser (within the session survival time)
Application Web application Lifecycle Current and all subsequent requests to the same web application

(6) Take objects in the memory scope

A. Provide the following methods to access objects within the scope of the objects:

A. setattribute (string, object) associates an object with a string and can be read later.

B. Object getattribute (string) reads the object by name

C. Enumeration getattributenames () gets the names of all objects in the scope of action.

D. Void removeattribute (string) delete an attribute.

(7) share requests between JSP pages

A. Several JSP pages can use the request object to share objects between JSP pages to process a request.

Sample jsp1.jsp for storing objects in the request context:

<% Employee employee1=new Employee();       employee1.setName("John Doe");       request.setAttribute("John",employee);%>  <jsp:forward page="jsp2.jsp"/>

Sample jsp2.jsp for retrieving objects from the request context:

<%=((Employee)request.getAttribute("John").getName())%>

(8) use JavaBeans in JSP

A. the business logic or model data can be implemented with JavaBean outside of JSP.

B. JavaBeans follow the following rules:

A. A non-variable Constructor

B. Each attribute has the SER and get methods.

C. serializable

A simple example of constructing the employan of employee:

public class EmployeeJavaBean implementsSerializable{String name;double salary;public EmployeeJavaBean(){name=null;salary=0.0}public void setName(String n){name=n;}public String getName(){return name;}public void setSalary(double s){salary=s;}public double getSalary(){return salary;}}

C. Use usebean in JSP to create a JavaBean instance

D. Access the variables of JavaBean by using serproperty and getproperty:

Examples of instantiating, initializing, and using the JavaBean Tag:

<jsp:useBean id="employee"  class="mypack.EmployeeJavaBean"> <jsp:setProperty name="employee" property="name"  value="John Doe"/></jsp:useBean><jsp:setProperty name="employee" property="salary"  value="20000"/>Employee:<jsp:getProperty name="employee"  property="name">

You can also use the get and set methods as follows:

<% employee.setSalary(employee.getSalary()+10000);%>salary<%=employee.getSalary()%>

E. By default, your JavaBean can be stored in the memory of the page.

F. You can use the scope attribute to set its range to page, request, session, or application.

Sample jsp1.jsp for storing objects in the request context:

<jsp:useBean id="employee"  class="myPack.EmployeeJavaBean"  scope="request"/><jsp:forward page="jsp2.jsp"/>

Sample jsp2.jsp for retrieving objects from the request context:

<jsp:useBean id="employee"class="mypack.EmployeeJavaBean"scope="request"/>

We will continue to improve the content of JSP in the following blog.

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.