It mainly introduces the mainstream Java Web programming techniques, design patterns and frameworks, and how to develop Web applications using Eclipese.
Key points: 1, Java Web programming of the main component technology;
2, MVC design mode;
3. Build a Java Web application based on the MVC pattern with eclipse.
Purpose: Learn how to build an MVC-based Java Web application with Eclipse.
First, the main component technology of Java Web programming
Html, JSP, Servlet, Javabean, Jdbc, Xml, Tomcat, MySQL
1.1 MVC design Pattern
Today, most of the applications we see are based on the B/s (browser/server) architecture, where the server is the Web server. Thus, Web applications are a widely used application model.
Development of Web program development methods:
Model 1 of the JSP
Model 2 of the JSP
Mvc
1 model 1 and Model 2
For Java's Dynamic Web programming technology, it has gone through the Model 1 and Model 2 eras.
Model 1:model 1 is the JSP big line of the era, in Model 1 mode, the entire Web application is composed of almost all JSP pages, JSP page receives processing client requests, the request is processed directly to respond. Use a small amount of JavaBean to handle database connections, database access, and so on.
Model 2:model 2 JSP no longer assumes the responsibility of the controller, it is only a performance layer role, only to render the results to the user, JSP page request and servlet (Controller) interaction, and servlet is responsible for the JavaBean communication with the background.
In Model 2 mode, models are played by JavaBean, the view is played by the JSP page, and the controller is acted upon by the servlet.
2 Comparison of Model 1 and Model 2
For very small web sites, if the late update, maintenance work is not particularly large, you can use the Model 1 mode to develop the application, instead of using Model 2 mode.
Although Model 2 provides better scalability and maintainability, it increases upfront development costs. To some extent, Model 2 is designed to reduce the complexity of the system's later maintenance, which leads to a higher degree of complexity in earlier development.
3 MVC Ideas
MVC is not a unique design idea of the Java language, nor is it a unique idea for Web applications, which is the norm that all object-oriented programming languages should obey.
The MVC idea divides an application into three basic parts: model, view, and Controller, three of which work together with minimal coupling to improve the scalability and maintainability of the application.
MVC:M (model) refers to the data models,
V (View) refers to the user interface,
C (Control) is the controller.
The purpose of using MVC is to separate the implementation code for M and v so that the same program can use a different representation.
The separation of models, views, and controllers allows a model to have multiple display views. If a user changes the model's data through a view's controller, all other views that depend on the data should be reflected in these changes. Therefore, whenever any data changes occur, the controller notifies all views of the change, causing the updates to be displayed. This is actually a model of the change-propagation mechanism. The relationships between models, views, Controller 3, and their main functions.
Part of MVC
The view represents the user interface, which is the HTML interface for Web applications.
Model: The processing of business processes/States and the development of business rules.
Control (Controller): Can be understood as a dispatcher, he decides what model to choose, what kind of view to choose, what kind of user request can be completed, the control layer does not do any data processing.
The benefits of MVC:
MVC is fundamentally forced to separate them, maximizing the separation of program code from the Web page, and data and business rules are separated from the presentation layer, so you can maximize the reuse of code.
4 Common MVC Framework
Struts: Includes two struts1 and struts2.
JSF:JSF is a standard, currently, JSF is an integral part of Jee 5.0, published with JEE 5.0
JSF's behavior is implemented in Pojo, and JSF's managed beans do not inherit any particular class. Therefore, there is no need to implement a redundant controller layer between the form and the model object. There are no controller objects in JSF, and the controller behavior is implemented through model objects.
The JSF event framework can be refined to each field in the form. JSF is still based on Jsp/servlet, still jsp/servlet architecture, so the learning curve is relatively simple
Ii. Building a Java Web application based on the MVC pattern with eclipse
Use JSP, servlet, and JavaBean to build a simple login system according to the MVC design pattern.
(1) Create a project in eclipse first
file-"new-" Dynamic Web Project
Project name fill in first and then click Next
On the last page, select Generate Web. XML Deployment Descriptor
(2) New JSP
Select the WebContent right mouse button under first new-"JSP File new name is index.jsp
Click Next to finish directly
Add source code in body
<body><form action= "Loginservlet" method= "POST" > <input type= "text" Name= " Username "size=" > <br><br>: <input type= "Password" Name= "Password" size= "> <br><br> <input type=" Submit " Name=" submit " value=" Sign in ><br> </form></body>
(3) New JavaBean
Select Item First Right-click new-Class input Name:user
Click Next to complete directly, modify the code as follows
Public classUser {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}
(4) New servlet
Select Project First right-click new-Servlet Input Class Name:loginservlet
Click Next to complete directly
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubUser u=NewUser (); String xm= ""; String ps= ""; XM=request.getparameter ("username"); PS=request.getparameter ("Password"); U.setusername (XM); U.setpassword (PS); HttpSession s=request.getsession (true); S.setattribute ("Username", U.getusername ()); Response.sendredirect ("Main.jsp"); }
(5) New main.jsp
Select the WebContent right mouse button under first new-"JSP File new name is main.jsp
Click Next to finish directly
Add source code in body
<body>, you successfully login, 20 seconds after automatic access to the main page!
(6) Operation
Select Index.jsp Right-click Run as->run on Server
Javabean+servlet+jsp Program _ Personal hard exploration