JSP Learning (JavaBean), jsp learning javabean

Source: Internet
Author: User
Tags html comment

JSP Learning (JavaBean), jsp learning javabean

Java Web Learning

I, Build a java web development environment:

(1) install jdk

(2) install the Tomcat server (open source project of Apache), install Tomcat, and set Environment Variables

(3) install EclipseEE (or MyEclipse)

II, Introduction to WEB-INF Security directories (accessible only to servers)

(1) web. xml file (project deployment file ):

When we start a WEB project, containers (such as JBoss and Tomcat) will first read the project web. the configuration in the xml configuration file can be started normally only after this step is completed without errors.

(2) the classes file is used to store the *. class file, that is, the compiled bytes.

(3) lib folder, used to store required jar packages, such as JDBC drivers connected to the database.

III, Configure the JDK and Tomcat server in Eclipse (javaEE:

(1) Add the downloaded Tomcat Server to Windows-Preferences-Server-Runtime Environments.

(2) modify the Project virtual path: Right-click the Project and choose Properties-Web Project Setting-Context root.

(3) modify the default tomcat port, find the server. xml file in the Servers directory, find the Connector tag in the file, and modify the port.

 

IV, Basic JSP syntax

(1) JSP Introduction: Java Server Page is a simple servlet version that operates on the Server like servlet.

(2)Page commandSyntax: <% @ page attribute 1 = "attribute value" attribute 2 = "attribute 1, attribute 2" %>

① Language: Specifies the scripting Language used by JSP. The default value is java.

② Import: reference the class library used by the script language.

③ ContentType: defines the encoding method of JSP.

(3)Include command: Embed an external file into the current JSP file and parse the JSP statement of the page.

(4)Taglib command: Use the tag library to set a new custom tag.

(5) JSP Declaration: <%! Variables and functions can be declared, which can be called later in the script.

(6) JSPThree annotations:

① HTML comment: <! --> Visible to the client

② JAVA Script annotation: <% // and/**/%> invisible

③ JSP Note: <% -- %> invisible

(7) JSP expression: <% = name %> (name is the variable in the Declaration)

(8) JSP lifecycle: client request-determine whether it is the first time-if so, convert jsp into a Servlet and execute jspInit () (only once throughout the cycle ), if not, the jspService () method is executed using multiple threads.

(9) Common JSPTwo jumps:

① Response. sendRedirect ("login1.jsp ");

② Request. getRequestDispatcher ("login1.jsp"). forward (request, response)

(10) Nine built-in JSP objects (new objects are not required): out, request, response, session, application, page (this), pageContext, exception, config.

① Out object method: print (), flush () (export the buffer content), clear () (exception occurs after the flush () method), clearBuffer () (no exception ).

② Request object method: getParameter (String name) returns the value of the parameter specified by name (that is, the value entered by the input tag ). GetParameterValues (String name) to obtain the multi-value array. (Mainly used for checkBox) setCharacterEncoding ("UTF-8"), you can set the encoding format. GetAttribute (String name) returns the attribute value of the specified attribute. SetAttribute ("password", "12345") is used to set attributes. GetServerName (), returns the server host name of the request.

③ Session Object method: The session disappears only when all current pages are closed. GetValueNames () returns all available attribute values, getCreateTime (), returns the session creation time, setAttribute (String name, Object value), invalidate (), and destroys the session.

④ Application Object lifecycle: Server restart-server shutdown.

⑤ Page Object: pointing to the current JSP page itself, which is equivalent to the this pointer in the class.

⑥ PageContext object: provides access to all objects and namespaces on the JSP page. Forward (. jsp), re-import to another page. Include (. jsp). The current location contains another file.

7. config object: it is used for JSP information transmission during servlet initialization, including the parameters used for servlet initialization.

Except exception object: An exception object. This object is generated when an exception occurs during page running. Add errorPage = "exception handling file" to the page ".

V, JavaBean Learning
<jsp:useBean id="myUser" class="ch1.User" scope="page"/><jsp:setProperty name="myUser" property="*"/><jsp:getProperty name="myUser" property="username" value=” ”/>

 

(1) JavaBean is similar to an encapsulated class. You can obtain values such as user names in HTML forms without using the traditional code, that is, request, the above id = "myUser" is similar to the name of the Self-instantiated class. class is used to specify a class to be instantiated. scope is used to specify the scope of the javaBean action, which has four attribute values:

① Page: Valid only on the current page.

② Request: You can use HttpRequest. getAttribute () to obtain the JavaBean object.

③ Session: You can use HttpSession. getAttribute () to obtain the JavaBean object.

④ Application: You can use the application. getAttribute () method to obtain the JavaBean object.

 

(2) Both setProperty and getProperty have the name attribute and property attribute ('*' indicates the value that automatically matches the name of the member of the instance with the form name, you can also directly write the name of a variable and assign values with values) and value attributes.

(3) differences between session and cookie:

① Session user information is stored on the server, and cookies are stored on the client.

② Session stores the Object type and cookie stores the String type.

③ When a Session ends, the stored data is destroyed. Cookies can be stored on the client for a long time.

④ Session stores important information, and cookie stores unimportant customer information.

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.