MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写。
MVC 结构图
mvc的优点:
下面做一个简单的MVC模式下的用户登录。
1 建立动态web工程,名称为Struct2_login。
在 webContent目录下简历三个jsp文件。分别为login.jsp,success.jsp,error.jsp
在src 目录下简历如下图结构的包:
Po 包用于存放用户类等。
Servlet包用于存放创建建好的servlet.穿件servlet时会自动将servlet添加到web目录下。
Dao包用于处理相关业务。
最后结构图为:
其中login.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用户名:
密码:
"/>
success.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
登录成功-------
error.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
登录失败-------
Servlert 代码:
package Struct2_login.src.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Struct2_login.src.dao.UserDao;
import Struct2_login.src.po.User;
/**
* Servlet implementation class UserServlet
*/
publicclass UserServletextends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
//TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
User us = new User();
us.setUser(request.getParameter("user"));
us.setPassword(request.getParameter("password"));
UserDao usDao = new UserDao();
Boolean test=usDao.checkUserPassword(us);
if(test ==true){
response.sendRedirect("success.jsp");
}
else{
response.sendRedirect("error.jsp");
}
}
}
UserDao 代码:
package Struct2_login.src.dao;
import Struct2_login.src.po.User;
publicclass UserDao {
public Boolean checkUserPassword(User user){
if(user.getUser().equals("admin")&& user.getPassword().equals("123")){
returntrue;
}
else{
returnfalse;
}
}
}
User.java 代码
package Struct2_login.src.po;
publicclass User {
public Stringuser;
public Stringpassword;
public User(){
}
public String getUser() {
returnuser;
}
publicvoid setUser(String user) {
this.user = user;
}
public String getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password = password;
}
}
将项目部署到tomcat下,然后启动tomcat
访问路径为:http://localhost:8080/Struct2_login/
Struct2_login 为项目名。