Flexible Use of JSP and JDBC

Source: Internet
Author: User

1. <jsp: forward page = "list. jsp"/>

Equivalent

 
 
  1. <%  
  2.     request.getRequestDispatcher("list.jsp").forward(request, response);  
  3. %> 

2. <jsp: useBean class = "anni. ContactDao" id = "contactDao" scope = "application"/>

Anni. ContactDao is a bean that encapsulates database operations.

Equivalent

 
 
  1. <%  
  2.     anni.ContactDao contactDao = (anni.ContactDao) application.getAttribute("contactDao");  
  3.     if (contactDao == null) {  
  4.         contactDao = new anni.ContactDao();  
  5.         application.setAttribute("contactDao", contactDao);  
  6.     }  
  7. %> 

In the above Code, we only want to create an object instance. Why not use new? It is much less and easier to understand than writing code like this. Why should we insist on using jsp: useBean?

Note that scope = "application" in the tag, and application is one of the four scopes we have introduced.

We first obtain the object corresponding to contactDao from the application, and then determine whether the obtained object is null. If it is null, the variable has not been initialized, in this case, you need to use new to create an object instance and put it into the application. The last thing we get is the contactDao instance.

3. create. jsp contains information about form submission.

Save. jsp form submission Information Processing

Contact is the get and set methods with corresponding properties in the corresponding object bean name.

 
 
  1. <jsp:useBean class="anni.ContactDao" id="contactDao" scope="application"/> 
  2. <jsp:useBean class="anni.Contact" id="contact"/> 
  3. <jsp:setProperty name="contact" property="*"/> 
  4. <%  
  5.     contactDao.save(contact);  
  6.     response.sendRedirect("list.jsp");  
  7. %> 

Equivalent

Retrieve contactDao from application and create a contact. The scope is not specified when you create a contact. By default, the local variable is created only with new, which does not affect any scope. Jsp: setProperty is a new thing. Its function is to set data for a javabean. Previously, we used jsp: useBean to create a contact instance. Now we use name = "contact" to set the data of this instance. property can specify an attribute, for example, property = "username ", you can also use asterisks *) to batch set all the attributes that can be found. This jsp action is actually equivalent to the following code.

 
 
  1. contact.setUsername(request.getParameter("username"));  
  2. contact.setSex(request.getParameter("sex"));  
  3. contact.setEmail(request.getParameter("email"));  
  4. contact.setQq(request.getParameter("qq"));  
  5. contact.setDescn(request.getParameter("descn")); contactDao.save(contact);response.sendRedirect("list.jsp"); 
  1. How to Use Hidden in JSP webpage creation?
  2. How to accelerate JSP database access in JDBC
  3. How to accelerate JSP access
  4. Which of ASP. NET, JSP, and PHP is better?
  5. JSP-related software

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.