Query in MVC mode of JavaWeb learning process, javawebmvc
I have been learning javaweb for the past few days, but it is always interrupted. Now I have time to learn about the query mode in MVC.
I. First, I want to know what mvc is?
MVC is short for Model-View-Controller, that is, Model-View-Controller. It is a design model. Its Applications are divided into three core modules, models, views, and controllers. They process their own tasks.
Model: it is the main part of an application, and the model refers to the business model. A model can provide data for multiple views.
View: displays and interacts with the user. It can display relevant data to users and accept user input, but it does not perform any actual business processing.
Controller: receives user input and calls models and views to fulfill user requirements.
Process: the client sends a request to the server, and the server sends the request to the servlet. The servlet receives the request, calls the model layer according to the request's business logic, and then wants the servlet to return a result, redirect a page based on the result.
II. Specific Query
Question: click a hyperlink on the page to display the student information in the database.
1. root directory structure
2.
Create a Student.
Content attributes: get and set methods.
private String studentId; private String name; private String idCard; private String sex; private int age; private int grade;
2. Create a StudentDao class to obtain database information and return the student linked list
Content: A Tool class that I have found
public class StudentDao { public List<Student> getAll(){ List<Student> students=new ArrayList<Student>(); ResultSet rs=null; try { String sql ="select studentId,name,idCard,sex,age,grade from student"; rs=DBConnection.executeQuery(sql); while(rs.next()){ String studentId=rs.getString(1); String name=rs.getString(2); String idCard=rs.getString(3); String sex=rs.getString(4); int age=rs.getInt(5); int grade=rs.getInt(6); Student student=new Student(studentId, name, idCard, sex, age, grade); students.add(student); } } catch (Exception e) { e.printStackTrace(); }finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return students; }}
3. Create a servlet class named ListAllStudentServlet Configuration Attribute. Only rewrite the doGet () method. Because another page needs to obtain the student linked list, it can be written by means of forwarding.
Content:
1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 4 StudentDao studentDao = new StudentDao (); 5 List <Student> students = studentDao. getAll (); 6 request. setAttribute ("students", students); 7 request. getRequestDispatcher ("/student. jsp "). forward (request, response); // forward 8}
4. Create a test. jsp file for sending requests.
Content: <a href = "listAllStudent"> List all students </a>
5. Create a display page, student. jsp
Content:
1 <body> 2 <% 3 List <Student> students = (List <Student>) request. getAttribute ("students "); 4%> 5
6. Display
Iii. Problems Encountered during learning
1. This problem occurs when you connect to the sqlserver database.
Problem: the driver cannot establish a secure connection with SQL Server through Secure Socket Layer (SSL) encryption. Error: "Server key ".
Solution:
This problem is caused by the security key between the JDK and the database.
The solution is:
1. Download two jar packages
1. bcprov-ext-jdk15on-1.54.jar
2. bcprov-jdk15on-1.54.jar
In: http://download.csdn.net/detail/cw_hello1/9557049
2. copy the downloaded two JAR Files to the JDK installation directory \ jre \ lib \ ext. For example, my Files are D: \ Program Files (x86) \ java \ JDK1.6 \ jre \ lib \ ext
3. Open the java. security File: the java. security file under the JDK installation directory \ jre \ lib \ security.
Replace security. provider.1 = sun. security. provider. Sun
Security. provider.1 = org. bouncycastle. jce. provider. BouncyCastleProvider
Re-execute the Database Connection Program. This method