Java Learning notes-jsp3 and JavaBean (35)

Source: Internet
Author: User

JSP configuration

JSP is the essence of the servlet, then the servlet can be configured for Web. XML, then the theoretical JSP can also be basic configuration.

Modify Web. xml

<context-param>    <param-name>title</param-name>    <param-value>czbk</ param-value>  </context-param>  <servlet>    <servlet-name>myjsp</servlet-name >    <jsp-file>/init.jsp</jsp-file>    <init-param>      <param-name>ip</ param-name>      <param-value>192.168.10.1</param-value>    </init-param>    < load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>   < servlet-name>myjsp</servlet-name>   <url-pattern>/haha.html</url-pattern>  </ Servlet-mapping>

Writing code to get configuration information

<body>    <%--JSP access Path--%> This is    my jsp! <br/>    <%--Get configuration parameters--%>    <%= Config.getinitparameter ("IP")%><br/>    <%= application.getinitparameter ("title")%><br/>    <%--overriding JSP initialization method--%>    <%!       public void Jspinit () {         System.out.println ("JSP init .....");            }    %></body>
JSP Error Troubleshooting
1. Translation phase
<body> <%-- </body>

2. Compile stage <body> <%= "Itcast%> </body>

3. Run stage <body> <%= 1/0%> </body>
Org.apache.jasper.JasperException:An exception occurred processing JSP page/error.jsp at line 7

The only way to handle errors is to look at the error message.

If you are forced to map the JSP to a different path when you make a JSP configuration, you need to use a one-to-many mapping to map the original path, otherwise the original path is not available for configuration parameters.

  <servlet-mapping>    <servlet-name> myjsp</servlet-name>    <url-pattern>/init.jsp </url-pattern>    <url-pattern>/haha.html</url-pattern>  </servlet-mapping>

Two JavaBean

So far JavaBean has primarily helped us with the encapsulation of basic data types. can be convenient for later data transmission.

Characteristics:

    1. Private words almost all the attributes
    2. Provides all the get and set methods
    3. constructor with no parameters
    4. Provides a constructor that has parameters

The attribute of JavaBean is not a property that you define in JavaBean, but a value that is followed by the get and set methods to remove the first letter size from the lowercase letters.

JavaBean Technology

The main task of JSP is to display the user's data. It is also necessary to assume responsibility for encapsulating user data. Then in the JSP page we have to use the corresponding technology to achieve the encapsulation of the data. That is, the JavaBean technology in JSP.

1 Creating a JavaBean Object

<jsp:usebean? Specify an action name, using Javabeanid= ""? Specifies a reference variable name, or a property name Beanname= ""? Specify an alias for the object class= ""? Specify the JavaBean class (fully qualified name) to be created scope= "page"? Specify the domain type= ""; Specify the type of class </jsp:useBean>

Example 1: Create an JavaBean object in the JSP.

<body>

<%--creates a JavaBean object that is stored in the specified page field--%>

<jsp:usebean id="MyUser" class="Cn.itcast.beans.User" scope="page"></jsp: Usebean>

<%= (User) pagecontext.findattribute ("MyUser"). GetName ()%>

</body>

The above code is translated as:

Cn.itcast.beans.User myuser = null;

Synchronized (_jspx_page_context) {

MyUser = (cn.itcast.beans.User) _jspx_page_context.getattribute ("MyUser", Pagecontext.page_scope);

if (myuser = = null) {

MyUser = new Cn.itcast.beans.User ();

_jspx_page_context.setattribute ("MyUser", MyUser, Pagecontext.page_scope);

}

}

First detects whether the specified domain contains an object of the specified property name, if it is directly obtained, if it is not created directly.

1 Setting property values

<jsp:setproperty? Specifies that the action name= "" is a set property? Specify which object to set property= ""? Specify which property to set param= ""? Specify what request parameter to use as the value of the above attribute value= ""/>? Specify a specific value if you need to use the request parameter as the value of the property, you can param, if you need to encapsulate all the request parameters to the corresponding properties of the specified object, then the property value can be directly *.

2 Getting property values

<jsp:getproperty       name= ""                   specifies the object to get property= ""/>             Specifies the property to get

3 Example for data encapsulation and acquisition

<%--creates a JavaBean object that is stored in the specified page field--%>

<jsp:usebean id="MyUser" class="Cn.itcast.beans.User" scope="page"></jsp: Usebean>

<%--Setting the JavaBean property--%>

<jsp:setproperty name="MyUser" property="*"/>à encapsulate data using request parameters

<%--Get JavaBean Property--%>

<jsp:getproperty name="MyUser" property="name"/><br/>

<jsp:getproperty name="MyUser" property="age"/><br/>

<jsp:getproperty name="MyUser" property="likes"/><br/>

Java Learning notes-jsp3 and JavaBean (35)

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.