<%@page pageEncoding="utf-8"%>
第1个JSP
<%!
public double bai(double d) {
return d*100;
}
%>
<%
for(int i=0;i<10;i++) {
%>
- <%=bai(Math.random()) %>
<%
}
%>
<%@include file="time.jsp"%>
<%@page pageEncoding="utf-8" contentType="text/html"
import="java.util.*,java.text.*"%>
<%
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(d);
%>
<%=time %>
package dao;
import java.util.List;
import entity.Emp;
public interface EmpDao {
List findAll();
void save(Emp e);
}
package dao;
import java.util.ArrayList;
import java.util.List;
import entity.Emp;
public class EmpDaoImpl implements EmpDao {
public List findAll() {
//模拟查询所有的员工
List list = new ArrayList();
Emp e1 = new Emp();
e1.setEmpno(1);
e1.setEname("唐僧");
e1.setJob("领导");
e1.setSal(9000.0);
list.add(e1);
Emp e2 = new Emp();
e2.setEmpno(2);
e2.setEname("悟空");
e2.setJob("职员");
e2.setSal(5000.0);
list.add(e2);
Emp e3 = new Emp();
e3.setEmpno(3);
e3.setEname("八戒");
e3.setJob("职员");
e3.setSal(6000.0);
list.add(e3);
return list;
}
//模拟增加一个员工
public void save(Emp e) {
System.out.println("增加了员工: " + e.getEname());
}
}
package entity;
import java.io.Serializable;
/**
* 建议:
* 1.尽量使用封装类型,因为它比基本类型多了null
* 2.使用java.sql包下的日期,因为JDBC支持这样的日期类型
*
*/
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Double sal;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
<%@page pageEncoding="utf-8" import="entity.*,dao.*,java.util.*"%>
查询员工
编号
姓名
职位
薪资
<%
EmpDao dao = new EmpDaoImpl();
List list = dao.findAll();
if(list != null){
for(Emp e : list) {
%>
<%=e.getEmpno() %>
<%=e.getEname() %>
<%=e.getJob() %>
<%=e.getSal() %>
<%
}
}
%>
package web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import dao.EmpDaoImpl;
import entity.Emp;
public class FindEmpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//查询所有的员工(处理业务)
EmpDao dao = new EmpDaoImpl();
List list = dao.findAll();
//(转发):让jsp继续完成这个请求
//将数据绑定到request上
req.setAttribute("emps", list);
//将请求转发给emp_list.jsp
//当前: /jsp2/findEmp
//目标: /jsp2/emp_list.jsp
req.getRequestDispatcher("emp_list.jsp").forward(req, res);
}
}
findEmp
web.FindEmpServlet
findEmp
/findEmp
<%@page pageEncoding="utf-8" import="java.util.*,entity.*"%>
查询员工
编号
姓名
职位
薪资
<%
List list = (List)request.getAttribute("emps");
if(list != null){
for(Emp e : list) {
%>
<%=e.getEmpno() %>
<%=e.getEname() %>
<%=e.getJob() %>
<%=e.getSal() %>
<%
}
}
%>
package entity;
import java.io.Serializable;
public class Course implements Serializable {
//对象的属性
private Integer courseId;
private String name;
private Integer days;
//Bean属性:和get/set对应的属性叫Bean属性
//1.去掉get/set剩下的单词,首字母小写
//2.通过get/set方法所分析出来的属性
//Bean属性通常都和对象属性一致,也可以修改为不一致
public Integer getId() {
return courseId;
}
public void setId(Integer courseId) {
this.courseId = courseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDays() {
return days;
}
public void setDays(Integer days) {
this.days = days;
}
}
package entity;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private Integer age;
private String sex;
private String[] interests;
private Course course;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Course;
import entity.Student;
public class FindStudentServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//1.获取参数(略)
//2.模拟查询学生(查)
Student s = new Student();
s.setName("zhangsan");
s.setAge(25);
s.setSex("M");
s.setInterests(new String[]{"篮球","足球","排球"});
Course c = new Course();
c.setId(1);
c.setName("Java");
c.setDays(85);
s.setCourse(c);
//3.将数据绑定到request上
req.setAttribute("stu", s);
//4.将请求转发给student.jsp
//当前:/jsp3/findStudent
//目标:/jsp3/find_student.jsp
req.getRequestDispatcher("find_student.jsp").forward(req, res);
}
}
<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="/dany_tags" prefix="s" %>
查询学生EL和JSTL
EL
姓名:${stu.name }
年龄:${stu['age'] }
课程:${stu.course.id }
性别:${requestScope.stu.sex }
年龄+3:${stu.age+3 }
介于20-30间:${stu.age>20 && stu.age<30 }
是否为空:${empty stu.interests }
参数:${param.user }
JSTL
性别:
男
女
性别:
男
女
${i }
这是我自己的标签库
Dany
3.1
s
/dany_tags
用来输出服务器的时间
sysdate
web.SysdateTag
empty
用来设置时间的格式
format
false
true
java.lang.String
package web;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 自定义标签
* 1.编写java类,继承SimpleTagSupport
* 2.在doTag方法中添加处理逻辑
* 3.配置标签说明文件
*/
public class SysdateTag extends SimpleTagSupport {
private String format = "yyyy/MM/dd HH:mm:ss";
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void doTag() throws JspException, IOException {
//创建服务器时间
Date date = new Date();
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat(format);
String now = sdf.format(date);
//将时间输出给浏览器
//PageContext extends JspContext
//该方法声明返回JspContext,
//但在实现时返回的是PageContext。
//所以可以将其强转为PageContext,
//从而获取其他8个隐含对象。
PageContext ctx = (PageContext) getJspContext();
JspWriter out = ctx.getOut();
out.println(now);
//此处一定不能关闭流,因为其他的标签
//也要用这个流。
}
}