PoeticalJustice

导航

JAVAWEB 一一 userweb1(原生,非servlet版)

 创建数据库和表

 

首先,创建一个web项目

 

然后引入jar包

 

创建jsp页面

 

创建包

 

创建接口

实现类

详细内容

首先创建一个登陆页面 login.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 'Login.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>
    <form action="jsp/dologin.jsp" method="post">
        用户名:<input type="text" name="username"/><br/>
        密码 :<input type="password" name="password"/> <br/>
        <input type="submit" value="登录"/>    
        <input type="reset" value="重置"/>
       </form>
  </body>
</html>

  

 

创建要跳转的页面 (充当半个控制器servlet)

 dologin.jsp

<%@page import="com.user.service.impl.UserServiceImpl"%>
<%@page import="com.user.service.UserService"%>
<%@ 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 'dologin.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>
    <% 
    //dologin相当于一个servlet
    //设置前台页面参数  编码格式
    	request.setCharacterEncoding("UTF-8");
    	//获取前台页面参数
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	//  调用Service 层方法 判断是否成功
    	UserService service = new UserServiceImpl();
    	boolean islogin = service.isLogin(username, password);
    	
    	if(islogin){//登录成功 跳转到sucess.jsp页面 否则 跳转到login.jsp
    	//转发
    		request.getRequestDispatcher("success.jsp").forward(request,response);
    		
    	}else{//登录失败
    		//重定向
    		response.sendRedirect("Login.jsp");
    	}
    	
    %>
  </body>
</html>

  成功跳转

  success.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 'success.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>
    <h1>登录成功</h1><br>
  </body>
</html>

  

 

  否则

   Login.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 'Login.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>
    <form action="jsp/dologin.jsp" method="post">
    	用户名:<input type="text" name="username"/><br/>
    	密码 :<input type="password" name="password"/> <br/>
    	<input type="submit" value="登录"/>    
    	<input type="reset" value="重置"/>
   	</form>
  </body>
</html>

  

 User

entity层

 

 

实体类

User.java

get set 方法 有参无参构造器

 1 package com.user.entity;
 2 
 3 public class User {
 4     
 5     private String username;
 6     private String password;
 7     private String job;
 8     private String email;
 9     private int age;
10     public String getUsername() {
11         return username;
12     }
13     public void setUsername(String username) {
14         this.username = username;
15     }
16     public String getPassword() {
17         return password;
18     }
19     public void setPassword(String password) {
20         this.password = password;
21     }
22     public String getJob() {
23         return job;
24     }
25     public void setJob(String job) {
26         this.job = job;
27     }
28     public String getEmail() {
29         return email;
30     }
31     public void setEmail(String email) {
32         this.email = email;
33     }
34     public int getAge() {
35         return age;
36     }
37     public void setAge(int age) {
38         this.age = age;
39     }
40     public User(String username, String password, String job, String email,
41             int age) {
42         super();
43         this.username = username;
44         this.password = password;
45         this.job = job;
46         this.email = email;
47         this.age = age;
48     }
49     public User() {
50         super();
51     }
52     
53 }

  

service层

接口 UserService.java

package com.user.service;

public interface UserService {

    public boolean isLogin(String username,String password);
}

  

实现类 UserServiceImpl.java


package com.user.service.impl;

import com.user.dao.UserDao;
import com.user.dao.impl.UserDaoImpl;
import com.user.entity.User;
import com.user.service.UserService;

public class UserServiceImpl implements UserService {

    public boolean isLogin(String username, String password) {
        
        UserDao userDao = new UserDaoImpl();
        User user = userDao.getUserByName(username);
        if(user!=null){
            String pwd= user.getPassword();
            if(pwd.equals(password)){
                return true;
            }
            return false;
            
        }else{
            return false;
        }
    }

 

dao层

接口 UserDao.java

1 package com.user.dao;
2 
3 import com.user.entity.User;
4 
5 public interface UserDao {
6     
7     public User getUserByName(String username);
8 
9 }

  

实现类 UserDaoImpl.java

 

