Javaweb Series nine (Jsp,javabean,el expression)

Source: Internet
Author: User
Tags html comment tag name to domain

1.jsp:java server Pages,java servers-side pages
Sun provides technology servlet JSP to develop dynamic websites
The JSP will be compiled into a servlet and will be put into the Tomcata work directory
JSP contains: Html+java code +jsp self-instruction
There are three ways of doing this:
<%! %>
<%%>
<%=%>
Topic
<%=x%>//to output the value of the member variable X
<% x=5; %>//assigning a member x variable to a value of 5
<% int x = 10; %>//defines an X value of 10 in the service method;
<% this.x = 20; %>//assigning the value of member variable x to 20
<%=x%>//the variable x output defined in the method
<%! int x = 0; %>//Define a member variable x value is 0
a.0 b.10 c.0 d.20 0
The end result is 0 10
2.jsp annotations
Jsp:java server Pages,java servers-side pages
In the JSP can write HTML comments, you can write Java comments, you can write the JSP itself comments
Three types of annotations during JSP execution, there will be
< three annotations for ><%--JSP annotations--%>java!----
After JSP is compiled into Seervlet, HTML exists, Java annotations exist, but JSP annotations do not exist
The servlet appears on the page, the HTML comment is present, and the Java comment and the JSP comment are gone.
<!--HTML comments--
<!--java Annotations--
<%
Single-line Comment
/* Multiline Comment
*/
/** Document Comments
*/
%>
<!--JSP comments--
<%--jsp's comment--%>
Instructions for 3.jsp
Syntax: <%@ directive's name attribute = "Property value"%>
The quality of the JSP contains three instructions:
Page: Set the information for the JSP page, <%@ page property = "Property value"%>
Properties in the page directive:
Language: Supported language types, now only Java,
Extends:jsp which class the servlet compiled into inherits: Default Httpjspbase
Session: Whether the session object can be used directly inside the JSP, the session property value is True | False
Import: Pour into a package that can be written together separated by commas
Packages can be imported separately
<%@ page import= "Java.util.Date"%>
<%@ page import= "Java.util.ArrayList"%>
<%@ page import= "Java.util.List"%>
can be written together
<%@ page import= "Java.util.list,java.util.arraylist"%>
Buffer: The output buffers size 8kb;
Autofulsh: The buffer is out of normal use
Erropage: If there is an exception to the current JSP page, go to the error page errorpage= "error page"
Iserropage: In the error page, you can display an error message. This property is usually used when the programmer is debugging the program, and when the program is given to the client,
This property must be set to False
The value True and False, the default value is False, and the following code is not generated
When you set the value of this property to true on the error page, a few lines of code are inside the generated servlet
Java.lang.Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable (request);
if (Exception! = null) {
Response.setstatus (Javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
Contenttype:jsp eventually compiled into a servlet, eventually using the browser encoding
Pageencoding:jsp compiled into a servlet, setting the encoding used
Iselignored:jsp inside dead Ignore El expression, change to True, write El expression, treat it as an expression
INCLUDE:JSP contains other JSP pages,
JSP contains other JSP page wording: <%@ include file= "included pages" > included pages do not need to write other content, as long as the content is written, and eventually only compiled into a servlet
Statically contained procedures, which are equivalent to code replication:

Contains a parameter that cannot be passed, cannot be a variable, only needs to write the contents of the file
Taglib: Introducing Tag Libraries
Alternative <%%> notation: <%@ tagilb= "tag library" prefix= "tag name"%>
4.jsp Built-in objects
Objects that can be used directly in the JSP
Resquest:httpservletrequest inside represents the request, gets the submitted parameter, is a domain object, sets the value and the value
Response: Equivalent to HttpServletResponse, representing the response, outputting content to the page
Session: Equivalent to the httpsession inside the servlet, also the domain object, scope session
Congfig: Equivalent to ServletConfig, gets the servlet name, gets initialization parameters
Application: Equivalent to servlet inside ServletContext, domain object, scope entire Web project, get global initialization parameters
Exception: Get exception Rejoice, GetMessage method, need to set Iserrorpage to True
Page: As a running Servlet object
Out: Output content to the page, Out.write () code, out using the Jspwrite object, using the Printwrite object with Reaponse.getwrite (), or output to the page
Output mechanism: First generate two buffers, one is out buffer, one is response buffer, out is not directly output, first put the content into the out buffer, and then put into the response buffer, and finally through the buffer want to page output

PageContext:
Parent class Jspcontext
PageContext: can only be used in JSP, servlet there is no concept of the page
Scope of Action: current JSP page
Set Value SetAttribute (java.lang.String name, java.lang.Object value)
Get value getattribute (java.lang.String name)
Code
<!--set values to the PageContext field
<%
Pagecontext.setattribute ("Page1", "Itcastpage1");
%>
<!--get it in traditional ways
<%=pagecontext.getattribute ("Page1")%>
<!--using the EL Expression-
${page1}
PageContext: You can set values to other domain objects
SetAttribute (name,value,scope) First parameter: Name second parameter: value third parameter: domain name
PageContext: You can get the value 1.${name in other domain objects} to get the value 2. Two parameters: The first parameter is the name of the domain object, and the second parameter is which domain object
Findattribute (): Indicates the smallest value inside the domain object
PageContext: You can get the other eight built-in objects: Pagecontext.getseesion
Four domain objects
PageContext: The scope is a JSP page, this object can only be used in JSP
Request: A range of requests, often used in conjunction with forwarding
Session: Scope one conversation
Application: Scope entire Web project
All four domain objects use setattribute to set the value, using GetAttribute to get the value
Action Label for 5.JSP
There are 6 action tags in the JSP:
<jsp: Dynamic Tag Name attribute = "attribute value" ></jsp: Dynamic Tag name >
<jsp:forward>: Represents the forwarded action
-Jsp:forward page= "forward to Path without project name" ></jsp:forward>
-<!--Use the forward action tag inside the JSP--
<jsp:forward page= "/jsp/hello.jsp" ></jsp:forward>

<jsp:include>: Represents the containing

<jsp:param>: Indicates page pass parameters
* Code <!--Use the forward action tag inside the JSP--
<!--equivalent to hello.jsp?id11=itcast200
<jsp:forward page= "/jsp/hello.jsp" >
<jsp:param value= "itcast200" name= "Id11"/>
</jsp:forward>
Second class: Action label for Operation JavaBean
<jsp:usebean>: Operation which JavaBean
<jsp:setProperty> Set properties for JavaBean
<jsp:getProperty> get the properties of JavaBean
Introduction to 6.JavaBean
JavaBean is a Java class, but follows a certain writing specification.
Code to follow: there must be a public constructor that has no parameters. JavaBean Private, attributes must be exposed through public methods
function of JavaBean: stored value and value, encapsulating data
Using traditional methods to access data,
When encapsulating data using JSP action tags, the name value of the form entry is the same
7.javabean introspection (complete the JavaBean package)
Complete encapsulation of JavaBean data
Use to Class
Introspector:
Method
BeanInfo Getbeaninfo (class<?> beanclass): Get the contents of JavaBean
Parameter class class, JavaBean byte-code file,
There are three ways to get class class names. Class object. GetClass () class.forname ("Package Classpath")
BeanInfo
Propertydescriptor[] Getpropertydescriptors (): Gets the property description, returns the array
PropertyDescriptor
Method Getreadmethod (): Means of reading, JavaBean the Getxx method inside
Method Getwritemethod (): Means of writing, JavaBean the Setxx method inside
Method
Invoke (Object obj, Object ... args): Let the method execute
Use of the 8.BeanUtil framework
Role: Encapsulating data to JavaBean
is a small operational JavaBean API developed by the Apache organization
If you want to use Beanutils, you must import a supported jar package (import two jar packages)
How to import a jar package inside a Web project
Copy the jar package that needs to be imported into the Webroot--web-inf--lib directory and import the jar package into the project automatically.
Note: The value of the form entry name must be the same as the property name inside the JavaBean
Use the class org.apache.commons.beanutils below
Beanutils
Populate (Object Bean, Map properties): Complete encapsulation of the data
Two parameters: The first parameter is the JavaBean object, the second parameter forms the input value and the input value, the map collection
Create a JSP page, write a submit form, submit the form to the servlet, and use the framework to encapsulate the data to the JavaBean operation within the servlet
Code
Get the data submitted by the form
Create a Javabe object
Using Frameworks for encapsulation
Person p12 = new person ();
try {
Beanutils.populate (P12, Request.getparametermap ());
} catch (Exception e) {
E.printstacktrace ();
}
Using the framework, if the type of the property is the base data type and the string type finishes encapsulation, but if the exception does not occur for these types to be converted
Convertutils
Register (Converter Converter, Class clazz)
Two parameters: The first parameter is the converter, the second parameter is the conversion type class
string D1 = (string) o;
Converts the date type of a string to a date type
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = null;
try {
Date = Format.parse (D1);
} catch (ParseException e) {
E.printstacktrace ();
}
return date;
9.el-expression
Purpose: Gets the value inside the domain object
Syntax: ${the name of the domain object}
(1) Use El expression to get the value of a domain object
Code
<!--set values to four domain objects--
<%
Pagecontext.setattribute ("Itcast", "Pagevalue");
Request.setattribute ("Itcast", "Requestvalue");
Session.setattribute ("Itcast", "Sessionvalue");
Application.setattribute ("Itcast", "Applicationvalue");
%>
<!--get values using el Expressions-
${pagescope.itcast}
${requestscope.itcast}
${sessionscope.itcast}
${applicationscope.itcast}
(2) Use El expression to get the value of an array inside a domain object
Gets by using the subscript of the array, returns an empty string "" if the subscript of the array does not exist
Code
<!--get the value of the array inside the domain object using the EL expression--
<%
String[] arr = {"Tom", "Jack", "Lucy"};
Put this array inside the domain object
Request.setattribute ("arr", arr);
%>
<!--get the value inside the array using the EL Expression--
${ARR[1]}
(3) Use El Expression to get the value inside the collection of the domain object
Put a list collection into a domain object
Use the set subscript to get a value
Code
<!--Gets the value of the list within the domain object using the EL expression--
<%
List List = new ArrayList ();
List.add ("AAAA");
List.add ("tttt");
List.add ("wwww");
Request.setattribute ("list", list);
%>
${LIST[10]}
Map collection to domain objects
<%
Map map = new HashMap ();
Map.put ("AAA", "AAAA");
Map.put ("BBB", "BBBB");
Map.put ("CCC", "CCCC");
Request.setattribute ("map", map);
%>
${MAP.AAA}
(4) using an El expression to get the value of the property of the object inside the domain object
Code
<!--get the value of the property of the object inside the domain object using the EL expression--
<%
Person1 P11 = new Person1 ();
P11.setid ("404");
P11.setusername ("Mary");
P11.setage ("500");
Request.setattribute ("P11", p11);
%>
${p11.id}
${p11.username}
${p11.age}
(5) Performing operations
Arithmetic Arithmetic +-
Code
<!--perform arithmetic operations--
<%
Pagecontext.setattribute ("AA", "11");
Pagecontext.setattribute ("BB", "22");
%>
${AA+BB}
Relational Operations > < <= >=
Logic Operations && | |
Ternary operator (Trinocular operation)
${user! = null? User.Name: ""}
${empty user? User.Name: ""}: Empty indicates null
${not empty user? User.Name: ""}: Not empty
10. Use El expression to manipulate web development common objects
The EL expression provides 11 hidden objects
Param: Get the submitted parameters, equivalent to Request.getparameter ();
Paramvalues: Get multiple values, equivalent to Request.getparametervalues ();
Header: Get header information, equivalent to Request.getheader ();
Initparam: Get Global initialization parameters
PageContext: Get built-in objects
<!--PageContext objects--
${pagecontext.request}
${pagecontext.response}

Javaweb Series IX (Jsp,javabean,el expression)

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.