Jsp/servlet Learning Note (core programming) JSP section

Source: Internet
Author: User

The JSP eventually needs to be converted to Servelt and compiled, and is actually a compiled servlet during the request

JSP specializes in generating a large number of organized pages of structured HTML or other character data. Servlets excel at raw data, build data-rich pages, and perform tasks with little or no output (redirection)

1.jsp is converted to servlet and compiled only for the first visit after the modification;
2. Load into memory Initialize and execute general rules that follow the servlet

Basic syntax: 11th Chapter

JSP calls dynamic code policy
1. Direct invocation (use and small amount of code)
2. Introduction Calling Java code (developing a separate utility class that invokes only the Java code required by the tool in the JSP)
3. Utility Bean
4. Using MVC
5. Using a JSP expression
6. Using Custom Labels

JSP SCRIPT element type
1.<%=java expression%> is evaluated and inserted into the servlet output
2.<%java code%> Scriptlet, they will insert the _jspservice method into the Select
3.<%! Field/method declaration%> declarations, which are inserted into the definition of the servlet class, do not belong to any of their own methods

Restricting Java code in a JSP
It is best to put the Java code in the package and put it in the web_inf/classes/package, then use one or two Java code to call

Regardless of what Java class is written, the class file is deployed in the web_inf/classes/package.
All classes are best placed in the package.

First, the application of JSP expression
<%=java expression%> the equation is converted into a string in the temperament to insert into the page. The evaluation is performed during run time. So you can access all the information in the request.
To simplify an expression, you can use several predefined variables. _jspservice method (in a servlet generated by the Jap page, a local variable equivalent to doget) will use what name

In terms of expressions, the most important thing is the object:
Request Response session out application (SERVLETCONTEXT) data structure shared by all servlets and JSP pages in the web App
Out is actually the printwrite that corresponds to the getwrite modification in the JSP

expression XML Syntax <jsp:expression>. </jsp:expression>
Be sure to lower case

Second, Scriptlet
Writing scriptlet to accomplish more complex tasks, you can plug arbitrary code into the _jspservice method to invoke
<% Java code%>
Import Word into HTML (you do not need to specify the type in the appropriate file.) The jspwirte of the JSP will buffer the document)
<% Response.setcontenttype ("Application/msword")%>

The expression does not end with a semicolon, and Scriptle inclusion Java code must end with a semicolon