 1 package com.user.dao.impl;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 
 6 import com.user.dao.BaseDao;
 7 import com.user.dao.UserDao;
 8 import com.user.entity.User;
 9 
10 public class UserDaoImpl implements UserDao {
11     BaseDao dao = new BaseDao();
12     public User getUserByName(String username) {
13         //
14         String sql ="select * from users where username = ?";
15         Object [] obj = new Object[]{ username};
16         ResultSet rs = dao.executeQuery(sql, obj);
17         User user  =null;
18         try {
19             while(rs.next()){
20                 String password = rs.getString("password");
21                 String job = rs.getString("job");
22                 String email = rs.getString("email");
23                 int age = rs.getInt("age");
24                 user = new User(username, password, job, email, age);
25             }
26             return user;
27         } catch (SQLException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }finally{
31             dao.closeConnection();
32         }
33         return null;
34     }
35
36 }

dao层  

basedao,java

package com.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 数据库操作的基类
 * @author YangKe
 *
 */
public class BaseDao {
    
    protected Connection conn;
    protected PreparedStatement ps; 
    protected ResultSet rs;
    protected String sql;
    //获取连接
    public Connection getConnection(){
        try {
            //获取上下文对象
            Context ctx = new InitialContext();
            //从上下文中查找数据源
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/emp");
            //从数据源中获取连接
            conn = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    //关闭连接释放资源
    public void closeConnection(){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //通过JDBC来对数据库进行查询操作
    public ResultSet executeQuery(String sql, Object[] obj ){
        //获取连接
        conn = getConnection();
        try {
            //预编译SQL
            ps= conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                //给占位符赋值
                ps.setObject(i+1, obj[i]);
            }
            //执行SQL语句,获取结果集
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    //通过JDBC来对数据库进行更新操作
    public boolean executeUpdate(String sql, Object[] obj ){
        //获取连接
        conn = getConnection();
        try {
            //预编译SQL
            ps= conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                //给占位符赋值
                ps.setObject(i+1, obj[i]);
            }
            //执行SQL语句,获取该更新语句实际影响的行数
            int count = ps.executeUpdate();
            //如果行数大于0,表示更新操作成功
            if(count>0){
                return true;
            //否则表示更新操作失败
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Login.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 'Login.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>
    <form action="jsp/dologin.jsp" method="post">
    	用户名:<input type="text" name="username"/><br/>
    	密码 :<input type="password" name="password"/> <br/>
    	<input type="submit" value="登录"/>    
    	<input type="reset" value="重置"/>
   	</form>
  </body>
</html>

  

dologin.jsp

<%@page import="com.user.service.impl.UserServiceImpl"%>
<%@page import="com.user.service.UserService"%>
<%@ 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 'dologin.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>
    <% 
    //dologin相当于一个servlet
    //设置前台页面参数  编码格式
        request.setCharacterEncoding("UTF-8");
        //获取前台页面参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //  调用Service 层方法 判断是否成功
        UserService service = new UserServiceImpl();
        boolean islogin = service.isLogin(username, password);
        
        if(islogin){//登录成功 跳转到sucess.jsp页面 否则 跳转到login.jsp
        //转发
            request.getRequestDispatcher("success.jsp").forward(request,response);
            
        }else{//登录失败
            //重定向
            response.sendRedirect("Login.jsp");
        }
        
    %>
  </body>
</html>

  

success .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 'success.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>
    <h1>登录成功</h1><br>
  </body>
</html>

  

 也可以直接跳转到别的页面比如list.jsp 一个雇员信息列表的页面 但是  这个页面的数据是从数据库查出来的(这样才是动态页面啊)

那么就需要再做一遍上面的步骤  (创建Emp接口和实现类 还有dolist页面 list页面)

 


Emp

entity层

Emp.java

package com.user.entity;

import java.util.Date;

public class Emp {
    private int empno;
    private String ename;
    private String job;
    private int mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;
    
    public Emp() {
        super();
    }
    public Emp(int empno, String ename, String job, int mgr, Date hiredate,
            double sal, double comm, int deptno) {
        super();
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }

    public int getEmpno() {
        return empno;
    }
    public void setEmpno(int 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 int getMgr() {
        return mgr;
    }
    public void setMgr(int mgr) {
        this.mgr = mgr;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public double getSal() {
        return sal;
    }
    public void setSal(double sal) {
        this.sal = sal;
    }
    public double getComm() {
        return comm;
    }
    public void setComm(double comm) {
        this.comm = comm;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }    
}

  

service层

接口 EmpService.java

package com.user.service;

import java.util.List;

import com.user.entity.Emp;

/**
 * @author YangKe
 *
 */
public interface EmpService {
    
    public List<Emp> getEmpList();
    
    public Emp getEmpById(int empno);
    
    public List<Emp> getEmpByName(String ename);
    
    public boolean addEmp(Emp emp);
    
    public boolean updateEmp(Emp emp);
    
    public boolean delEmpById(int empno);
                
}

  

实现类 EmpServiceImpl

package com.user.service.impl;

import java.util.List;

import com.user.dao.EmpDao;
import com.user.dao.impl.EmpDaoImpl;
import com.user.entity.Emp;
import com.user.service.EmpService;

public class EmpServiceImpl implements EmpService {

    EmpDao dao = new EmpDaoImpl();
    public List<Emp> getEmpList() {
        // TODO Auto-generated method stub
        
        return dao.getEmpList();
    }

    public Emp getEmpById(int empno) {
        // TODO Auto-generated method stub
        return null;
    }

    public List<Emp> getEmpByName(String ename) {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean addEmp(Emp emp) {
        return dao.addEmp(emp);
    }

    public boolean updateEmp(Emp emp) {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean delEmpById(int empno) {
        // TODO Auto-generated method stub
        return false;
    }

}

  

dao层

接口 EmpDao.java

package com.user.dao;

import java.util.List;

import com.user.entity.Emp;

public interface EmpDao {
    //获取雇员列表
    public List<Emp> getEmpList();
    //根据雇员编号查某个雇员
    public Emp getEmpByNo(int empno);
    //根据名字来查雇员
    public List<Emp> getEmpByName(String name);
    //新增雇员
    public boolean addEmp(Emp emp);
    //修改雇员
    public boolean updateEmp(Emp emp);
    //删除雇员
    public boolean delEmpById(int empno);        
}

  

实现类 EmpDaoImpl.java

package com.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.user.dao.BaseDao;
import com.user.dao.EmpDao;
import com.user.entity.Emp;

public class EmpDaoImpl implements EmpDao {

    BaseDao dao = new BaseDao();
    public List<Emp> getEmpList() {
        String sql = "select * from emp ";
        Object [] obj = new Object[]{};
        ResultSet rs = dao.executeQuery(sql, obj);
        List<Emp> list = new ArrayList<Emp>();
        try {
            while(rs.next()){
                
                String ename = rs.getString("ename");
                int empno =rs.getInt("empno");
                String job = rs.getString("job");
                int mgr  = rs.getInt("mgr");
                Date hiredate = rs.getDate("hiredate");
                double sal = rs.getDouble("sal");
                double comm = rs.getDouble("comm");
                int deptno = rs.getInt("deptno");
                Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
                list.add(emp);
            }
            return list;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return null;
    }

    public Emp getEmpByNo(int empno) {
        // TODO Auto-generated method stub
        return null;
    }

    public List<Emp> getEmpByName(String name) {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean addEmp(Emp emp) {
        String sql = "insert into emp(empno,ename,job,mgr,sal,comm,deptno) values(?,?,?,?,?,?,?)";
        Object[] obj = new Object[] { emp.getEmpno(), emp.getEname(),
                emp.getJob(), emp.getMgr(), emp.getSal(), emp.getComm(),
                emp.getDeptno() };
        return dao.executeUpdate(sql, obj);
    }

    public boolean updateEmp(Emp emp) {
        // TODO Auto-generated method stub
        return false;
    }

    public boolean delEmpById(int empno) {
        // TODO Auto-generated method stub
        String sql = "delete from emp where empno= ?";
        Object[]obj = new Object[]{empno};
        
        return dao.executeUpdate(sql, obj);
        
    }

}

  

 查

dolist.jsp

这个页面的数据是从数据库查出来的(这样才是动态页面啊)

所以 我们需要跳转到一个dolist页面 (充当servlet)让他把页面从数据来出来后跳转到list页面

<%@page import="com.user.entity.Emp"%>
<%@page import="com.user.service.EmpService"%>
<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@ 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 'dolist.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>
    <% 
    //设置前台页面参数  编码格式
       request.setCharacterEncoding("UTF-8");
       
    EmpService service =new EmpServiceImpl();
    List<Emp>list=service.getEmpList();
    //转发到list。jsp
    request.setAttribute("list", list);
    request.getRequestDispatcher("list.jsp").forward(request, response);
    
    %>
  </body>
</html>

  

list.jsp

<%@page import="com.user.entity.Emp"%>
<%@ 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 'list.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="jsp/addEmp.jsp" 新增雇员>新增雇员</a>
    <table bordercolor="red" boder="1px">
        <thead>
            <tr>
                <td>雇员编号</td>
                <td>雇员姓名</td>
                <td>工作</td>
                <td>经理编号</td>
                <td>入职日期</td>
                <td>薪水</td>
                <td>津贴</td>
                <td>部门编号</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <% 
            request.setCharacterEncoding("UTF-8");
            
            List<Emp>list=(List<Emp>)request.getAttribute("list");
            for(int i = 0; i<list.size();i++){
            Emp emp = list.get(i);
            
            %>
            <tr>
                <td><%=emp.getEmpno()%></td>
                <td><%=emp.getEname()%></td>
                <td><%=emp.getJob()%></td>
                <td><%=emp.getMgr()%></td>
                <td><%=emp.getHiredate()%></td>
                <td><%=emp.getSal()%></td>
                <td><%=emp.getComm()%></td>
                <td><%=emp.getDeptno()%></td>
                <td>
                <a href="#">修改</a>
                <a href="jsp/dodel.jsp?empno=<%=emp.getEmpno()%>">删除</a>
                </td>
            </tr>
            
        </tbody>
        
              <%}%>
    </table>
     <br>
  </body>
</html>

  

 

 

增:

在list展示页面 a标签对应的一个添加雇员的页面

 

 

addEmp.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 'addEmp.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>
   <form action="jsp/doadd.jsp">
       员工编号<input name ="empno"/><br/>
       员工姓名<input name ="ename"/><br/>
       员工工作<input name ="job"/><br/>
       经理编号<input name ="mgr"/><br/>
       入职日期<input name ="hiredate"/><br/>
       薪水<input name ="sal"/><br/>
       部门编号<input name ="deptno"/><br/>
       <input type="submit" value="提交"/>
       <input type="reset" value="重置"/><br/>
       </form>
  </body>
</html>

  

 

doadd .jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@ 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 'doadd.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>
    <% 
                request.setCharacterEncoding("UTF-8");
                String empnoStr=request.getParameter("empno");
                String ename=request.getParameter("ename");
                String job=request.getParameter("job");
                String mgrStr=request.getParameter("mgr");
        //        String hiredateStr=request.getParameter("hiredate");
                String salStr=request.getParameter("sal");
                String deptnoStr=request.getParameter("deptno");
                //格式转化
                int empno = Integer.parseInt(empnoStr);
            //    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                
                int mgr = Integer.parseInt(mgrStr);
            //    Date hiredate = sdf.parse(hiredateStr);
                double sal = Double.parseDouble(salStr);
                int deptno= Integer.parseInt(deptnoStr);
                // 封装
                Emp emp  = new Emp(empno,ename,job,mgr,sal,deptno);
                EmpService service = new EmpServiceImpl();
                boolean isAdd = service.addEmp(emp);
//                 if(isAdd){
                    
//                 }else{
//                 }
                response.sendRedirect("dolist.jsp");
    %>
  </body>
</html>

  

  

dolist.jsp

<%@page import="com.user.entity.Emp"%>
<%@page import="com.user.service.EmpService"%>
<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@ 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 'dolist.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>
    <% 
    //设置前台页面参数  编码格式
       request.setCharacterEncoding("UTF-8");
       
    EmpService service =new EmpServiceImpl();
    List<Emp>list=service.getEmpList();
    //转发到list。jsp
    request.setAttribute("list", list);
    request.getRequestDispatcher("list.jsp").forward(request, response);
    
    %>
  </body>
</html>

  

list.jsp

<%@page import="com.user.entity.Emp"%>
<%@ 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 'list.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="jsp/addEmp.jsp" 新增雇员>新增雇员</a>
    <table bordercolor="red" boder="1px">
        <thead>
            <tr>
                <td>雇员编号</td>
                <td>雇员姓名</td>
                <td>工作</td>
                <td>经理编号</td>
                <td>入职日期</td>
                <td>薪水</td>
                <td>津贴</td>
                <td>部门编号</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <% 
            request.setCharacterEncoding("UTF-8");
            
            List<Emp>list=(List<Emp>)request.getAttribute("list");
            for(int i = 0; i<list.size();i++){
            Emp emp = list.get(i);
            
            %>
            <tr>
                <td><%=emp.getEmpno()%></td>
                <td><%=emp.getEname()%></td>
                <td><%=emp.getJob()%></td>
                <td><%=emp.getMgr()%></td>
                <td><%=emp.getHiredate()%></td>
                <td><%=emp.getSal()%></td>
                <td><%=emp.getComm()%></td>
                <td><%=emp.getDeptno()%></td>
                <td>
                <a href="#">修改</a>
                <a href="jsp/dodel.jsp?empno=<%=emp.getEmpno()%>">删除</a>
                </td>
            </tr>
            
        </tbody>
        
              <%}%>
    </table>
     <br>
  </body>
</html>

  

list.jsp页面

添加一个 删除连接

dodel.jsp

<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@page import="com.user.service.EmpService"%>
<%@ 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 'dodel.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><% 
    request.setCharacterEncoding("UTF-8");
    String empnoStr=request.getParameter("empno");
    //格式转化
    int empno = Integer.parseInt(empnoStr);
    EmpService service = new EmpServiceImpl();
    boolean isDel = service.delEmpById(empno);
//     if(isDel){
//     }
    request.getRequestDispatcher("dolist.jsp").forward(request, response);
    %>
  </body>
</html>

  dolist.jsp

<%@page import="com.user.entity.Emp"%>
<%@page import="com.user.service.EmpService"%>
<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@ 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 'dolist.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>
	<% 
    //设置前台页面参数  编码格式
   	request.setCharacterEncoding("UTF-8");
   	
	EmpService service =new EmpServiceImpl();
	List<Emp>list=service.getEmpList();
	//转发到list。jsp
	request.setAttribute("list", list);
	request.getRequestDispatcher("list.jsp").forward(request, response);
	
	%>
  </body>
</html>

  list.jsp

<%@page import="com.user.entity.Emp"%>
<%@ 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 'list.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="jsp/addEmp.jsp" 新增雇员>新增雇员</a>
    <table bordercolor="red" boder="1px">
    	<thead>
    		<tr>
    			<td>雇员编号</td>
    			<td>雇员姓名</td>
    			<td>工作</td>
    			<td>经理编号</td>
    			<td>入职日期</td>
    			<td>薪水</td>
    			<td>津贴</td>
    			<td>部门编号</td>
    			<td>操作</td>
    		</tr>
    	</thead>
    	<tbody>
    		<% 
    		request.setCharacterEncoding("UTF-8");
    		
    		List<Emp>list=(List<Emp>)request.getAttribute("list");
    		for(int i = 0; i<list.size();i++){
    		Emp emp = list.get(i);
    		
    		%>
    		<tr>
    			<td><%=emp.getEmpno()%></td>
    			<td><%=emp.getEname()%></td>
    			<td><%=emp.getJob()%></td>
    			<td><%=emp.getMgr()%></td>
    			<td><%=emp.getHiredate()%></td>
    			<td><%=emp.getSal()%></td>
    			<td><%=emp.getComm()%></td>
    			<td><%=emp.getDeptno()%></td>
    			<td>
    			<a href="jsp/dopreupdate.jsp?empno=<%=emp.getEmpno()%>">修改</a>
    			<a href="jsp/dodel.jsp?empno=<%=emp.getEmpno()%>">删除</a>
    			</td>
    		</tr>
    		
    	</tbody>
    	
    	  	<%}%>
    </table>
     <br>
  </body>
</html>

  

 

 改

 改的话  多一步 因为 你要先根据条件从数据库查出你要改的那条记录

然后用一个页面接收 然后修改 提交 然后 再查一下表

 list.jsp页面中添加一个连接

 

dopreupdate.jsp

<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@page import="com.user.entity.Emp"%>
<%@page import="com.user.service.EmpService"%>
<%@ 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 'dopreupdate.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>
    <%
    	request.setCharacterEncoding("UTF-8");
    	String empnoStr = request.getParameter("empno");
    	int empno = Integer.parseInt(empnoStr);
    	EmpService service= new EmpServiceImpl();
    	Emp emp =service.getEmpById(empno);
    	request.setAttribute("emp", emp);
    	request.getRequestDispatcher("preupdate.jsp").forward(request, response);
    	
     %> 
  </body>
</html>

  

preupdate.jsp

<%@page import="com.user.entity.Emp"%>
<%@ 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 'preupdate.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>
    <%
    	request.setCharacterEncoding("UTf-8");
    	Emp emp = (Emp)request.getAttribute("emp");
    %>
    <form action="jsp/doupdate.jsp">
    	员工编号:<input name ="empno" disabled="disabled" value = "<%=emp.getEmpno() %>"/><br/>
    	员工姓名:<input name ="ename"value = "<%=emp.getEname()%>"/><br/>
    	员工工作:<input name ="job"value = "<%=emp.getEmpno() %>"/><br/>
    	经理编号:<input name ="mgr"value = "<%=emp.getMgr() %>"/><br/>
      	入职日期:<input name ="hiredate"value = "<%=emp.getHiredate() %>"/><br/>
    	薪水:<input name ="sal"value = "<%=emp.getSal()%>"/><br/>
    	津贴:<input name ="comm"value = "<%=emp.getComm() %>"/><br/>
    	部门编号:<input name ="deptno"value = "<%=emp.getDeptno() %>"/><br/>
    	<input type="submit" value="提交"/>   
    	<input type="reset" value="重置"/>
    </form>
     
  </body>
</html>

  doupdate.jsp

<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@page import="com.user.service.EmpService"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.user.entity.Emp"%>
<%@ 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 'doupdate.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>
    <% 
        request.setCharacterEncoding("UTF-8");
    	String empnoStr = request.getParameter("empno");
    	String ename = request.getParameter("ename");
    	String job = request.getParameter("job");
    	String mgrStr = request.getParameter("mgr");
    	String hiredateStr = request.getParameter("hiredate");
    	String salStr = request.getParameter("sal");
    	String commStr = request.getParameter("comm");
    	String deptnoStr = request.getParameter("deptno");
    	
    	int empno =Integer.parseInt(empnoStr);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Date hiredate =sdf.parse(hiredateStr);
    	int mgr = Integer.parseInt(mgrStr);
    	double sal = Double.parseDouble(salStr);
    	double comm = Double.parseDouble(commStr);
    	int deptno = Integer.parseInt(deptnoStr);
    	
    	Emp emp = new Emp(empno,ename, job, mgr, hiredate, sal, comm, deptno);
    	EmpService service = new EmpServiceImpl();
    	boolean isUpdate = service.updateEmp(emp);
    	request.getRequestDispatcher("dolist.jsp").forward(request, response);
//     	if(){
//     	}else{
//		}
    %>
  </body>
</html>

  dolist.jsp

<%@page import="com.user.entity.Emp"%>
<%@page import="com.user.service.EmpService"%>
<%@page import="com.user.service.impl.EmpServiceImpl"%>
<%@ 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 'dolist.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>
	<% 
    //设置前台页面参数  编码格式
   	request.setCharacterEncoding("UTF-8");
   	
	EmpService service =new EmpServiceImpl();
	List<Emp>list=service.getEmpList();
	//转发到list。jsp
	request.setAttribute("list", list);
	request.getRequestDispatcher("list.jsp").forward(request, response);
	
	%>
  </body>
</html>

  list.jsp

<%@page import="com.user.entity.Emp"%>
<%@ 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 'list.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="jsp/addEmp.jsp" 新增雇员>新增雇员</a>
    <table bordercolor="red" boder="1px">
    	<thead>
    		<tr>
    			<td>雇员编号</td>
    			<td>雇员姓名</td>
    			<td>工作</td>
    			<td>经理编号</td>
    			<td>入职日期</td>
    			<td>薪水</td>
    			<td>津贴</td>
    			<td>部门编号</td>
    			<td>操作</td>
    		</tr>
    	</thead>
    	<tbody>
    		<% 
    		request.setCharacterEncoding("UTF-8");
    		
    		List<Emp>list=(List<Emp>)request.getAttribute("list");
    		for(int i = 0; i<list.size();i++){
    		Emp emp = list.get(i);
    		
    		%>
    		<tr>
    			<td><%=emp.getEmpno()%></td>
    			<td><%=emp.getEname()%></td>
    			<td><%=emp.getJob()%></td>
    			<td><%=emp.getMgr()%></td>
    			<td><%=emp.getHiredate()%></td>
    			<td><%=emp.getSal()%></td>
    			<td><%=emp.getComm()%></td>
    			<td><%=emp.getDeptno()%></td>
    			<td>
    			<a href="jsp/dopreupdate.jsp?empno=<%=emp.getEmpno()%>">修改</a>
    			<a href="jsp/dodel.jsp?empno=<%=emp.getEmpno()%>">删除</a>
    			</td>
    		</tr>
    		
    	</tbody>
    	
    	  	<%}%>
    </table>
     <br>
  </body>
</html>  

 登录

Login.jsp dologin.jsp

  成功                                                                dolist.jsp list.jsp

  失败   Login.jsp

查 dolist.jsp list.jsp

增 addEmp.jsp doadd.jsp                                             dolist.jsp list.jsp

删 dodel.jsp                                                                  dolist.jsp list.jsp

改 dopreupdate.jsp preupdate.jsp doupdate.jsp          dolist.jsp list.jsp

 

posted on 2017-11-08 17:19  PoeticalJustice  阅读(398)  评论(0编辑  收藏  举报