JSTL标签库
4 core标签库常用标签
自定义标签
HelloTag.java
public class HelloTag implements SimpleTag {
private JspTag parent;
private PageContext pageContext;
private JspFragment jspBody;
public void doTag() throws JspException, IOException {
pageContext.getOut().print("Hello Tag!!!");
}
public void setParent(JspTag parent) {
this.parent = parent;
}
public JspTag getParent() {
return this.parent;
}
public void setJspContext(JspContext pc) {
this.pageContext = (PageContext) pc;
}
public void setJspBody(JspFragment jspBody) {
this.jspBody = jspBody;
}
}
hello.tld
MVC
1 MVC设计模式
JavaWeb经典三层框架
三层的HELLOWORLD程序:
domain:
package cn.itcast.domain;
/**
* 把数据库中查询出的结果保存到这个对象中。
* @author cxf
*
*/
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
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;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
dao:
package cn.itcast.dao;
import cn.itcast.domain.User;
public class UserDao {
/*
* 把xml中的数据查询出来之后,封装到user对象中,然后返回
*/
public User find() {
return new User("zhangSan", "123");
}
}
service :
package cn.itcast.service;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
public class UserService {
// service层依赖dao层
private UserDao userDao = new UserDao();
/*
* service的查询,需要使用dao来完成!
*/
public User find() {
return userDao.find();
}
}
servlet :
package cn.itcast.web.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 cn.itcast.domain.User;
import cn.itcast.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);
}
}
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="<c:url value='/UserServlet'/>">点击这里查看</a>
</body>
</html>
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>