English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
I. First, let's understand what MVC is?
MVC is Model-View-Abbreviation for Controller, i.e., Model-View-Controller. It is a design pattern that divides the application into three core modules: model, view, and controller. They handle their own tasks.
Model: It is the main part of the application, where the model refers to the business model. One model can provide data for multiple views.
View: It is the interface that the user sees and interacts with. It can display relevant data to the user, accept user input, but it does not perform any actual business processing.
Controller: Accepts user input and calls the model and view to complete user requirements.
Process: The client sends a request to the server, the server sends the request to the servlet, the servlet receives the request, calls the model layer according to the business logic of the request, and then returns a result to the servlet. According to the result, a page is forwarded (forward, redirect).
Second, specific query
Problem: Click a hyperlink on the page to display student information from the database
1Root directory structure
2.
Create a Student class.
Content attributes: get and set methods.
private String studentId; private String name; private String idCard; private String sex; private int age; private int grade;
2Create a StudentDao class to obtain database information and return a list of students
Content: there is a utility 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(); } 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 and configure properties. Only rewrite the doGet() method. Because another page needs to get the student list, it can be written in the form of a forward.
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 requests.
Content:<a href="listAllStudent">List all students</a>
5.Create a display page, student.jsp
Content:
<body> <% List<Student> students=(List<Student>)request.getAttribute("students"); %> <h3>Student Information Table</h3> <table> <tr> <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
III. Problems encountered during the learning process
1.This problem occurred when connecting to the sqlserver database.
Problem: The driver cannot establish a secure connection with SQL Server by using the Secure Sockets Layer (SSL) encryption. Error: "Server key".
Solution:
This problem is the security key issue between JDK and the database.
The plan 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 under JDK installation directory \jre\lib\security.
Find security.provider.1=sun.security.provider.Sun to be replaced with
security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider
The above is the detailed explanation of the MVC query mode in the JavaWeb learning process introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. I also want to express my heartfelt thanks to everyone for their support of the Yell Tutorial website!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)