创建了table_student
增删查改,欢迎页面
只要这个操作连接了数据库,就表示一个独立功能
增加一个学生信息
删除一个学生信息
修改一个学生信息
查看一个学生详情
1、在Java通过动态页面跳转到一个普通的html需要以/开始并加上项目名在/html文件
//浏览器请求的路径为http://localhost:8080/ManageServletWebApp/add.html,可以的
//http://localhost:8080代表本地地址的ip和端口号,后面是我们webapp的根路径/add.html是我们的资源
<a href="/ManageServletWebApp/add.html" >新增学生</a>
//还可以写成下面这样吗?不可以
<a href="/add.html" >新增学生</a>
//这里浏览器请求的URL就是:http://localhost:8080/add.html
//这样也是不行的,我们所有的页面都在我们的webapp的根下,就是http://localhost:8080/这个的下面,这样写你是找不到我们的项目名的,即ManageServletWebApp,你找不到这个,你也没有指定任何其他项目名,肯定是错的
//那么写成这样可以吗?不可以
<a href="/web/add.html" >新增学生</a>
//上面访问的是这个地址,http://localhost:8080/web/add.html,报错找不到,依然是没用项目名
记住!!!!只要是web下面的东西,它都是/ManageServletWebApp的,你可以把web看成/ManageServletWebApp,这就是我们项目的根目录
//不已斜杠开始,表示当前所在资源的相对路径下的add.html
<a href="add.html" >新增学生</a>
//浏览器请求的路径就是/ManageServletWebApp/oa/add.html
//因为你根本就没oa这个东西
请求的资源[http://localhost:8080//ManageServletWebApp/oa/add.html],报错不可用
2、在普通的html文件中跳转,是/项目名/xml中的路径
<!-- html中的路径是以/开始的,/oa/list是xml里面的路径-->
<a href="/ManageServletWebApp/oa/list">查看学生信息列表</a>
3、在静态html文件直接相互跳转是直接写html的相对路径就ok(这里是相对路径,浏览器从/ManageServletWebApp/下开始)
因为资源我们是放在web下的,因此相对路径就是/ManageServletWebApp
<a href="delete.html"></a>
但是其实这个跳转,它在浏览器中的路径是这样的
http://localhost:8080/ManageServletWebApp/delete.html
1、某处用到项目名时,不要写死,使用方法String contextPath = req.getContextPath();,来动态获取当前项目名。
2、上面总结过的跳转路径问题。
3、可以直接在href后面加请求体,比如**?studentid="+id**,这样我们就可以使用doget方法来处理你这个页面表格中想要发给我的数据了,post的话就不太方便了,post更适合用于表单的提交。
<a href="contextPath+/oa/updateEdit?studentid="+id >修改</a>
4、写这个网页之前,可以考虑先整理一下工具类,比如常用的数据库连接,关闭流等等,都可以以静态的方式,放在里面,方便我们在写其他Java程序的时候调用,减少代码冗余。
5、我们可以在写项目之前,以静态的页面,先构建好前端的样子,包括什么位置存放什么信息等等,这些静态的HTML页面,后面就是我们的模板,我们可以根据它的样子,改为动态的版本。一般来说,我们的网站中不会存在调用静态页面的情况,即URL中一般不会出现以html结尾的资源路径。
6、在写一个小项目的时候,可以先把框架搭建起来,然后在从头到尾的,一步一步的去实现这些功能,先不要将怎么简化,最开始要做的是先实现这些功能,后面你可以根据情况,在去简化Java代码。
7、单纯的用servlet写,有很多地方很不方便,比如把静态页面改为动态的时候,要用write方式输出,代码就很长,而且改起来很麻烦。
全部代码:
package com.sj.www;
import java.sql.*;
import java.util.ResourceBundle;
/**
* 数据库工具类
*/
public class DBUtil {
private static ResourceBundle bundle=ResourceBundle.getBundle("resources.jdbc");
private static String driver=bundle.getString("driver");
private static String url=bundle.getString("url");
private static String user=bundle.getString("user");
private static String password=bundle.getString("password");
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
* @return 返回连接对象
*/
public static Connection getconnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 关闭数据库相关资源
* @param connection 数据库连接对象
* @param statement 数据库操作对象
* @param resultSet 结果集对象
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生信息管理纯servlet版本title>
head>
<body>
<h1 style="text-align: center">欢迎使用!
<a href="/ManageServletWebApp/oa/list" >管理学生信息a>
h1>
body>
html>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>listservlet-name>
<servlet-class>com.sj.www.listActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>listservlet-name>
<url-pattern>/oa/listurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>addservlet-name>
<servlet-class>com.sj.www.addActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>addservlet-name>
<url-pattern>/oa/addurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>deleteservlet-name>
<servlet-class>com.sj.www.deleteActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>deleteservlet-name>
<url-pattern>/oa/deleteurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>updateEditservlet-name>
<servlet-class>com.sj.www.updateEditActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>updateEditservlet-name>
<url-pattern>/oa/updateEditurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>updateservlet-name>
<servlet-class>com.sj.www.updateActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>updateservlet-name>
<url-pattern>/oa/updateurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>detailsservlet-name>
<servlet-class>com.sj.www.detailsActionservlet-class>
servlet>
<servlet-mapping>
<servlet-name>detailsservlet-name>
<url-pattern>/oa/detailsurl-pattern>
servlet-mapping>
web-app>
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class listAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//动态获取项目路径/ManageServletWebApp
String contextPath = req.getContextPath();
//获取连接数据库对象
connection = DBUtil.getconnection();
//查询所有学生信息
String sql = "select id as '学生学号',name as '学生姓名',sex as '学生性别',classno as '学生班级'" +
"from table_student";
try {
preparedStatement = connection.prepareStatement(sql);
//获取数据集
resultSet = preparedStatement.executeQuery();
//遍历数据集
int id = 0;
String name = null;
String sex = null;
int classno = 0;
String htmlhead = "\n" +
"\n" +
"\n" +
" \n" +
" Title \n" +
"\n" +
" "+
"\n" +
"学生信息列表
\n" +
"
\n" +
"\n" +
" \n" +
" 学生学号 \n" +
" 学生姓名 \n" +
" 学生性别 \n" +
" 学生班级 \n" +
" 操作 \n" +
" ";
String htmlflod = "
\n" +
"\n" +
"\n" +
"";
String htmltext = "";
while (resultSet.next()) {
id = resultSet.getInt("学生学号");
name = resultSet.getString("学生姓名");
sex = resultSet.getString("学生性别");
classno = resultSet.getInt("学生班级");
htmltext += " \n" +
" " + id + " \n" +
" " + name + " \n" +
" " + sex + " \n" +
" " + classno + " \n" +
" \n" +
" +contextPath+"/oa/updateEdit?studentid="+id+"\" >修改\n" +
" +id+
")\">删除\n" +
" +contextPath+"/oa/details?studentid="+id+"&studentname="+name+"&studentsex="+sex+"&classno="+classno+"\">详情\n" +
" \n" +
" ";
}
writer.println(htmlhead + htmltext + htmlflod);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(connection, preparedStatement, resultSet);
}
}
}
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 增加学生信息
*/
public class addAction extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
Connection connection = null;
PreparedStatement preparedStatement=null;
//获取连接数据库对象
connection = DBUtil.getconnection();
//新增sql语句
String sql="insert into table_student(name,sex,classno) values(?,?,?)";
//新增一条数据
try {
/**
* 通过前端post请求获取数据
*/
String name = req.getParameter("studentname");
String sex = req.getParameter("studentsex");
String classno = req.getParameter("studentclass");
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,sex);
preparedStatement.setString(3,classno);
int rows = preparedStatement.executeUpdate();
if (rows>0){
writer.println("加入学生信息成功!
");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class deleteAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet=null;
//获取连接数据库对象
connection = DBUtil.getconnection();
//新增sql语句
String sql = "delete from table_student where id=?";
//新增一条数据
try {
/**
* 通过前端post请求获取数据
*/
int id = Integer.parseInt(req.getParameter("studentid"));
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
int rows = preparedStatement.executeUpdate();
if (rows > 0) {
writer.println("删除学生信息成功!
");
}else{
writer.println("删除学生信息失败!
");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
/*关闭数据库相关连接*/
DBUtil.close(connection,preparedStatement,resultSet);
}
}
}
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class updateEditAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//写动态html界面
writer.println("\n" +
"\n" +
"\n" +
" \n" +
" update \n" +
"\n" +
"\n" +
"\n" +
" 修改学生信息
\n" +
"
\n" +
" ");
//获取前端发来的数据
int id = Integer.parseInt(req.getParameter("studentid"));
try {
//获取连接数据库对象
connection = DBUtil.getconnection();
//然后进行查询相关信息
String s1 = "select name,sex,classno from table_student where id=?";
preparedStatement = connection.prepareStatement(s1);
preparedStatement.setInt(1,id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String name = resultSet.getString("name");
String sex = resultSet.getString("sex");
int classno = resultSet.getInt("classno");
writer.println(" +
" \n" +
" \n" +
" 学生学号:+id+" readonly>
\n" +
" 学生姓名:+name+">
\n" +
" 学生性别:+sex+">
\n" +
" 学生班级:+classno+">
\n" +
"
\n" +
" ");
}
writer.println("\n" +
"\n" +
"");
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.close(connection, preparedStatement,resultSet);
}
}
}
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class updateAction extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 该方法用于接受post请求,修改数据库
Connection connection = DBUtil.getconnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//解决乱码
resp.setContentType("text/html");
//输出到屏幕上的工具
PrintWriter writer = resp.getWriter();
//获取post请求发送的数据
int studentid = Integer.parseInt(req.getParameter("studentid"));
String name = req.getParameter("studentname");
String sex = req.getParameter("studentsex");
int classno = Integer.parseInt(req.getParameter("studentclass"));
try {
String sql = "update table_student set name =? , sex=? , classno=? where id=?";
preparedStatement = connection.prepareStatement(sql);
//编译后,赋值,执行sql语句
preparedStatement.setString(1, name);
preparedStatement.setString(2, sex);
preparedStatement.setInt(3, classno);
preparedStatement.setInt(4, studentid);
int rows = preparedStatement.executeUpdate();
if (rows > 0) {
writer.write("修改成功!");
} else {
writer.write("修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(connection, preparedStatement, resultSet);
}
}
}
package com.sj.www;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 学生信息详情页
*/
public class detailsAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//读出get请求的请求体数据即可。
int studentid = Integer.parseInt(req.getParameter("studentid"));
String studentname = req.getParameter("studentname");
String studentsex = req.getParameter("studentsex");
int classno = Integer.parseInt(req.getParameter("classno"));
//直接根据所获得的数据以页面的形式输出即可
PrintWriter writer = resp.getWriter();
writer.println("\n" +
"\n" +
"\n" +
" \n" +
" detail \n" +
"\n" +
"\n" +
"\n" +
" 学生信息详情
\n" +
"
\n" +
" 学生学号: "+studentid+"
\n" +
" 学生姓名: "+studentname+"
\n" +
" 学生性别: "+studentsex+"
\n" +
" 学生班级: "+classno+"
\n"+
"\n" +
"\n" +
"");
}
}
虽然简单,但是还是花费了不少心思的