java web mvc思想介绍

1、首先简介一下什么是MVC思想。

在百度百科里面对MVC的说明,MVC全名是Model View Controller。是模型(model)-视图(view)-控制器(controller)的缩写。那么在JAVA的web开发中。MVC各自是相应于:显示页面(视图)、数据对象(模型)、业务处理(控制器)。

简单举个样例来说,在学生信息管理系统里。登陆界面就是一个视图。而登陆完毕后。进行username和password推断并跳转对应的页面,就是控制器,而保存username和password的对象,就是模型。三者的关系就是这样。

我们以下给出了一个java动态网页的样例。

2、文件结构



3、所要用到的

tomcat7.0


4、代码结构

UserCheck

package com.test.dao;

import com.test.vo.User;

public class UserCheck {
	
	public boolean login(User user)
	{
		if(user.getUsername().equals("admin") && user.getPassword().equals("123456"))
			return true;
		return false;
	}

}

UserLogin

package com.test.service;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.dao.UserCheck;
import com.test.vo.User;

/**
 * Servlet implementation class UserLogin
 */
@WebServlet("/UserLogin")
public class UserLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public UserLogin() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User u=new User();
		u.setUsername(request.getParameter("username"));
		u.setPassword(request.getParameter("password"));
		System.out.println(request.getParameter("username")+" "+request.getParameter("password"));
		UserCheck dao=new UserCheck();
		if(dao.login(u))
		{
			response.sendRedirect("login_s.jsp");
		}
		else
		{
			response.sendRedirect("login_f.jsp");
		}
		
	}

}

User
package com.test.vo;

public class User {

	private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	private String password;
}

静态页面比較简单,就不贴上来了。

posted @ 2016-03-14 08:14  yxwkaifa  阅读(876)  评论(0编辑  收藏  举报