?? saymessageprocessor.java
字號:
package org.sunxin.guestbook.controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.sunxin.guestbook.*;
import org.sunxin.guestbook.beans.*;
import org.sunxin.guestbook.util.*;
public class SayMessageProcessor implements Processor
{
public void perform(HttpServletRequest req,
HttpServletResponse resp,
ServletContext sc)
throws IOException,ServletException,GuestbookException
{
HttpSession session=req.getSession(false);
String username=null;
//判斷用戶是否已經登錄。
if(null==session || null==(username=(String)session.getAttribute("user")))
{
throw new GuestbookException(
"您還沒有登錄,請先<a href=\"logon.jsp\">登錄</a>!");
}
String action=req.getParameter("action");
//判斷用戶是訪問留言頁面還是提交留言。
if(null==action || !"say".equals(action))
{
try
{
StringBuffer sb=new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("<page name=\"say\">");
sb.append("<say/></page>");
req.setAttribute("page", sb.toString());
}
catch (Exception e)
{
throw new ServletException("頁面產生錯誤!");
}
}
else
{
String title=req.getParameter("title");
String content=req.getParameter("content");
if(null==title || "".equals(title) || null==content)
{
throw new GuestbookException(
"留言主題不能為空,請<a href=\"say.jsp\">返回</a>重新填寫!");
}
title=CharacterConvert.toHtml(title.trim());
content=CharacterConvert.toHtml(content.trim());
//創建Message對象,保存用戶留言的相關信息。
Message msgBean=new Message();
msgBean.setUsername(username);
msgBean.setTitle(title);
msgBean.setContent(content);
String fromIP=req.getRemoteAddr();
msgBean.setIp(fromIP);
GuestbookDB gstDB = (GuestbookDB)sc.getAttribute("gstdb");
//將用戶留言的相關信息保存到數據庫中。
gstDB.saveMessage(msgBean);
String strMsg="發送成功,<a href=\"say.jsp\">繼續留言</a>,或者<a href=\"index.jsp\">查看留言</a>";
req.setAttribute("success",strMsg);
new SuccessProcessor().perform(req, resp,sc);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -