jsp web programming summary

2010-11-15  来源:本站原创  分类:Java  人气:132 

Why use JSP

Servlet the following shortcomings:

1.Servlet coding, deployment and debugging tasks cumbersome

2. Generate dynamic web pages cumbersome division of labor is not conducive to project

JSP Servlet can use to make up for these shortcomings

What is JSP

JSP (Java Server Pages), HTML statements and nested by which the Java code in the form of a plain text file with the extension. Jsp. Jsp page using the technology for the dynamic pages written in

JSP is usually used to simplify the creation of text generated Servlet, the Servlet is more suitable for the native byte sent to the client or the Java source code needed full control of the source code of the occasion. Separation of the two well

JSP engine

Container manufacturers define a class to help us translate JSP files, such as: org.apache.jasper.servlet.JspServlet

Ps: After all of the JSP translator HttpJspPage class must implement the interface, and HttpJspPage Servlet interface is the sub-interfaces, so in this sense, JSP is the essence of Servlet

JSP life cycle

1. Web container receives the first request for a JSP page, the first of the JSP page is automatically translated into Servlet code

2. Web container is responsible for the Servlet code is compiled to bytecode. With the source files in one directory

3. Web servlet container loads the newly generated classes

4. Web container creates servlet instances

5. Web container invokes the Servlet example jspInit () methods, JSP page loads resources

6. Container creates a new thread to handle the customer's request, servlet object _jspService () method to run

7. Servlet container calls the generated object jspDestroy () method, the destruction of resources loaded

JSP implicit objects

Servlet essentially translated in the local variables, a total of 9, which

There are 4 areas of scope:

pageContext - the representative pageContext object, only valid in the current page

request - HttpServletRequest object on behalf of, in a valid request

session - HttpSession objects on behalf of, and effective in a session

application - on behalf of ServletContext object is valid in the entire web application

There are 2 output areas

response - the HttpServletResponse associated with the response on behalf of the object

out - output stream associated with the response on behalf of the JspWriter object that can output character stream

There are 3 other

config - on behalf of ServletConfig, used to configure the servlet object

page - the current jsp object, the equivalent of this

exception - Throwable object representative of, only the use of JSP error page

Script elements

Declaration <%! Contents of the statement%>

- Mainly used to define, you can define global variables, static variables and methods

Script <% Java code%>

- Writing Java code, rich _jspService () method

Expression <% = expression% script>

- Equivalent to out.print ("script expression")

Notes

JSP Comments <% - comment content -%>

- Translated on the server side code and client are not visible, safe

Html comments <! - Comment content ->

- Translated on the server side and client code are visible

Java Notes / /

- On the server side code after translation can be seen, the client is not visible

JSP Directive

JSP directive is used to provide information on the entire JSP page, JSP page and the container used for communication between the three types of instruction

Page directive

<% @ Page attribute1 = "value1" attribute2 = "value2"%>

- The JSP page used to set the properties and related functions, page 11 attributes command, the more common are:

contentType: Set MIME types and JSP code

import: the introduction of the JSP Java packages and classes, separated by commas between multiple packages

session: Specifies whether the current page to get the current user's session object

errorPage: If an exception occurs the current page, the page will be redirected to the errorPage specified page treatment

isErrorPage: allows you to specify the JSP pages for the error handling page

include instruction

That when the JSP compiler to insert a file or code that contains the file that contains a static

<% @ Include file = "relativeURL"%>

Ps: 1) include instructions contained in the file name can not be a variable url, can only be a static file name.

2) to be included in the page, generally do not contain <html> <body> tags, can not exist between the same two pages of local variables

taglib directive

Allows users to custom label

<% @ Taglib prefix = "tagprefix" uri = "url"%>

JSP standard action

<jsp:include> action

Achieve a dynamic include, to show the results inserted into the current page to display

<jsp:include page="localURL" flush="true"/>

page: that a relative path

flush: essential attributes, and can only be true, that when the buffer is cleared automatically when full

Ps: static include and dynamic include the difference between:

1) static include a copy of the file intact, so the page is included, generally do not contain <html> and <body> tags, can not exist between the same two pages of local variables; dynamic contains contains the results of the two pages between the same variables can exist

2) The static occurs when included in the compilation, only include files; dynamic when included in the request can include JSP and Servlet request url

<jsp:forward> action

Equivalent to the internal forwarding

<jsp:forward page="relativeURL">

EL Expression

EL (expression_r_r Language) - Expression Language

Syntax :${...}

EL's main objective is to eliminate from the Java code in JSP pages, therefore, EL expressions can not define variables in the script.

EL expression is implicit variables:

Category

Implicit variable name

Description

JSP pageContext Used to access the JSP implicit objects
Scope pageScope And the page scope attribute names and values associated with the Map class
requestScope With the request scope attribute names and values associated with the Map class
sessionScope With session scope attribute names and values associated with the Map class
applicationScope And the application scope attribute names and values associated with the Map class
Request parameters param String contains the request parameters Map class
paramValues Request parameters array contains the string (String []) of the Map class
Request header header Map the first string contains the request class
headerValues Contains the request header string array (String []) of the Map class
Cookie cookie Storage requests included by name in the Map class Cookie

Custom Label

1. Create a new class that inherits BodyTagSupport class, BodyTagSupport class implements the Tag interface, the rewrite method to implement, is generally doEndTag ()

2. In the WEB-INF create a new following. Tld file, complete the registration

<? Xml version = "1.0" encoding = "UTF-8"?>
<taglib>
<tlib-version> 1.0 </ tlib-version>
<jsp-version> 1.2 </ jsp-version>
<uri> http://www.baidu.com </ uri>
<tag>
<name> tagTast </ name>
<tag-class> com.lovo.MyTag </ tag-class>

</ Tag>
</ Taglib>

3. Taglib directive in jsp page add

<% @ Taglib prefix = "my" uri = "com.lovo.MyTag">

4. In the jsp page using custom tags

Set properties for the custom tag

1. The definition of property, and get and set methods generated

2. <tag> Tags in tld file is registered

<attribute>
<name> property name <name>
<required> true </ required>
</ Attribute>

3. In the jsp page, which set the attribute of the

Standard Tag Library JSTL:

Requires two Jar files: jstl.jar, standard.jar; constructed to be successful, add the following directive in the JSP;
<% @ Taglib uri = " http://java.sun.com/jsp/jstl/core "prefix =" c "%>

Common tags:

<c:if> - judge

<c:forEach> - Cycle

Used in the JSP JavaBean:

1, <jsp:useBean> action

Syntax: <jsp:useBean scope="page|request|session|applicaion">

id - the object property name

scope - Scope

class - the class full path name.

2, <jsp:getProperty> action

Syntax: <jsp:getProperty name="beanName" property="propertyName">

name - the corresponding <jsp:useBean> in the id.

property - the property name to get

3, <jsp:setProperty> action

Syntax: <jsp:setProperty name="beanName" property=" Property name ">

<jsp:setProperty name="beanName" property=" Property name " param=" The name of the parameter ">

<jsp:setProperty name="beanName" property=" Property name " value=" Property value "> / / get the request parameters

<jsp:setProperty name="beanName" property="*">