<jsp:scriptlet>java code</jsp:scriptlet>
Try to avoid mixing HTML code with Scriptle, and sometimes use this method in order to include the HTML content in the If...else.
<%if (Math.random () < 0.5) {%>
<H1> has e nice day<%} else {%>
<H1> has e nice day<%}%>
Equivalent to
if (Math.random () < 0.5) {
Out.println ("<H1> has e nice day} else {
Out.println ("<H1> has e nice day}

Iii. inserting a declaration into the master definition body of a servlet class
<%! Field or Method definition%>
Output is not normally generated. Can contain any field or code. But don't overwrite code like Doget,dopost init
The page can be initialized and cleaned using Jspinit or jspdestory
It is equivalent to redefining a class in the Servlet class that inherits the Httpjsppage interface, and the class contains the code in the Declaration. And then call the code in _jspservice.

<jsp:declaration>field or Method definition</jsp:declaration>

Implicit variables in the _jspservice method (in fact, built-in local variables)
Request response out Session application
Config (this page servletconfig) page (equivalent this) exception (Error page) PageContext class (This class has getrequest,getresponse, GetSession and GetSession methods)

The 12th chapter controls the generated servlet structure JSP Page
<% directive attribute= "value"%>
There are three main types of directives in JSP page include Taglib
Page Control servlet structure
Include inserting a file into the JSP page
Taglib Custom Labels

Import Properties:
<%@ page import= "Package.class",...%>
The use class must be placed in the web-inf/classes

ContentType and Pageencoding Properties:
<%@ page contenttype= "Mime-type; Charset=character-set "%>
<% Request.setcontenttype ("...");%>
You can introduce the spreadsheet to the original book, page 253.
Change language:
<%@ page pageencoding= "Shite_jis";%> Japanese

Session Properties:
<%@ page session= "true"%> whether session is enabled

Iselignored Properties:

Buffers and AutoFlush properties: Buffer size and auto-empty buffer
<%@ page buffer= "32Kb" autoflush= "true"%>

Info property: Defines a string that is obtained through the Getservletinfo method in the servlet
<%@ page info= "Somemessage"%>

ErrorPage and Iserrorpage Properties:
<%@ page erropage= "errprpage URL"%> error page occurs
<%@ page iserrorpage= "true"%> can be used as an error page for other pages

Error pages are placed in Web-inf to prevent direct user access

IsThreadSafe abandoned Singlethreadmodel poorly designed

Extends properties:
Specifies the superclass of the class generated by the JSP page
<%@ page extends= "Package.class"%>

Language properties: Specifying scripting language
<%@ page language= "Java"%> without the default Java

<jsp:directive.directivetype attribute= "Value" >
Such as:
<jsp:directive.page import= "java.util.*" >

13th. JSP contains files and applets
Jsp:include include page during action request
HTML page JSP page plain text servlet output, etc.
Exactly the same as the include method in the RequestDispatcher class

<jsp:include page= "Relation-oath-to-resource"/>
Secondary properties Flush: Whether the page contains empty main page output stream
At any time, whatever the server is dealing with, it should be interpreted according to the current Web application. It is only when the browser is processing that it will be interpreted relative to the server root directory.

The page that is included by default uses the same request parameter as the main page
You can use Jsp:param to increase request parameters
<jsp:include page= "Relation-oath-to-resource" >
<jsp:param name= "id" value= "A123" >

Jsp:param will be included in the page priority adoption

Include specifies
<%@ include file= "Relative url"%>

Jsp:include is activated during the request and only works when requested
Include directives are activated during page conversions and are difficult to maintain

Jsp:include is programmed as a servlet only when requested by a contained page, so the include directive is more powerful

<%--navbar.jsp Modified 9/1/03--%>
<%@ include file = "navbar.jsp"%>
Modify the included file to change the modified date automatically when you add a comment

<jsp:directive.include file= "..." >

Forwarding requests use Jsp:forword to get the full output of the secondary page
However, this is not recommended because the main page cannot contain any output.
You can use the servlet to invoke the ForWord method of Requsetdispather

Jsp:plugin adding applets

14th Chapter The application of JavaBean component in JSP document
Pros: No need for Java syntax objects sharing simple request parameters and object properties facilitate correspondence
The system will first look for the Bean object and cannot find the object in the creation
1. You must have a 0-parameter constructor
2. The instance variable (field) that should not be exposed
3. Persistent values should be accessed through the Set/getxxx party

Bean Basic Tasks
JSP page, mainly using three kinds of building and operation JavaBean Operation JavaBean Component

1.<jsp:usebean id= "Beanname" class= "Package.class"/>

<%classname beanname = new ClassName ();%>

2.<jsp:getproperty name= "Beanname" property= "PropertyName"/>
<%=beanname.propertyname ()%>
3.<jsp:setproperty name= "Beanname" property= "PropertyName" value= "PropertyValue"/>
<% beanname.propertyname ("PropertyValue");%>

<jsp:setproperty name= "Beanname" property= "*"/>
Properties Auto-Associate simplify operations

Type= "A variable type that can be declared is a parent class or an interface"

Shared Properties Scope
The scope= "page" page scope means that the current request is processed, and the Bean object is bound to a local variable and should also be placed inside the PageContext, and the servlet can be accessed through getattribute of the predefined variable PageContext. General omission does not write

Scope= "Request"
Indicates that the Bean object is bound to a local variable and is also written to httpservletrequest to access it through getattribute. The request object is shared when the page jumps

Scope= "Session"
Indicates that the Bean object is bound to a local variable and is also written to HttpSession to access the objects in the HttpSession through GetAttribute.

Scope= "Application"
Indicates that the Bean object is bound to a local variable and is also written to ServletContext and shared throughout the Web App. Accessed via ServletContext's getattribute access.

The bean can automatically convert a string int double type and is case sensitive

Simulate constructors, initialize variables on first access
<jsp:usebean id= "Counter" class= "Counterclass" scope= "Applicaion" >
<jsp:setproperty name= "Counter" property= "Fristpage" value= "share,jsp"

Jsp/servlet Learning Note (core programming) JSP section

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.