Javaweb back end < five > JSP Learning Notes

Source: Internet
Author: User
Tags date now getmessage html comment uuid

I. INTRODUCTION of JSP

1, is also the Sun company launched the development of Dynamic Web resources technology, belongs to one of the Java EE technology. Because the principle is servlet, so jsp/servlet together.

Second, HTML, servlet, and JSP

1, HTML: Static page, can not contain Java code, only HTML-related tags

2, Servlet: Write Java code, do the output of the page, not very convenient (development efficiency is very low)

3, Jsp:html+java

Third, the current time of the output
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <title>First JSP, output the current time</title>        <Metahttp-equiv= "Pragma"content= "No-cache">    <Metahttp-equiv= "Cache-control"content= "No-cache">    <Metahttp-equiv= "Expires"content= "0">    <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > -  </Head>    <Body>now it is:<%        Date  Now = New Date(); //Out.write ( Now. toLocaleString ()); %>    <%= Now%>  </Body></HTML>

Four, the principle and life cycle of JSP

1, JSP is the servlet, learn the key to JSP (grammar), always think of his corresponding servlet.

2. Always flip through the JSP corresponding to the servlet source code

Find in work

V. Syntax for JSP 5.1 template elements

Refers to those HTML in the JSP. You write template elements when you develop them, because they determine the appearance of the page.

5.2Java Program Fragments

Syntax: <%java program%>

Appears in the service method of the JSP corresponding servlet.

Variables declared by <%: local%>

5.3Java-expression

Syntax: <%= expression%>

=

Little exercise: Homework

Java program fragments and expression exercises

1<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%>2 3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >456<title> first JSP, output current time </title>7     8<meta http-equiv= "Pragma" content= "No-cache" >9<meta http-equiv= "Cache-control" content= "No-cache" >Ten<meta http-equiv= "Expires" content= "0" > One<!-- A<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > -- -  the -    -<body> - now it is: +<% -Date now =NewDate (); +         //Out.write (now.tolocalestring ()); A%> at<%=now%> -</body> -View Code

5.4JSP Declaration

Syntax: <%!

%>

Function: Defines the members of the JSP corresponding to the Serlvet (variables and methods, static methods)

<% @pageImport= "Java.lang.reflect.Method"%><%@ page language= "Java"Import= "Java.util.*" session= "true" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >inti=100;//Not recommended         Public voidM1 () {//out.write ("Hello");//No, because out is a local variable in the service    }    Static{System.out.println ("Oyeah"); }     %> <!--Hello--<%--shit--%> <%=session.getid ()%> </body>View Code

Note: It is seldom used in development. Interviews and exams are often tested.

5.5JSP comments

Syntax: <%--This is a JSP comment--%>

Function: commented out code, the JSP engine will not be translated into the servlet source. Be aware of the differences from HTML annotations.

HTML comment:<!--This is HTML comment--

5.6JSP Instruction: Introduction:

Directives are designed for the JSP engine. And just tell the engine how to handle the rest of the JSP page (not the instructions section);

Basic syntax Format: <%@ directive attribute 1 = "value 1" ....%>.

Generally put instructions at the top of the JSP page (not required)

The properties of the directive can be written in one instruction or separately: for example:

<%@ directive Attribute 1 = "Value 1" Property 2 = "Value 2"%>.

Equivalent to:

<%@ directive Attribute 1 = "value 1"%>.

<%@ directive Attribute 2 = "Value 2"%>.

Page

Property:

Language: Tell the engine that the script is in Java. Default Java, Java supported. It's OK to not write.

Extends: tells the engine which is the parent class of the servlet that corresponds to the JSP. No need to write, do not change.

*import: Tell the engine which packages to import.

Note: Engine auto-import: java.lang.*;javax.servlet.*;javax.servlet.http.*;javax.servlet.jsp.*;

form of import:

<% @page import= "Java.util.Date,Java.util.UUID"%> or:

<% @page import= "Java.util.Date"%>

<% @page import= "Java.util.UUID"%> auto-import with eclipse:alt+/

*session: Tells the engine whether to produce a HttpSession object, that is, whether to call Request.getsession () in code. The default value is true.

The buffer:jspwriter is used to output the contents of the JSP onto the page. Tell the engine to set his cache size. Default 8Kb.

*errorpage: Tells the engine, the current page has an exception, which page should be forwarded to (the path:/represents the current application)

Configure global error page: Web. xml

*iserrorpage: Tells the engine whether to catch the exception. If this property is true, you can use the exception implicit object to print the details of the exception in the page.

<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"Iserrorpage="false"%>

<%=exception.getmessage ()%>

*contenttype: Tells the engine the MIME type of the response body.

Contenttype= "Text/html;charset=utf-8" is equivalent to Response.setcontenttype ("<%=exception. GetMessage ()%>");

