JavaWeb(18) : JavaWeb的三层框架与HelloWorld程序

一、MVC结构

它不是Java独有,所有的B/S项目都在使用它!

  • M : model 模型(自己写代码)
  • V :View视图(jsp)
  • C:Controller 控制器(Servlet)

二、JavaWeb三层框架

  • Web层 : 与Web相关的内容(Servlet,JSP,四大域)
  • 业务层:业务对象(Service)
  • 数据层:操作数据库(Dao : Data Access Object)(所有对数据库的操作,不能跳出DAO之外)

三、三层框架的简易HelloWorld

index.jsp :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
<a href="${pageContext.request.contextPath }/UserServlet">点击这里查看</a>
  </body>
</html>

UserServlet :

package waf.yty.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import waf.yty.domain.User;
import waf.yty.service.UserService;

public class UserServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * servlet依赖service,通过service完成功能,把结果保存到request域中,转发到jsp
		 */
		UserService userService = new UserService();
		User user = userService.find();
		
		request.setAttribute("user", user);
		
		request.getRequestDispatcher("/show.jsp").forward(request, response);


	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
		

	}

}

UserService :

package waf.yty.service;

import waf.yty.dao.UserDao;
import waf.yty.domain.User;

public class UserService {
	// service层依赖dao层
	private UserDao userDao = new UserDao();
	/*
	 * service的查询,需要使用dao来完成!
	 */
	public User find() {
		return userDao.find();
	}

UserDao :

package waf.yty.dao;

import waf.yty.domain.User;

public class UserDao {
	/*
	 * 把xml中的数据查询出来之后,封装到user对象中返回
	 */
	public User find() {
		return new User("zhangSan","666");
	}

}

User :

package waf.yty.domain;
/**
 * 把数据库中查询的结果保存到这个对象中
 * @author yangtengyu
 *
 */
public class User {
	
	private String username;
	private String password;
	
	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;
	}
	
	
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}

}

show.jsp :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'show.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
${user.username }<br>
${user.password }<br>

  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值