1. First, let’s understand what mvc is?
MVC is the abbreviation of Model-View-Controller, that is, model-view-controller. It is a design pattern, and the application is divided into three core modules, models, views, and controllers. They each handle their own tasks.
Model: It is the main part of the application, and the model refers to the business model. A model can provide data for multiple views.
View: The interface that users see and interact with. Relevant data can be displayed to the user and input from the user, but it does not perform any actual business processing.
Controller: accepts user input and calls models and views to complete user needs.
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 requested business logic, and then thinks that the servlet returns a result and turns (forward, redirect) a page according to the result.
2. Specific inquiry
Question: Click on a hyperlink on the page to display the information of students in the database
1. Root directory structure
2.
Create a student class Student.
Content properties: Get the get, set method.
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 to the student linked table
Content: There is a tool class I found myself
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 blocke.printStackTrace();}} return students;}}}3. Create a servlet class named ListAllStudentServlet configuration property. Only rewrite the doGet() method. Because another page needs to obtain the student linked list, it can be written in a forwarded way.
content:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {StudentDao studentDao=new StudentDao();List<Student> students=studentDao.getAll();request.setAttribute("students", students); request.getRequestDispatcher("/student.jsp").forward(request, response); // Forward}4. Create a test.jsp to send the request.
Content: <a href="listAllStudent">List all students</a>
5. Create display page, student.jsp
content:
<body><%List<Student> students=(List<Student>)request.getAttribute("students");%><h3>StudentInformation Table</h3><table><ttr><th>studentId</th><th>name</th><th>idCard</th><th>sex</th><th>age</th><th>grade</th></tr><%for(Student student:students){%><tr><td><%=student.getStudentId() %></td><td><%=student.getName() %></td><td><%=student.getIdCard() %></td><td><%=student.getSex() %></td><td><%=student.getAge() %></td><td><%=student.getGrade() %></td></tr><% } %></table></body>6. Display
3. Problems encountered during learning
1. This problem occurred while connecting to the SQLserver database.
Problem: The driver cannot establish a secure connection with SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Server key".
Solution:
This problem is the problem of security keys 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
The download address is: http://download.csdn.net/detail/cw_hello1/9557049
2. Copy the two downloaded JAR files to: JDK installation directory /jre/lib/ext, for example, mine is D:/Program Files (x86)/java/JDK1.6/jre/lib/ext
3. Open the java.security file: the java.security file in the JDK installation directory /jre/lib/security.
Find security.provider.1=sun.security.provider.Sun to replace
security.provider.1=org.bouncycastle.jce.provider.BoucyCastleProvider
The above is a detailed explanation of the MVC query mode of the JavaWeb learning process introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support to the Wulin Network website!