作者:mobiledu2502852147 | 来源:互联网 | 2023-10-13 12:06
1、首先简单介绍一下什么是MVC思想。在百度百科里面对MVC的说明,MVC全名是ModelViewController,是模型(model)-视图(view)-控制器(cont
1、首先简单介绍一下什么是MVC思想。
在百度百科里面对MVC的说明,MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写。那么在JAVA的web开发中,MVC分别是对应于:显示页面(视图)、数据对象(模型)、业务处理(控制器)。
简单举个例子来说,在学生信息管理系统里,登陆界面就是一个视图,而登陆完成后,进行用户名和密码判断并跳转相应的页面,就是控制器,而保存用户名和密码的对象,就是模型。三者的关系就是这样。我们下面给出了一个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;
}
静态页面比较简单,就不贴上来了。java web mvc思想介绍