代码写好了,但是运行时报404错误,我配置也都没有问题
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>addCourseServlet</servlet-name>
<servlet-class>com.vincent.jdbc.servlet.addCourseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addCourseServlet</servlet-name>
<url-pattern>/addCourse</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>showCourseServlet</servlet-name>
<servlet-class>com.vincent.jdbc.servlet.showCourseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showCourseServlet</servlet-name>
<url-pattern>/showCourse</url-pattern>
</servlet-mapping>
</web-app>
package com.vincent.jdbc.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionUtil {
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException {
Connection conn=dataSource.getConnection();
return conn;
}
public static void release(Connection conn, Statement statement){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement=null;
}
}
public static void release(Connection conn, Statement statement, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement=null;
}
}
}
package com.vincent.jdbc.course;
import java.util.Date;
public class Course {
//成员属性
private int id;
private String name;
private String category;
private String desp;
private Date craeatTime;
//无参构造
public Course(){
}
//带参构造
public Course(int id, String name, String category, String desp, Date craeatTime) {
this.id = id;
this.name = name;
this.category = category;
this.desp = desp;
this.craeatTime = craeatTime;
}
//get/set方法
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 String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public Date getCraeatTime() {
return craeatTime;
}
public void setCraeatTime(Date craeatTime) {
this.craeatTime = craeatTime;
}
}
package com.vincent.jdbc.servlet;
import com.vincent.jdbc.util.ConnectionUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
public class addCourseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
String category=request.getParameter("category");
String desp=request.getParameter("desp");
Connection conn=null;
PreparedStatement pstmt=null;
try{
//1.注册驱动
conn=ConnectionUtil.getConnection();
//2.编写SQL语句
String sql="insert into course values(null,?,?,?,curtime())";
//3.预编译SQL语句
pstmt=conn.prepareStatement(sql);
//4.设置参数
pstmt.setString(1,name);
pstmt.setString(2,category);
pstmt.setString(3,desp);
//5.执行SQL语句
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("保存成功!");
request.getRequestDispatcher("/showCourse").forward(request,response);
}
}catch (Exception e){
e.printStackTrace();
}finally {
ConnectionUtil.release(conn,pstmt);
}
}
}
package com.vincent.jdbc.servlet;
import com.vincent.jdbc.course.Course;
import com.vincent.jdbc.util.ConnectionUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class showCourseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建一个List集合用于存放课程
List<Course> courses = new ArrayList<>();
Connection conn =null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//1.获得连接
conn=ConnectionUtil.getConnection();
//2.编写SQL语句
String sql="select * from course";
//3.预编译sql语句
pstmt=conn.prepareStatement(sql);
//执行SQL
rs=pstmt.executeQuery();
while (rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getString("category")+" "+rs.getString("desp")+" "+rs.getDate("createTime"));
Course course = new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("name"));
course.setCategory(rs.getString("category"));
course.setDesp(rs.getString("desp"));
course.setCraeatTime(rs.getTime("createTime"));
courses.add(course);
System.out.println(courses);
}
request.setAttribute("list",courses);
request.getRequestDispatcher("WEB-INF/jsp/showCourse.jsp").forward(request,response);
}catch (Exception e){
e.printStackTrace();
}finally {
ConnectionUtil.release(conn,pstmt,rs);
}
}
}
<%--
Created by IntelliJ IDEA.
User: superme
Date: 2019/7/27
Time: 13:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>课程添加</title>
</head>
<body>
<form action="<%=basePath%>addCourse" method="get">
<center>
<div>课程添加</div>
课程名 <input type="text">
所属方向<input type="text">
课程描述<input type="text">
<input type="button" value="添加">
</center>
</form>
</body>
</html>
<%@ page import="com.vincent.jdbc.course.Course" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: superme
Date: 2019/7/27
Time: 14:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>显示课程</title>
</head>
<body>
<form action="<%=basePath%>showCourse" method="get">
<h3 align="center">添加成功</h3>
<table align="center">
<tr>
<td>课程名称</td>
<td>所属方向</td>
<td>课程描述</td>
<td>创建时间</td>
</tr>
<%
//获取集合
List<Course> courseList = (List<Course>) request.getAttribute("list");
for(Course c:courseList){
%>
<tr>
<td><%=c.getName()%></td>
<td><%=c.getCategory()%></td>
<td><%=c.getDesp()%></td>
<td><%=c.getCraeatTime()%></td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
正在回答
同学你好,404错误是访问路径问题,问一下同学的访问路径是什么?使用的是IDEA还是eclipse,建议同学将项目结构展开截图贴一下。
祝:学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星