📄 reply.java
字号:
package guestbook;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.sql.DataSource;
import java.sql.*;
public class reply extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
private DataSource dataSource=null;
private int id;
//Initialize global variables
public void init() throws ServletException {
Init init=new Init(getServletContext().getRealPath("/"));
dataSource=LinkDB.getDB();
if (dataSource==null) {
LinkDB.setDB(init.getDriverName(),init.getDBURL(),init.getDBUser(),init.getDBPassword());
dataSource=LinkDB.getDB();
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
id = Integer.parseInt(request.getParameter("id"));
}
catch(NumberFormatException e){
throw new RuntimeException("错误的ID号");
}
response.sendRedirect("reply.jsp");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GB2312");
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
String content=request.getParameter("replyContent");
if(content.getBytes().length>1000){
out.println("<script>alert('回复内容太长,请不要超过500字');document.location='javascript:history.go(-1);'</script>");
return;
}
content=Check.toHTML(content);
content=Check.insteadCode(content,"'","''");
String replytime=Head.getTime();
Connection conn=null;
Statement stmt=null;
try{
conn=dataSource.getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int num=stmt.executeUpdate("update guestbook set reply='"+content+"',replytime='"+replytime+"' where id='"+id+"'");
if(num==0){
out.println("<script>alert('回复失败,可能ID不存在');document.location='index.jsp';</script>");
}
else{
out.println("<script>alert('回复成功');document.location='index.jsp';</script>");
}
}
catch(SQLException e){
out.println(e);
}
finally {
try{
stmt.close();
conn.close();
LinkDB.shutdownDataSource(dataSource);
}
catch(SQLException e) {
out.println(e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -