开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 3

notfand/LearnJava

forked from yswift/LearnJava
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
LearnJava
/
JavaWeb
/
src
/
example
/
StudentServlet.java
LearnJava
/
JavaWeb
/
src
/
example
/
StudentServlet.java
StudentServlet.java 6.58 KB
一键复制 编辑 原始数据 按行查看 历史
yswift 提交于 2019年04月24日 22:06 +08:00 . fix bug
package example;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.sql.rowset.serial.SerialBlob;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@WebServlet(name = "StudentServlet", urlPatterns = {"/student/create", "/student/edit", "/student/photo", "/student/delete"})
// 允许上传文件,最大1M,
@MultipartConfig(
fileSizeThreshold = 1024 * 1024, // 小于1m的文件保存在内存中
maxRequestSize = 1024 * 1024
)
public class StudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getServletPath();
switch (path) {
case "/student/create":
create(request, response);
break;
case "/student/edit":
edit(request, response);
break;
default:
request.setAttribute("msg", "不支持的url: " + path);
request.getRequestDispatcher("/student/list.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getServletPath();
switch (path) {
case "/student/delete":
delete(request, response);
break;
case "/student/photo":
getPhoto(request, response);
break;
default:
request.setAttribute("msg", "不支持的url: " + path);
request.getRequestDispatcher("/student/list.jsp").forward(request, response);
}
}
// 新建学生
private void create(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
Student s = new Student();
updateStudent(s, request);
StudentDAO dao = new StudentDAO();
dao.save(s);
// 设置用于显示给用户的成功消息
String msg = "已成功添加学生:" + s.getName();
// 传递给 jsp
request.setAttribute("msg", msg);
request.getRequestDispatcher("list.jsp")
.forward(request, response);
} catch (SQLException e) {
throw new IOException("save failed", e);
}
}
// 使用客户端提交的信息更新student对象
private void updateStudent(Student s, HttpServletRequest request) throws ServletException, SQLException, IOException {
s.setNo(request.getParameter("No"));
s.setName(request.getParameter("Name"));
String a = request.getParameter("Age");
if (a != null && a.length() > 0) {
s.setAge(Integer.parseInt(a));
}
String b = request.getParameter("Birthday");
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
if (b != null && b.length() > 0) {
try {
s.setBirthday(new java.sql.Date(sf.parse(b).getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
}
uploadPhoto(s, request);
}
// 处理照片上传
private void uploadPhoto(Student s, HttpServletRequest request) throws IOException, ServletException, SQLException {
Part filePart = request.getPart("Photo");
if (filePart == null || filePart.getSize() <= 0) {
return;
}
InputStream is = filePart.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
copy(is, os);
Blob photo = new SerialBlob(os.toByteArray());
s.setPhoto(photo);
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
String id = request.getParameter("Id");
StudentDAO dao = new StudentDAO();
Student s = dao.findById(id);
updateStudent(s, request);
dao.update(s);
// 设置用于显示给用户的成功消息
String msg = "已成功修改学生信息:" + s.getName();
// 传递给 jsp
request.setAttribute("msg", msg);
request.getRequestDispatcher("list.jsp")
.forward(request, response);
} catch (SQLException e) {
throw new IOException("save failed", e);
}
}
// 删除学生
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
try {
StudentDAO dao = new StudentDAO();
dao.delete(id);
// 传递给 jsp
request.setAttribute("msg", "删除学生成功");
request.getRequestDispatcher("list.jsp")
.forward(request, response);
} catch (SQLException e) {
throw new IOException("cannot delete student by id: " + id, e);
}
}
// 输出学生照片
private void getPhoto(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
StudentDAO dao = new StudentDAO();
try {
Student s = dao.findById(id);
if (s == null || s.getPhoto() == null) {
response.sendError(404, "cannot found student by id:" + id);
}
// 设置结果MIME
response.setContentType("image/jpg");
// 照片直接写入输出流
OutputStream os = response.getOutputStream();
Blob photo = s.getPhoto();
InputStream is = photo.getBinaryStream();
copy(is, os);
os.close();
} catch (SQLException e) {
throw new IOException("cannot found student by id: " + id, e);
}
}
// 把数据从输入流is,复制到输出流os中
public static void copy(InputStream is, OutputStream os) throws IOException {
int rlen;
byte[] buffer = new byte[1024];
while ((rlen = is.read(buffer)) != -1) {
os.write(buffer, 0, rlen);
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Java 相关课程源码
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/notfand/LearnJava.git
git@gitee.com:notfand/LearnJava.git
notfand
LearnJava
LearnJava
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /