1. <jsp: forward page = "list. jsp"/>
Equivalent
- <%
- request.getRequestDispatcher("list.jsp").forward(request, response);
- %>
2. <jsp: useBean class = "anni. ContactDao" id = "contactDao" scope = "application"/>
Anni. ContactDao is a bean that encapsulates database operations.
Equivalent
- <%
- anni.ContactDao contactDao = (anni.ContactDao) application.getAttribute("contactDao");
- if (contactDao == null) {
- contactDao = new anni.ContactDao();
- application.setAttribute("contactDao", contactDao);
- }
- %>
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.
- <jsp:useBean class="anni.ContactDao" id="contactDao" scope="application"/>
- <jsp:useBean class="anni.Contact" id="contact"/>
- <jsp:setProperty name="contact" property="*"/>
- <%
- contactDao.save(contact);
- response.sendRedirect("list.jsp");
- %>
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.
- contact.setUsername(request.getParameter("username"));
- contact.setSex(request.getParameter("sex"));
- contact.setEmail(request.getParameter("email"));
- contact.setQq(request.getParameter("qq"));
- contact.setDescn(request.getParameter("descn")); contactDao.save(contact);response.sendRedirect("list.jsp");
- How to Use Hidden in JSP webpage creation?
- How to accelerate JSP database access in JDBC
- How to accelerate JSP access
- Which of ASP. NET, JSP, and PHP is better?
- JSP-related software