相关文章
  • jsp web programming summary 2010-11-15

    Why use JSP Servlet the following shortcomings: 1.Servlet coding, deployment and debugging tasks cumbersome 2. Generate dynamic web pages cumbersome division of labor is not conducive to project JSP Servlet can use to make up for these shortcomings W

  • 1. Web programming in Chinese garbage problem 2010-07-14

    In Web programming, involving transfer of two Chinese jsp page parameter, the problem will be garbled, the background always print out a number of question marks. Search the web will come out a lot of answers, most say that is changing my jsp page en

  • Web programming is a functional programming 2010-11-30

    Any one in two areas - local applications and Web applications - have done long-term development of people will tell you, web application development and the traditional application development is very different. This refers not programming languages

  • Java multi-threaded programming summary 2010-04-18

    Copyright: original works, allowing reproduced, reprinted, please be sure to indicate the article hyperlink form the original source , author information and this statement. Otherwise held liable. http://lavasoft.blog.51cto.com/62575/27069 Java multi

  • linux shell programming Summary (Abstract) 2010-05-10

    Seldom written, easy to forget. Extract notes. linux shell programming summary http://blog.csdn.net/ltbook This week learning the unix / linux shell programming, refer to the "LINUX and UNIX Shell Programming Guide", David Tansley forward; Xu Ya

  • Linux, the Apache reverse proxy ProxyPass IIS Web site summary 2010-08-19

    Linux, Apache reverse proxy ProxyPass IIS Web site summary of the collection in the write down this conclusion, I would like to say a few words: Do not mix work and love together, anxious edge distressed ah! In order to complete a seemingly simple pa

  • Perl Language (VII) Web Programming 2011-04-28

    Perl Language (VII) Web Programming 16. Perl and Web CGI (Common Gateway Interface) -----> FastCGI ------> Apache mod_perl 16.1 CGI use CGI; my $ q = CGI-> new; $ H = $ q-> header (-charset => 'utf-8', -Type => 'text / html'); print $ h.

  • Tomcat JSP Web Development garbled Summary 2010-12-09

    1. Garbled static page file encoding and the browser to display the code is inconsistent. 1) Check the original file encoding, you can use Notepad to open, view and select Save As; 2) add a command to the current page to recommend browser with the sp

  • java web development summary (1): Introduction & my server configuration to upgrade the road map 2010-05-01

    Foreword to do java development have four years, look back, and he is to sum up a need for accumulation and breakthrough time. Bowen in front of me there that my work experience, here to mention what I have experienced project: have been doing web de

  • Businesses and individuals how to choose the type of Web programming language and 2010-10-14

    Businesses and individuals on how software development languages and types of choices Now if you hear someone talking about software or software development, then, may not be started again just a few years ago when the IT industry as a stranger. With

  • sqlserver database, web programming sqlserverhelper 2010-03-07

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace RedGlove.DBUtility { public class SqlServerHelper { // The database connection string

  • mysql database web programming mysqlhelper 2010-03-07

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.SqlClient; using System.Configuration; using MySql.Data.Types; using MySql.Data.MySqlClient; // The source download from www.51aspx.com

  • oracle database web programming oraclehelper 2010-03-07

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.OracleClient; using System.Configuration; namespace RedGlove.DBUtility { public abstract class OracleHelper { // The database connectio

  • access database web programming accesshelper 2010-03-07

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.OleDb; using System.Configuration; namespace RedGlove.DBUtility { public abstract class OleDBHelper { // The database connection string

  • Programming Summary (3) 2010-03-23

    12, today, I wrote a small application. On the short message. Need to give an action --- encrypted short message. Current practice is to provide an interface. securityable (in my application to use this name). Then, write a short message type, this i

  • jsp web server to achieve the list of files and include file download (update) 2010-06-02

    Reference to some online content written by someone else, write the following jsp, down alternative. Project has two jsp pages, list.jsp (list of functions to achieve here by default WebRoot / spjk / video folder) download.jsp (to achieve download) S

  • How to use the Facebook API to open maps Web programming? 2010-07-16

    Facebook, an open API for its highly innovative use of proven Web application design it's extraordinary influence. Recently, Facebook replaced with a new interface, its Facebook Connection API, the interface allows any page on the Internet the same p

  • [Transfer] with ScriptX controls Web Print Summary 2010-10-08

    Recently did a small program needs to be fine on the Web to print position control, IE basically be printing their own half-crippled, unable to customize paper, can not be directly printed, and because the browser version or settings in the operating

  • Javascript object programming summary 2010-11-10

    Encoding every day, almost every day, can not do without javascript object programming, and he repeatedly had to "javascript advanced programming" part of the object of programming watched many times, but still often get a confused number of low

  • ubuntu web programming solutions to display Chinese garbled 2010-11-18

    The blog is Just for the beginner like me !!!!! I use ubuntu10.10, just learning the "Web technology application," the editor that comes with ubuntu gedit to write a few HTML, JSP, running in the Firefox browser and found the Chinese are always