创建数据库和表

首先,创建一个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

JAVAWEB 一一 userweb1(原生,非servlet版)的更多相关文章

  1. Eclipse下创建Spring MVC web程序--非maven版

    首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...

  2. js 控制Div循环显示 非插件版

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 购物车非cookie版

    2015.11.26购物车,非cookie版 [点击来,你发现被骗了(笑哭,笑哭,笑哭,源代码的话,留下邮箱吧,是在不好找这一时半会儿的.)] Jsp通过反射机制获取bean中的标签,但其实,可以没有 ...

  4. 浅谈JavaWEB入门必备知识之Servlet入门案例详解

    工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...

  5. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  6. [C++]竞赛模板·数据统计与IO(重定向版与非重定向版)

      /* 数据统计与IO 重定向版模板 描述:本机测试用文件数据流重定向,一旦提交到比赛就自动“删除”重定向语句 */ # define LOCAL #include<stdio.h> # ...

  7. mysql 非安装版的配置

    一直以来都是使用wamp中集成的mysql数据库,今天突然想试试下载一个mysql的zip包进行配置. 一.下载mysql非安装版 下载地址可以到:http://dev.mysql.com/downl ...

  8. UOJ34 多项式乘法(非递归版)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 为什么原生的servlet是线程不安全的而Struts2是线程安全的?

    因为原生的servlet在整个application生命周期中,只在初次访问的时候实例化一次,以后都不会再实例化,只会调用Server方法进行响应,所以如果在servlet类中定义成员变量,那么就会让 ...

随机推荐

  1. Android Studio将项目打包成apk

    Android Studio将项目打包成apk 第一种方法:适合自己调试用. (1)直接在项目中生成: (2)位置是在你的项目中 第二种方法:适合发布应用. (1)找到Generate Signed ...

  2. mysql给查询的结果添加序号

    1.法一: select  (@i:=@i+1)  i,a.url from  base_api_resources a  ,(select   @i:=0)  t2 order by a.id de ...

  3. SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  4. 【MySql】【Navicat】下载,安装,激活攻略

    来了一家新公司,新电脑,最近申请了DB访问的权限. 公司用的MySql数据库,自己下载了MySql workbench,用的也还不错. 现在下载了一个Navicat,比较讨厌的是,现在很多软件都需要注 ...

  5. tornado 和 djanjo 转义处理对比

    tornado tornado默认是转义所有字符,比较安全,但有时候我们的确需要把字符当做html来解析处理,因此我们需要做些处理. 所有的模板输出都已经通过 tornado.escape.xhtml ...

  6. 20165205 2017-2018-2《Java程序设计》结对编程一 第二周总结

    20165205 2017-2018-2<Java程序设计>结对编程一 第二周总结 设计思路 编写主类Arithmetic4 编写ArithmeticFunc类来实现计算,其中包括:加.减 ...

  7. 20165205 2017-2018-2《Java程序设计》结对编程一 第一周总结

    20165205 2017-2018-2<Java程序设计>结对编程一 第一周总结 需求分析 对输入的算式进行计算,要求满足一下条件: 支持整数运算,如2+5,47+7865. 支持多运算 ...

  8. 设置nginx中文件上传的大小限制度

    通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题: 用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_s ...

  9. activiti explorer5.22.0源代码解读

    请求通过ExplorerApplicationServlet(AbstractApplicationServlet.service()方法)进入web系统中. Activiti Explorer的应用 ...

  10. Eureka 注册中心 简单搭建

    直接上代码:配置文件截图 server.port= spring.application.name=eureka-server eureka.instance.hostname=127.0.0.1 # ...