<!-- jsp页面 -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.unit5.service.EmpService"%>
<%@ page import="com.unit5.utils.Condition"%>
<%@ page import="com.unit5.pojo.Emp"%>
<%@ page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
EmpService es=new EmpService();
//获取HTML条件参数
String idCondition=request.getParameter("id");
String nameCondition=request.getParameter("name");
String deptNameCondition=request.getParameter("deptName");
//构造条件对象
Condition cd=new Condition(idCondition,nameCondition,deptNameCondition);
//获取条件查询结构
ArrayList<Emp> empsList=es.findEmps(cd);
//将获取数据保存在作用域中
request.setAttribute("empList", empsList);
%>
<!-- 头部 -->
<div style="background-color:cornflowerblue; color:white; width:100%; height:150px;">
<h1 style="line-height:80px; margin-left:30px;">人力资源管理系统</h1>
</div>
<!-- 菜单 -->
<table style="border:1px solid cornflowerblue; width:100%;">
<tr>
<td width="10%"><a href="http://127.0.0.1:8080/unit5/emp_index.jsp">首页</a></td>
<td width="10%"><a href="http://127.0.0.1:8080/unit5">添加</a></td>
<td width="10%"><a href="http://127.0.0.1:8080/unit5">其他</a></td>
<td></td>
<td></td>
<td width="50%"></td>
</tr>
</table>
<!-- 条件查询 -->
<br/><form action="http://127.0.0.1:8080/unit5/emp_index。jsp" method="get" style="width:100%">
查询条件:员工id<input type="number" style="width:100px;" name="id">
姓名<input type="text" style="width:100px;" name="name">
部门<select style="width:160px; height:30px;" name="deptName">
<option value="所有">所有</option>
<option value="研发部">研发部</option>
<option value="行政部">行政部</option>
<option value="销售部">销售部</option>
<option value="董事会">董事会</option>
<option value="财务部">财务部</option>
</select>
<input type="submit" value="查询数据">
<input type="reset" value="清空">
</form><br/>
<!-- 数据查询 -->
<table border="1px" style="width:100%">
<tr>
<td>员工id</td>
<td>姓名</td>
<td>年龄</td>
<td>职位</td>
<td>入职日期</td>
<td>薪资</td>
<td>部门</td>
<td>操作</td>
</tr>
<c:forEach items="${requestScope.empList}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.age}</td>
<td>${emp.job}</td>
<td>${emp.hireDate}</td>
<td>${emp.salary}</td>
<td>${emp.deptName}</td>
<td><a href="连接到删除功能jsp">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
//实体类构造方法
package com.unit5.pojo;
import java.sql.Date;
public class Emp {
private int id;
private String name;
private int age;
private String job;
private Date hireDate; //日期要用Date类进行定义,在此处要用中Date
private double salary;
private String deptName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
//具体业务类
package com.unit5.service;
import java.sql.SQLException;
import java.util.ArrayList;
import com.unit5.DAO.EmpDAO;
import com.unit5.pojo.Emp;
import com.unit5.utils.Condition;
public class EmpService {
//条件查询数据
EmpDAO ed=new EmpDAO();
public ArrayList findEmps(Condition cd) throws SQLException {
ArrayList<Emp> empList=new ArrayList<Emp>();
String sql="select * from emps where 1=1"; //1=1默认查询所有
System.out.println("查询条件:"+cd.id+"-"+cd.name+"-"+cd.deptName);
if(cd.id!=null && cd.id!="") {
sql=sql+" and id="+cd.id; //字段拼接,注意空格;
}
if(cd.name!=null && cd.name!="") {
sql=sql+" and name='"+cd.name+"'"; //字符串要加单引号,java中字符串要加双引号 ,在此程式中单引号要被双引号阔上
}
if(cd.deptName!=null && cd.deptName!="") {
sql=sql+" and deptName='"+cd.deptName+"'";
}
System.out.println("条件查询sql:"+sql);
empList=ed.findEmpBySql(sql);
return empList;
}
}
//数据库连接
package com.unit5.utils;
import java.sql.Connection;
import java.sql.DriverManager;
public class GetConnection {
private static String address="jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimeZone=UTC";
private static String dataBaseName="";
private static String dataBasePwd="";
public static Connection getConn(){
Connection conn=null;
try{
//加载jdbc驱动,固定程式
Class.forName("com.mysql.cj.jdbc.Driver");
//链接数据库,固定程式
conn=DriverManager.getConnection(address,dataBaseName,dataBasePwd);
}catch(Exception e){
e.printStackTrace();
System.out.println("数据库链接失败!");
}
return conn;
}
}
//条件查询数据实体类构造方法
package com.unit5.utils;
public class Condition {
public String id;
public String name;
public String deptName;
public Condition(String id, String name, String deptName) {
this.id = id;
this.name = name;
this.deptName = deptName;
}
}
//增、删、改、查的公共方法
package com.unit5.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.unit5.pojo.Emp;
import com.unit5.utils.GetConnection;
public class EmpDAO {
//查询数据公共方法
public ArrayList findEmpBySql(String sql) throws SQLException {
ArrayList<Emp> empList=new ArrayList<Emp>(); //特指Emp类里的参数
Connection conn=GetConnection.getConn();
Statement stmt=conn.createStatement(); //jdbc数据库连接固定程式
ResultSet rs=stmt.executeQuery(sql); //将外部传进的sql传进执行,有返回值的用executeQuery,直接执行的用execute;
while(rs.next()) { //循环保存sql的数据,每循环一次累加保存到empList里,直到保存完毕为止;
Emp emp=new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setAge(rs.getInt("age"));
emp.setJob(rs.getString("job"));
emp.setHireDate(rs.getDate("hireDate"));
emp.setSalary(rs.getDouble("salary"));
emp.setDeptName(rs.getString("deptName"));
empList.add(emp);
}
conn.close(); //关闭连接
stmt.close(); //关闭连接
return empList;
}
}
执行后就变成这样,求大神解答?
404错误 表示 没找到你的页面,检查一下 你访问的地址 是否正确
比如你的访问地址 可能是 localhost:8080/unit5/emp_index.jsp
建议如下:如有帮助,请采纳一下,谢谢
jsp去那个web.xml配置一下路径,然后tomcat也去配置一下
可以尝试右键emp_index.jsp代码点击run运行 eclipse内运行 查看 上方路径复制到浏览器
问题可能是的项目上下文路径或者web.xml配置错误或者访问路径错误
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server.服务器版本: Apache Tomcat/8.5.65
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 服务器构建: Mar 30 2021 12:28:40 UTC
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 服务器版本号: 8.5.65.0
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 操作系统名称: Windows 10
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS.版本: 10.0
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 架构: amd64
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java 环境变量: C:\Program Files\Java\jdk-12.0.1
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java虚拟机版本: 12.0.1+12
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM.供应商: Oracle Corporation
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE: D:\Java\ecplice\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME: D:\Java\Tomcat\apache-tomcat-8.5.65
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 命令行参数: -Dcatalina.base=D:\Java\ecplice\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 命令行参数: -Dcatalina.home=D:\Java\Tomcat\apache-tomcat-8.5.65
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 命令行参数: -Dwtp.deploy=D:\Java\ecplice\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: 命令行参数: -Dfile.encoding=GBK
6月 18, 2021 2:15:54 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: 在java.library.path:[C:\Program Files\Java\jdk-12.0.1\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk-12.0.1/bin/server;C:/Program Files/Java/jdk-12.0.1/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files (x86)\Autodesk\Backburner\;C:\Program Files\Java\jdk-12.0.1\bin;;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Users\pan\AppData\Local\Microsoft\WindowsApps;;D:\Java\jdk和eclipse安装文件\eclipse;;.]上找不到基于APR的Apache Tomcat本机库,该库允许在生产环境中获得最佳性能
6月 18, 2021 2:15:54 下午 org.apache.coyote.AbstractProtocol init
信息: 初始化协议处理器 ["http-nio-8080"]
6月 18, 2021 2:15:54 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
6月 18, 2021 2:15:54 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 591 ms
6月 18, 2021 2:15:54 下午 org.apache.catalina.core.StandardService startInternal
信息: 正在启动服务[Catalina]
6月 18, 2021 2:15:54 下午 org.apache.catalina.core.StandardEngine startInternal
信息: 正在启动 Servlet 引擎:[Apache Tomcat/8.5.65]
6月 18, 2021 2:15:55 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。
6月 18, 2021 2:15:55 下午 org.apache.coyote.AbstractProtocol start
信息: 开始协议处理句柄["http-nio-8080"]
6月 18, 2021 2:15:55 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 839 ms
查询条件:null-null-null
条件查询sql:select * from emps where 1=1
执行到这一步就没了???
右键运行后,就查到这个页面,但是点击查询后就查不到了?
你的代码里有一个中文的句号
<br/><form action="http://127.0.0.1:8080/unit5/emp_index。jsp" method="get" style="width:100%">