JSP/servlet learning notes

Source: Internet
Author: User

I. jsp tag

1. Note: <% -- Comment content -- %>
2. Declaration: <%! Declarative Part %>
3. expression: <% = expression %>
4. Script: <% script %>

Ii. Three JSP compilation commands: Page, include, tablib

Format: <% @ compile command name attribute name = "property value" %>

1. Page
<% @ Page
[Language = "Java"]
[Extends = "package. Class"]
[Import = "package. Class I package .*},... "]
[Session = true I false "]
[Buffer = "none I 8kb I size kb" 1
[Autoflush = "True I false"]
[Isthread8afe = "True I false"]
[Info = "text"]
[Errorpage = "relativeurl"]
[Contenttype = "mimetype [; charset = characterset]" I "text/html; charset = ISO808859-1"]
[Iserrorpage = "True I false"]
%>

Example:
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" %>
<% @ Page import = "Java. SQL. *" %>

2. Include
Format: <% @ include file = "relativeurlspec" %>
If the embedded file needs to change frequently, we recommend that you use the <JSP: Include> operation command because it is a dynamic include statement.

3. tablib
.

Iii. Seven action commands of JSP

1. jsp: forward: the execution page turns to forward request processing to the next page.

For JSP 1.0, use the following syntax:
<JSP: Forward page = "{relativeurl I <% = expression % >}"/>

For JSP and later versions, you can use the following syntax:
<JSP: Forward page = "{relativeurl I <% = expression % >}">
{<JSP: Param.../>}
</Jsp: Forward>

The second syntax is used to add additional request parameters when forwarding. The added request parameter value can be obtained through the getparameter method of the httpservletrequest class.

2. jsp: Param: used to pass parameters. It must be used with tags of other supported parameters.

<JSP: Param name = "parametername" value = "patametervalue"/>

3. jsp: include: Used to dynamically introduce a JSP page.

<JSP: Include page = "{relativeurl I <% = expression %>}" Flush = true "/>
Or
<JSP: Include page = "{relativeurl I <% = expression %>}" Flush = "true">
<JSP: Param name = "parametername" value = "patametervalue"/>
</Jsp: Include>

4. jsp: usebean: Use Javabean.

<JSP: usebean id = "name" class = "classname" Scope = "page I request I Session I application"/>

5. jsp: setproperty: Modify the attribute value of the JavaBean instance.
6. jsp: getproperty: Get the attribute value of the JavaBean instance.
Example:
<JSP: usebean id = "Pi" class = "Lee. Person" Scope = "page"/>
<JSP: setproperty name = "Pi" property = "name" value = "Wawa"/>
<JSP: getproperty name = "Pi" property = "Age"/>

7. jsp: Plugin: used to download JavaBean or applet to the client for execution.
<JSP: plugin type = "bean | applet"
Code = "classfilename"
Codebase = "classfilediretoryname"
[Name = "instancename"]
[Archive = "urltoarchive"]
[Align = "bottom 1 top I Middle 1 left 1 right"]
[Heigh = "displaypixels"]
[Width = "displaypixels"]
[Hspace = "leftrightpixels"]
[Vspace = "topbottompiexels"]
[Jreversion = reversionnumber11.2 "]
[Nspluginurl = "urltoplugin"]
[Iepluginurl = "urltoplugin"]>
[<JSP: parames>
[JSP: Param name = "parametername" value = "parametervalue" I>]
</Jsp: Params>]
[<JSP: fallback> text message for user that can no see the plugin
</Jsp: fallback>]
</Jsp: plugin>

Iv. Nine built-in objects of JSP

Note: The object names of these objects are in lower case.

1. Application
Javax. servlet. servletcontext instance
This instance represents the web application to which the JSP belongs. It can be used for JSP pages or information exchange between servlets. Common methods include getattribute (string attnarne), setattribute (string attnarne, string attvalue), and getinitparameter (string paramnarne.

2. config
Javax. servlet. servletconfig instance
This instance represents the configuration information of the JSP. Common methods include getinitpararneter (string paramnarne) and getinitpararneternarnes. In fact, JSP pages do not need to be configured, so there is no configuration information. Therefore, this object is more effective in servlet.

3. Exception
Java. Lang. throwable instance
This instance indicates exceptions and errors on other pages. This object can be used only when the iserrorpage attribute of the compiled command page is true. Common methods include getmessage () and printstacktrace.

4. Out
Javax. servlet. jsp. jspwriter instance
This instance represents the output stream of the JSP page, which is used to output content to form an HTML page.

5. Page
It indicates the page itself, which is usually of little use. That is, this in the servlet. Its type is the generated servlet.

6. pagecontext
Javax. servlet. jsp. pagecontext instance
This object represents the context of the JSP page. You can use this object to access the shared data on the page. Common methods include getservletcontext () and getservletconfig.

7. Request
Javax. servlet. http: instance of httpservletrequest
This object encapsulates a request, and all client request parameters are encapsulated in this object. This is a common object, which must be used to obtain client request parameters. Common methods include getpararneter (string paramnarne), getpararnetervalues (string paramname), setattribute (string atttibutename, object attributevalue ),
Getattribute (string attributename) and setcharacterencoding (string env.

8. Response
Javax. servlet. http. httpservletresponse instance
The response of the server to the client. Usually, this object is rarely used for direct response, and the output response uses the out object, while the response object is often used for redirection. Commonly used methods include sendredirect (Java. Lang. string location.

9. Session
Javax. servlet. http. httpsession instance
This object represents a session. Start from the connection established between the client browser and the site, and start the session until the browser is closed. Common methods include getattribute (string attname), setattribute (string attname, string attvalue.

V. MVC Based on Servlet/JSP/Bean

M: model, that is, model, corresponding to JavaBean.
V: view, that is, view, corresponding to the JSP page.
C: controller, that is, controller, corresponding to servlet.

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.