📄 writeservlet.java
字号:
package com.tiandinet.HiMessage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tiandinet.util.FileUpload;
import com.tiandinet.util.CommonField;
import com.tiandinet.util.FileField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.FilenameFilter;
public class WriteServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public WriteServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HiMessageFuncs hmf = new HiMessageFuncs();
Conf conf = hmf.getConfiguration();
String replyId = request.getParameter("replyId");
if (replyId != null && !replyId.equals("")) {
Msg replyMsg = hmf.getMessageForServletInvoke(replyId);
if (replyMsg != null) {
request.setAttribute("replyMsg", replyMsg);
}
}
request.setAttribute("headimaSelect", this.getHeadimas(request));
request.setAttribute("conf", conf);
RequestDispatcher rd= request.getRequestDispatcher("/write.jsp");
rd.forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean messageStatus = true;
try {
FileUpload fUpload = new FileUpload();
fUpload.setSavePath("uploadImages");
fUpload.parseRequest(this.getServletContext(), request);
Hashtable fields = fUpload.getFields();
CommonField name = (CommonField)fields.get(new String("name"));
CommonField headima = (CommonField)fields.get(new String("headima"));
CommonField qq = (CommonField)fields.get(new String("qq"));
CommonField msn = (CommonField)fields.get(new String("msn"));
CommonField email = (CommonField)fields.get(new String("email"));
CommonField content = (CommonField)fields.get(new String("content"));
//reId
CommonField reId = (CommonField)fields.get(new String("reId"));
String reMsgId = "NONE";
if (reId != null && !reId.fieldValue.equals("")) {
reMsgId = reId.fieldValue;
}
FileField file = (FileField)fields.get(new String("pic"));
String filename = "NONE";
if (file.status
&& file.clientFileName != null
&& file.objectFileName != null)
filename = file.objectFileName;
HiMessageFuncs hmf = new HiMessageFuncs();
messageStatus = hmf.addMessage(reMsgId,
request.getRemoteAddr(),
name.fieldValue,
headima.fieldValue,
qq.fieldValue,
email.fieldValue,
msn.fieldValue,
content.fieldValue,
filename);
}
catch (Exception e) {
messageStatus = false;
}
String info = "";
if (messageStatus)
info = "alert('成功发表留言!');window.location.href='IndexServlet';";
else
info = "alert('发表留言失败!');window.history.back();";
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println(" <HEAD>");
out.println(" <TITLE>HiMessage - TiandiNet.com</TITLE>");
out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
out.println(" </HEAD>");
out.println(" <BODY>");
out.println("<script language='JavaScript'>");
out.println(info);
out.println("</script>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
private String getHeadimas(HttpServletRequest request) {
String headimasSelect = "";
// init the path
ServletContext context = this.getServletContext();
String path = context.getRealPath("/");
path = path.replace('\\', '/');
int length = path.length();
if (path.charAt(length - 1) == '/') {
path = path.substring(0, length - 1);
}
String headimaPath = path + "/images/headimas/";
File dir = new File(headimaPath);
MyFilenameFilter myFilter = new MyFilenameFilter();
String[] files = dir.list(myFilter);
headimasSelect = "<select name=\"headima\" onchange=\"JavaScript:changeHeadima(this.options[this.selectedIndex].value" +");\">\n";
headimasSelect += "<option value=\"NONE\">Select one</option>\n";
for (int i = 0; i < files.length; i++) {
headimasSelect += "<option value=\"" + files[i] + "\">" + files[i] + "</option>\n";
}
headimasSelect += "</select>";
return headimasSelect;
}
}
class MyFilenameFilter implements FilenameFilter {
public boolean accept(File dir, String filename) {
//get the extend name
int position = filename.lastIndexOf(".");
String extendName = filename.substring(position + 1);
if (extendName.toLowerCase().equals("gif") && !filename.equals("no.gif")) {
return true;
}
else {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -