Java learning notes-JSP3 and javaBean (35), jsp3javabean

Source: Internet
Author: User

Java learning notes-JSP3 and javaBean (35), jsp3javabean
JSP Configuration

JSP is essentially Servlet, so Servlet can be configured in web. xml, so JSP can also be configured in theory.

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>

Write 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/> <% -- override JSP Initialization Method -- %> <%! Public void jspInit () {System. out. println ("jsp init...") ;}%> </body>
JSP troubleshooting
1. Translation phase
<Body> <% -- </body>
Org. apache. jasper. JasperException:/error. jsp (7,8) Unterminated <% -- tag
2. compilation phase <body> <% = "itcast %> </body>
Org. apache. jasper. JasperException: Unable to compile class for JSP:
3. Running stage <body ><%= 1/0%> </body>
org.apache.jasper.JasperException: An exception occurred processing JSP page /error.jsp at line 7

Only one way to handle errors is to view the error information.

If jsp is forcibly mapped to another path during jsp configuration, you need to use one-to-multiple ing to map the original path, otherwise, configuration parameters cannot be obtained in the original path.

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

 

So far, the JavaBean mainly helps us encapsulate basic data types. It facilitates later data transmission.

Features:

The attribute value of JavaBean is not defined in JavaBean. It is the value after the get and set Methods remove letters from the first letter and convert them to lowercase letters.

JavaBean technology

The main task of JSP is to display user data. Therefore, you must assume the responsibility for encapsulating user data. Therefore, we must use the corresponding technology to encapsulate data on the JSP page. That is, the JavaBean technology in JSP.

1. Create a JavaBean object

<Jsp: useBean indicates the action name, And javabeanid = "" indicates the reference variable name, it can also be the property name beanName = "" alias of the specified object class = "" specifying the javabean class to be created (fully qualified name) scope = "page" specified field type = ""> specified class type </jsp: useBean>

Example 1: Create a javabean object in JSP.

<Body>

<% -- Create a javabean object stored in the specified page Domain -- %>

<Jsp: useBean id ="Myuser"Class ="Cn. itcast. beans. User"Scope ="Page"> </Jsp: useBean>

<% = (User) pageContext. findAttribute ("myuser"). getName () %>

</Body>

The above code is translated:

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, check whether the specified domain contains objects with the specified property name.

1. Set the property value

<Jsp: setProperty specifies the action name = "" To set which object property = "" To set which property = "" to specify which property param = "" to specify which request parameter is used as the preceding property. value = ""/> specifies the specific value. If you want to use the request parameter as the attribute value, you can use param, if you want to encapsulate all Request Parameters in the corresponding properties of the specified object, the value of the property can be *.

2. Get the property value

<Jsp: getProperty name = "" specify the object to be retrieved property = ""/> specify the property to be retrieved

3. Data encapsulation and Acquisition

<% -- Create a javabean object stored in the specified page Domain -- %>

<Jsp: useBean id ="Myuser"Class ="Cn. itcast. beans. User"Scope ="Page"> </Jsp: useBean>

<% -- Set the javabean attribute -- %>

<Jsp: setProperty name ="Myuser"Property ="*"/> À encapsulate data using Request Parameters

<% -- Get the javabean attribute -- %>

<Jsp: getProperty name ="Myuser"Property ="Name"/> <Br/>

<Jsp: getProperty name ="Myuser"Property ="Age"/> <Br/>

<Jsp: getProperty name ="Myuser"Property ="Likes"/> <Br/>

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.