Java Learning (v) examples

Source: Internet
Author: User

First, demand analysis

Make a JSP page that dynamically displays the contents of the information table.

1, do an entity class: Studentinfo (contains 4 fields)

2, the simulation generates 3 data, is essentially new Studentinfo 3 instances,

Each instance represents a row of records (the following 3 records are placed in the database, this example is temporarily not processed)

3, the process of processing the 3 data, regardless of the interface (using Java code and JSP UI page separation)

Write a class and method that handles data problems with a return value of list<studentinfo>

Add 3 data (Studentinfo) to 3 studentinfo instances, adding them to the list

In other words, it is the list that represents the data set.

Attention:

A, this class is the business class (the class that processes business logic), to develop the habit: write interface first

B, when callers use this class, they should not care who is the implementation class, only the interface and its API

4, the entire list, dropped to the JSP used to display the data, showing the status of a table.

Second, the concrete operation Flow 1, the first realistic body Class A. Build package (Entity)

B. Establishment of 1 entity classes: Studnetinfo.java

C. For the entity example, add a private property, the attribute should correspond to the field one by one of the two-dimensional table

Id,name,classes,place

1  Public classStudentinfo {2     PrivateString ID;3     PrivateString name;4     PrivateString classes;5     PrivateString Place;6 }7 d. Generate Get,set with one click, as follows:8  PublicString getId () {9 returnID;Ten } One  Public voidsetId (String id) { A  This. ID =ID; -}
2, the establishment of a business logicInterfaceAndImplementation Class

A. Build Package service First

B. Establishing an interface Studentservice.java
1  Public Interface Studentservice {2     // How many business logic do you have to design how many methods 3      Public List<studentinfo> getallstudents (); 4 }
C. Creating an implementation class Studentserviceimpl.java
1 need:implements  Studentservice this interface 2publicclass  Implements  Studentservice {34 }
D. Implementing a non-implemented method

1 @Override 2      Public List<studentinfo> getallstudents () {3         // business logic code here 4         returnnull; 5     }

The specific code is as follows:

1 @Override2      PublicList<studentinfo>getallstudents () {3         //A collection of data that holds all student information4List<studentinfo> stulist =NewArraylist<studentinfo>();5         6         //simulating the removal of 3 student information from the database7Studentinfo SI1 =Newstudentinfo ();8Studentinfo Si2 =Newstudentinfo ();9Studentinfo Si3 =Newstudentinfo ();TenSi1.setid ("1"); OneSi1.setname ("Li Weiming"); ASi2.setname ("Wang Xianjun"); -Si3.setname ("Big Head"); -          the         //Store 3 students in the list collection - Stulist.add (SI1); - Stulist.add (SI2); - Stulist.add (SI3); +          -         returnstulist; +}
3. Create a JSP page and invoke the service fetch DataSet List A. Create a new JSP page

B. Invoking the service

The test code is as follows:

1 2 New Studentserviceimpl (); 3 list<studentinfo> stulist = stuservice.getallstudents (); 4 out.println (Stulist.size ()); 5 %>

Modify the code to traverse the collection (for):

1 2 New Studentserviceimpl (); 3 list<studentinfo> stulist = stuservice.getallstudents (); 4  for (Studentinfo student:stulist) {5     out.println (student.getname () + "<br>"); 6     7 }8 %>
Iii. Summary

In the process should be noted:

Java Learning (v) examples

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.