JDBC and JSP simulate the MVC three-layer architecture

Source: Internet
Author: User

M is the exponential data model, V is the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different expressions. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure synchronization between M and V. Once M changes, V should be updated synchronously.

Model-View-controller MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. It has been widely used. Sun's J2EE platform design model has been recommended in recent years and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.

MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: model, view, and controller. They process their own tasks.

View

A view is the interface on which the user sees and interacts with it. For older Web applications, a view is an interface composed of HTML elements. In New Web applications, HTML still plays an important role in the view.

How to process the application interface becomes increasingly challenging. One major benefit of MVC is that it can process many different views for your applications. In fact, there is no real processing in the view. Whether the data is stored online or an employee list, as a view, it is just a way to output data and allow users to manipulate it.

Code implementation:

package com.accp;public class StudentForm {private int studId;private String studName;private String studAge;public int getStudId() {return studId;}public void setStudId(int studId) {this.studId = studId;}public String getStudName() {return studName;}public void setStudName(String studName) {this.studName = studName;}public String getStudAge() {return studAge;}public void setStudAge(String studAge) {this.studAge = studAge;}}

JSP:

<% SimpleDateFormat sf = new SimpleDateFormat ("MM dd, yyyy"); String dateString = sf. format (new Date (); out. print (dateString); StudentModel smodel = new StudentModel (); ArrayList list = smodel. listStudent (DbConnection. getConnection (); Iterator it = list. iterator (); StudentForm sform = null; while (it. hasNext () {sform = (StudentForm) it. next (); % ><tr ><td ><%= sform. getStudId () % ></ td ><td ><%= sform. getStudName () %>
</Td> <td ><%= sform. getStudAge () % ></ td ></ tr ><%}%>

Model

The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as EJBs and ColdFusion Components to process databases. The data returned by the model is neutral, that is, the model has nothing to do with the data format, so that a model can provide data for multiple views. Because the Code applied to the model can be reused by multiple views only once, code duplication is reduced.

Package com. accp; import java. util. arrayList; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. statement; import java. SQL. resultSet; public class StudentModel {private final String SQL _LIST = "SELECT studId, studName, studAge FROM StudentInfo"; private final String SQL _ADD = "INSERT INTO StudentInfo (studName, studAge) VALUES (?,?) "; Public ArrayList ListStudent (Connection conn) {ArrayList list = null; Statement stmt = null; ResultSet rs = null; try {list = new ArrayList (); stmt = conn. createStatement (); rs = stmt.exe cuteQuery (SQL _LIST); while (rs. next () {StudentForm sf = new StudentForm (); sf. setStudId (rs. getInt ("studId"); sf. setStudName (rs. getString ("studName"); sf. setStudAge (rs. getString ("studAge"); list. add (sf) ;}} catch (Exception e) {// TODO: handle exceptionSystem. out. println ("query exception:" + e. toString ();} finally {DbConnection. closeResultSet (rs); DbConnection. closeStatement (stmt); DbConnection. closeConnection (conn) ;}return list ;}}

Data Connection:

Package com. accp; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import java. SQL. preparedStatement; public class DbConnection {private static String DRIVER_CLASS = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; private static String DATABASE_URL =" jdbc: sqlserver: // localhost: 1433; databaseName = Student "; private static String DATABASE_USER = "Sa"; private static String DATABASE_PASSWORD = "sasa"; public static Connection getConnection () {Connection conn = null; try {Class. forName (DRIVER_CLASS); conn = DriverManager. getConnection (DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD); System. out. println ("SQL2005 connection successful! ");} Catch (Exception ex) {System. out. println ("2111:" + ex. getMessage () ;}return conn;} public static void closeConnection (Connection conn) {try {if (conn! = Null) {conn. close () ;}} catch (Exception e) {System. out. println (e. toString () ;}} public static void closeStatement (Statement stmt) {try {if (stmt! = Null) {stmt. close () ;}} catch (Exception e) {System. out. println (e. toString () ;}} public static void closeResultSet (ResultSet rs) {try {if (rs! = Null) {rs. close () ;}} catch (Exception e) {System. out. println (e. toString ());}}}

Controller

The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on a Web page and send an HTML form, the controller does not output anything or process anything. It only receives the request and determines which model component is called to process the request, and then determines which view is used to display the data returned by the model for processing.

Now let's summarize the MVC processing process. First, the Controller receives user requests and decides which model should be called for processing. Then, the model uses the business logic to process user requests and return data, finally, the controller uses the corresponding view to format the data returned by the model and presents it to the user through the presentation layer.

  1. In-depth analysis of JSP technology Advantages and Disadvantages
  2. Analysis on the functions of Action attributes in JSP
  3. JSP best practices use JSTL to update JSP pages

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.