A detailed study of MVC query pattern in Javaweb learning process _java

Source: Internet
Author: User

First, understand what MVC is?

MVC is the abbreviation for Model-view-controller, the model-view-controller. It is a design pattern, and it divides the application into three core modules, models, views, controllers. They are dealing with their own tasks.

Model: 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 the user sees and interacts with. You can display the relevant data to the user, accept the user's input, but it does not do any actual business processing.

Controller: Accepts the user's input and invokes the model and view to fulfill the user's needs.

Process: Clients send requests to the server, the server sends the request to the servlet, the servlet receives the request, invokes the model layer based on the requested business logic, and then returns a result to the servlet, which turns (forwards, redirects) to a page.

Second, specific inquiries

Question: Click on a hyperlink in the page to display the student's information in the database

1. root directory structure

2.

Create a student class student.

Content properties: Gets 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 get the database information back to the student list

Content: One of the tools I've found is a tool class

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 property. Overwrite the Doget () method only. Because another page needs to get the student list can be written in a forward 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); Forwarding
}

4. Create a test.jsp to send the request.

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");
%>
 
 

6. Show

Iii. problems encountered in the learning process

1. This problem occurs when connecting to a SQL Server database.

Issue: The driver cannot establish a secure connection with SQL Server by using Secure Sockets Layer (SSL) encryption. Error: ' Server key '.

   Workaround:

This problem is a security secret key between the JDK and the database.

     The plan is:

1. Download two jar packs

1.bcprov-ext-jdk15on-1.54.jar

2.bcprov-jdk15on-1.54.jar

Download address in: http://download.csdn.net/detail/cw_hello1/9557049


2. Copy the downloaded two 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 java.security File: Java.security file under \jre\lib\security of JDK installation directory.

Find Security.provider.1=sun.security.provider.sun and replace it with

Security.provider.1=org.bouncycastle.jce.provider.bouncycastleprovider

The above is a small set up to introduce the Javaweb learning process of the MVC query model, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for your support cloud Habitat community site!

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.