*pageencoding: tells the engine which code table to use when translating JSPs (to read JSP files from disk).

Pageencoding to match the encoding on the existing disk

In the compiler (MyEclipse Eclipse) Don't worry, pageencoding will automatically convert to pageencoding encoding when modified

Pageencoding= "UTF-8": equivalent, informed the engine with UTF-8 read JSP, also Response.setcontenttype ("Text/html;charset=utf-8");

*iselignored: Tells the engine whether to ignore the El expression. The default value is False, not ignored. ${1+1}

Summary: There are many attributes. The default value that is generally used. Easy to use, the simplest form:

<%@ page pageencoding="UTF-8"%>

Include

Contains

Properties: File, beginning with "/", represents the current app.

<% @include file= "/07include.jsp"%>

Taglib:

Introduction of external labels.

<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

URI: The URI address of the external label, like the namespace

Prefix: prefix.

Vi. forwarding and inclusion in the JSP

1. Forwarding:

<jsp:forward page= "/10forward.jsp" ></jsp:forward>

The built-in label for the JSP.

2. Parameters: cannot be used alone

Passing request parameters to a target

3. Include: (Interview questions)

Static inclusions: <% @include%>

Dynamic inclusion: The rest of the

<jsp:include page= ""/>

Summary: Use tags to reduce the number of Java scripts.

Vii. nine hidden Objects of JSP: (Interview questions)

Local variables in the service method of the servlet corresponding to the JSP

JSP implied object name

Type

Note

Request

Javax.servlet.http.HttpServletRequest

Response

Javax.servlet.http.HttpServletResponse

Session

Javax.servlet.http.HttpSession

Page directive has a switch

Application

Javax.servlet.ServletContext

Config

Javax.servlet.ServletConfig

Page

Javax.servlet.http.HttpServlet

Instance reference of the current JSP corresponding to the servlet

exception

Java.lang.Throwable

Page directive has a switch

Out

Javax.servlet.jsp.JspWriter

The character output stream. Equivalent to PrintWriter

PageContext

Javax.servlet.jsp.PageContext

is important

Viii. exception Handling in JSP

1. Run-time exception

The above situation to find JSP corresponding servlet source code.

2. Error (Syntax not correct)

Ix. PageContext abstract class (very important)

1, itself a domain (scope) object, but also the ability to manipulate the properties of the other 3 domain objects

L Page Range:

void SetAttribute (String name,object value);

void RemoveAttribute (String name);

Object getattribute (String name);

L manipulate properties in other domain objects:

void SetAttribute (String name,object value,int scope);

void RemoveAttribute (String name,int scope);

Object getattribute (String name,int scope);

int scope: Constant, PageContext page_scope (page) request_scope (Request) Session_scope (session) Application_scope (APP)

L Find the method of specifying the name object in four scopes:

Page request (Request) session (session) application (application)

Object Findattribute (String name);

2. Get the other eight JSP hidden objects (with custom labels)

Pagecontext.getrequest ();

Getservletcontext ();

Getservletconfig ();

GetException ();

Get ...

3, provide a simple method of forwarding and inclusion

Pagecontext.include (String URL);

10 or four large domain objects

Actual development: storage of data.

PageContext: Page range. Minimum. Rarely used in development. Demo code.

ServletRequest: Request scope. Different resources, only with forwarding.

HttpSession: Session scope. Multiple requests for shared data, but different clients cannot share.

ServletContext: Application range. Biggest. Use as little as possible. Time to do synchronous processing.

Xi. best Practices for JSP development

Servlet: Good at writing Java code logic, not good at outputting HTML content.

In development as: Controller.

JSP: Not good at writing Java code logic, good at displaying results.

In development as: view (Show results)

12, the concept of JavaBean

1, JavaBean also known as Pojo (Plain old Java object), VO (Value object), Do (Data object)

2, the JavaBean writing standard

A. Must be public

B. Provide the default construction method

C, the fields are private: private String username;

D. Provide a public getter or setter method: property.

GetUserName (): Read Property, property name: Username.

Setusername (String username): Write property, property name: Username.

Note: Private Boolean married; Getmarried () ===ismarried ()

E, generally need to implement the Java.io.Serializable interface.

Conclusion: JavaBean is mainly used in the development of Java EE in the encapsulation data.

13. Use JavaBean in JSP

1, import JavaBean: With the import of page directive

2. Declaration JavaBean:

3. Get Properties:

If the property is null, then the interface displays null.

4. Set Properties:

Note: Conversion issues for types

You can use the request parameter to set the properties of the JavaBean.

5. JavaBean Survival Range:

When declaring JavaBean, specify the scope property. Default value page, current page range.

Optional value: page|request|session|application

Summary: Jsp+javabean, development model, MODEL1 (model 1)

Javaweb back end < five > JSP Learning Notes

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.