Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from yswift/LearnJava
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
LearnJava
/
JavaWeb
/
src
/
example
/
StudentServlet.java
LearnJava
/
JavaWeb
/
src
/
example
/
StudentServlet.java
StudentServlet.java 6.58 KB
Copy Edit Raw Blame History
yswift authored 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...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

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

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

取消
提交

About

Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/li-jiahongjia/LearnJava.git
git@gitee.com:li-jiahongjia/LearnJava.git
li-jiahongjia
LearnJava
LearnJava
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

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