此工程的模式是javaBean+servlet+jsp
1.po层
package com.inspur.po;
import java.sql.Date;
public class EmpDept {
private int empno;
private String ename;
private String mgr;
private String job;
private Date hiredate;
private int sal;
private int comm;
private String dname;
private String loc;
private int deptno;
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
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 Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public EmpDept(int empno, String ename, String mgr, String job, Date hiredate, int sal, int comm, String dname, String loc, int deptno) {
super();
this.empno = empno;
this.ename = ename;
this.mgr = mgr;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.dname = dname;
this.loc = loc;
this.deptno = deptno;
}
public EmpDept() {
super();
}
}
2.dao层
package com.inspur.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.inspur.po.EmpDept;
import com.inspur.tool.DBConn;
public class SelectEmp {
public List Select(int deptno, String name){
Connection conn=DBConn.getConn();
PreparedStatement pst=null;
ResultSet rst=null;
EmpDept emp=null;
List list=new ArrayList();
String sql="select s1.empno,s1.ename,s2.ename,s1.job,s1.hiredate,s1.sal,s1.comm,dname,loc,s1.deptno " +
"from emp s1,emp s2,dept " +
" where dept.deptno=s1.deptno and s2.empno=s1.mgr " +
" and s1.deptno=? and s1.ename like ?";//联表查询,s1和s2是自查询
try {
pst=conn.prepareStatement(sql);
pst.setInt(1,deptno);
pst.setString(2,"%"+name+"%");//模糊查询
rst=pst.executeQuery();
while(rst.next()){
emp=new EmpDept(rst.getInt("empno"),rst.getString(2),rst.getString(3),rst.getString("job"),rst.getDate("hiredate"),rst.getInt("sal"),rst.getInt("comm"),rst.getString("dname"),rst.getString("loc"),rst.getInt("deptno"));
list.add(emp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
3.servlet层
package com.inspur.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.inspur.dao.SelectEmp;
public class EmpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deptno=request.getParameter("deptno");
String name=request.getParameter("name");
HttpSession session=request.getSession();
int dept_no=Integer.parseInt(deptno);//将从表里取出的部门号转换为整形
SelectEmp emp=new SelectEmp();
List list=new ArrayList();
list=emp.Select(dept_no,name);
if(list.isEmpty()){
session.setAttribute("error_message","没有此部门");
request.getRequestDispatcher("selectemp.jsp").forward(request,response);
}else{
session.setAttribute("deptno",list);
request.getRequestDispatcher("selectemp.jsp").forward(request,response);//将从数据库中查出的信息返回到页面
}
}
}
4.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.*,com.inspur.po.EmpDept" %>
<html>
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="selectemp" name="" method="post">
<table border="1" align="center">
<tr align="center">
<td width="150">
<select name="deptno">
<option value="0">---请选择部门号---</option>
<%-- <option value="ename">姓名</option>--%>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</td>
<td width="83">请输入姓名:</td>
<td width="168"><input type="text" name="name"/></td>
<td width="79"><input type="submit" value="查询"></td>
</tr>
<tr align="center">
<td>员工号</td>
<td>员工姓名</td>
<td>经理姓名</td>
<td>工作类型</td>
<td width="83">工作时间</td>
<td width="55">工资</td>
<td width="56">奖金</td>
<td width="64">部门名称</td>
<td width="77">公司地址</td>
<td width="70">部门号</td>
</tr>
<%List list=(List)session.getAttribute("deptno");
if(list==null||list.equals("")){%>
<%--<%=session.getAttribute("error_message") %>这个不管用不知道为什么取不到值--%>
<%}else{
for(Iterator it=list.iterator();it.hasNext();){
EmpDept emp=(EmpDept)it.next();%>
<tr align="center">
<td><%=emp.getEmpno() %></td>
<td><%=emp.getEname() %></td>
<td><%=emp.getMgr() %></td>
<td><%=emp.getJob() %></td>
<td><%=emp.getHiredate() %></td>
<td><%=emp.getSal() %></td>
<td><%=emp.getComm() %></td>
<td><%=emp.getDname()%></td>
<td><%=emp.getLoc() %></td>
<td><%=emp.getDeptno() %></td>
</tr>
<%} %>
<%} %>
</table>
</form>
</body>
</html>
这里web.xml就不往出贴了,呵呵
分享到:
相关推荐
JSP页面显示list的常用方法
json2 ajax返回list处理 在jsp页面 遍历
把在页面上的分页操作,在java类用一个对象封装,并且把整个分页的html标签封装在这个对象的String类型的属性里,在需要分页的地方,直接从后台取得这个对象,并把对象中的字符串用页面的内置out对象写到页面上,...
jsp+jquery项目中正常使用vue组件并调用接口封装的request拦截器
怎么解决JSP页面中文问题。。。。。。。。。。。。。。。。
下面小编就为大家带来一篇js中获取jsp表单中radio类型的值简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
JSP网页模板JSP网页模板JSP网页模板JSP网页模板JSP网页模板JSP网页模板JSP网页模板JSP网页模板JSP网页模板
从后台取得的数据list在jsp页面控制table每行显示四列
在strus2中list如何输出到jsp中,文档中有详细的说明,就是在jsp中循环出java中的list集合,利用strus2的标签,源代码来自网络,本人补充并测试过很好用,以此分享,希望能帮助更多的初学者。
使用servlet,jdbc将mysql中数据显示在jsp页面中
在一个jsp页面中,定义了一个用户登录界面(以form表单定义),但要达到检验输入的信息的合法性,就需要对输入的用户信息作判断,那么自然就需要取出其中的值。有的页面可以用action传参来得到form中输入的信息值,...
jsp页面路径问题,request.getcontentType.;request.getscheme;request.getServerName
jsp自定义标签Tag封装接口和实现方法
任何jsp页面只要使用包含附件中的loading.jsp将自动拥有“页面正在加载的。。”提示信息,在页面加载完后将自动隐藏。 我为什么使用这个组件? 1。交互性。 客户清楚知道,你的页面是在加载,需要等待,而不用...
在jsp页面登陆时,有验证码功能会更加安全,这些资料能教你如何在jsp页面中实现验证码功能,
jsp数据库连接大全和数据库操作封装到Javabean
通过JSP将数据库中的数据显示到网页上,数据库为Mysql数据库。
在jsp页面中的日期选择框
jsp分页类 jsp分页类 jsp分页类
javajsp中 中文问题详解,