control层
action.form层
articalform(上传文章)
package cn.itcast.action.form;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.ArticalType;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.form.ArticalFormBean;
import cn.itcast.service.ArticalService;
import cn.itcast.service.ArticalTypeService;
import cn.itcast.service.impl.ArticalServiceImpl;
public class ArticalForm extends HttpServlet {
/*
* 将文章保存到数据库中,同时实现传入的各个字段的校验
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService as= (ArticalService) act.getBean("articalServiceImpl");
ArticalTypeService ats= (ArticalTypeService) act.getBean("articalTypeServiceImpl");
String title=request.getParameter("title");// 标题
String articalType=request.getParameter("articalType");// 文章类型
String content=request.getParameter("content");// 正文内容
Date sdTime=new Date();// 写文章时间
String info=request.getParameter("info");// 注释
String comeform=request.getParameter("comefrom");
ArticalFormBean af=new ArticalFormBean(title,content,info,comeform);
if(!af.init())
{
request.setAttribute("articalerror", af.getArticalerrormap());
request.getRequestDispatcher("/servlet/ShowArtical").forward(request, response);
return;
}
String[] prams={articalType};
List<ArticalType> typelist=ats.getScrollData(-1, -1, "o.name=?1",prams ).getResultlist();
if(typelist==null)
{
request.setAttribute("typeerror","文章类型不存在");
request.getRequestDispatcher("/servlet/ShowArtical").forward(request, response);
return;
}
try {
ArticalType at=typelist.get(0);
if(!(request.getParameter("id")==null||"".equals(request.getParameter("id"))))
{
Artical artical=as.find(Integer.parseInt(request.getParameter("id")));
artical.setArticalType(at);
artical.setComeform(comeform);
artical.setContent(content);
artical.setCount(1);
artical.setInfo(info);
artical.setSdTime(sdTime);
artical.setTitle(title);
as.update(artical);
request.getRequestDispatcher("/servlet/MasterBlog").forward(request, response);
return;
}
Artical artical=new Artical();
artical.setArticalType(at);
artical.setComeform(comeform);
artical.setContent(content);
artical.setCount(1);
artical.setInfo(info);
artical.setSdTime(sdTime);
artical.setTitle(title);
Master master=(Master) request.getSession(false).getAttribute("master");
artical.setMaster(master);
as.save(artical);
request.getRequestDispatcher("/servlet/MasterBlog").forward(request, response);
return;
} catch (Exception e) {
request.setAttribute("typeerror","文章类型未选择");
request.getRequestDispatcher("/servlet/ShowArtical").forward(request, response);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
LoginForm(用户登入)
package cn.itcast.action.form;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Master;
import cn.itcast.service.ArticalService;
import cn.itcast.service.MasterService;
public class LoginForm extends HttpServlet {
/*
* 用户登入校验,如过能通过正确的用户名密码找到master,怎跳转到博客主页界面,否则,跳转到登入界面
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession masterSession=request.getSession();
String name=request.getParameter("name");
String password=request.getParameter("password");
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
String[] parm={name,password};
List<Master> master= as.getScrollData(-1, -1, " o.name=?1 and o.password=?2 ",parm).getResultlist();
if(master!=null)
{
for(Master m:master)
{
masterSession.setAttribute("master",m);
}
}else
{
masterSession.setAttribute("master",null);
request.getRequestDispatcher("/page/index.jsp").forward(request, response);
return;
}
request.getRequestDispatcher("/servlet/MasterBlog").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
MasterForm(用户注册)
package cn.itcast.action.form;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.form.MasterFormBean;
import cn.itcast.service.ArticalService;
import cn.itcast.service.MasterService;
public class MasterFormAction extends HttpServlet {
/*
* 用户注册界面,注册成功跳转到index界面,校验不成功继续注册
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
String password=request.getParameter("password");
String repassword=request.getParameter("repassword");
String sex=request.getParameter("sex");
String email=request.getParameter("email");
MasterFormBean msbean=new MasterFormBean(name,password,repassword,sex,email);
if(!msbean.init())
{
request.setAttribute("error", msbean.getErrormap());
request.getRequestDispatcher("/page/regiest.jsp").forward(request, response);
return;
}else
{
Master master=new Master();
master.setEmail(email);
master.setName(name);
master.setPassword(password);
master.setRegiestdate(new Date());
master.setSex(sex);
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
as.save(master);
request.getRequestDispatcher("/page/index.jsp").forward(request, response);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
PhotoUpLoad(图片上传)
package cn.itcast.action.form;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Photo;
import cn.itcast.service.PhotoService;
import cn.itcast.utils.MyFileUtils;
public class PhotoUpLoad extends HttpServlet {
/*
* 图片上传,大小不得超过1M
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
PhotoService ps= (PhotoService) act.getBean("photoServiceImpl");
Master master=(Master) request.getSession(false).getAttribute("master");
String encoding=request.getCharacterEncoding();
String name;
String info;
String filename;
Photo photo=new Photo();
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setSizeThreshold(1024);
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setHeaderEncoding(encoding);
try {
List items=upload.parseRequest(request);
Iterator iter=items.iterator();
while(iter.hasNext())
{
FileItem fileitem=(FileItem) iter.next();
if(fileitem.isFormField())
{
if("name".equals(fileitem.getFieldName()))
{
name=fileitem.getString(encoding);
photo.setName(name);
}
if("photoinfo".equals(fileitem.getFieldName()))
{
info=fileitem.getString(encoding);
photo.setPhotoinfo(info);
}
}
if(!fileitem.isFormField())
{
filename=fileitem.getName();
photo.setPathname(filename);
long size=fileitem.getSize();
if(filename==null|| "".equals(filename.trim())||size==0)
{
continue;
}
File file=new File(this.getServletContext().getRealPath("/")+filename);
fileitem.write(file);
photo.setImage(MyFileUtils.fileTobyte(file));
file.delete();
photo.setUptime(new Date());
photo.setMaster(master);
ps.save(photo);
response.sendRedirect(request.getContextPath()+"/servlet/MasterBlog");
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
WordForm(文章评论)
package cn.itcast.action.form;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Word;
import cn.itcast.service.ArticalService;
import cn.itcast.service.WordService;
public class WordForm extends HttpServlet {
/*
* 通过Ajax完成用户评论
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService as= (ArticalService) act.getBean("articalServiceImpl");
WordService ws= (WordService) act.getBean("wordServiceImpl");
Master master=(Master) request.getSession(false).getAttribute("master");
int id=Integer.parseInt(request.getParameter("id"));
String parm=request.getParameter("content");
String content = URLDecoder.decode(parm, "UTF-8");
if(content==null||"".equals(content.trim()))
{
response.getWriter().write("游客:<br/> "+"评论内容为空");
}
Artical artical =as.find(id);
Word word=new Word();
word.setArtical(artical);
word.setContent(content);
word.setSetdate(new Date());
word.setMaster(master);
ws.save(word);
if(master==null)
{
response.getWriter().write("游客 :<br/> "+content);
}
else
{
response.getWriter().write(master.getName()+":<br/> "+content);
}
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return super.getServletInfo();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
AddFriend(添加朋友)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.service.FriendService;
import cn.itcast.service.MasterService;
public class AddFriend extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
FriendService fs= (FriendService) act.getBean("friendServiceImpl");
String friendname=request.getParameter("friendname");
Master master=(Master) request.getSession(false).getAttribute("master");
if(friendname==null||"".equals(friendname.trim()))
{
response.getWriter().write("好友名为空");
return;
}
Object[] params={friendname};
List<Master> friendlist=as.getScrollData(-1, -1, " o.name=?1 ", params).getResultlist();
if(friendlist==null)
{
response.getWriter().write("该用户不存在");
return;
}
try {
Master friendmaster= friendlist.get(0);
Friend friend=new Friend();
friend.setMaster(master);
friend.setName(friendmaster.getName());
friend.setQICQ(friendmaster.getQICQ()+"");
friend.setSex(friendmaster.getSex());
fs.save(friend);
response.getWriter().write(friend.getName());
} catch (Exception e) {
response.getWriter().write("该用户不存在");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
}
CencelMaster(用户注销)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CencalMaster extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().setAttribute("master", null);
response.sendRedirect(request.getContextPath()+"/servlet/ShowIndex");
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
DeleteArtical(删除文章)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.service.ArticalService;
public class DeleteArtical extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
int id=Integer.parseInt(request.getParameter("id"));
articalService.delete(id);
response.sendRedirect(request.getContextPath()+"/servlet/ListArtical");
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ListArtical(文章列表)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Master;
import cn.itcast.service.ArticalService;
public class ListArtical extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
Master m=(Master) request.getSession(false).getAttribute("master");
Object[] parm={m};
List<Artical> articallist=articalService.getScrollData(0, 3, " o.master=?1 ",parm ).getResultlist();
request.setAttribute("articallist", articallist);
request.getRequestDispatcher("/page/listblog.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
MasterBlog(博主博客)
package cn.itcast.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Photo;
import cn.itcast.service.ArticalService;
import cn.itcast.service.FriendService;
import cn.itcast.service.PhotoService;
import cn.itcast.utils.MyFileUtils;
public class MasterBlog extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
FriendService fs= (FriendService) act.getBean("friendServiceImpl");
PhotoService ps= (PhotoService) act.getBean("photoServiceImpl");
Master m=(Master) request.getSession(false).getAttribute("master");
Object[] parm={m};
List<Artical> articallist=articalService.getScrollData(0, 3, " o.master=?1 ",parm ).getResultlist();
List<Friend> friendlist=fs.getScrollData(-1, -1, " o.master=?1 ", parm).getResultlist();
List<Photo> photolist=ps.getScrollData(0,6," o.master=?1 ", parm).getResultlist();
for(Photo photo:photolist)
{
File file=MyFileUtils.byteTofail(photo.getImage(),this.getServletContext().getRealPath("/")+photo.getPathname());
}
request.setAttribute("articallist", articallist);
request.setAttribute("friendlist",friendlist);
request.setAttribute("photolist", photolist);
request.getRequestDispatcher("/page/masterblog.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ReEditBlog(博客编辑)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.ArticalType;
import cn.itcast.service.ArticalService;
import cn.itcast.service.ArticalTypeService;
public class ReEditBlog extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
ArticalTypeService as= (ArticalTypeService) act.getBean("articalTypeServiceImpl");
List<ArticalType> list=as.getScrollData().getResultlist();
request.setAttribute("type", list);
int id=Integer.parseInt(request.getParameter("id"));
Artical artical=articalService.find(id);
request.setAttribute("artical", artical);
request.getRequestDispatcher("/page/editblog.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
SerchFriend(查找好友)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.service.FriendService;
import cn.itcast.service.MasterService;
public class SearchFriend extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
String friendname=request.getParameter("friendname");
if(friendname==null||"".equals(friendname.trim()))
{
response.getWriter().write("好友名为空");
return;
}
Object[] params={friendname};
List<Master> friendlist=as.getScrollData(-1, -1, " o.name=?1 ", params).getResultlist();
if(friendlist==null)
{
response.getWriter().write("该用户不存在");
return;
}
try {
response.getWriter().write(friendlist.get(0).getName()+"可以添加");
} catch (Exception e) {
response.getWriter().write("该用户不存在");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
}
ShowArtical(显示文章类别,应为ShowArticalTypr)
package cn.itcast.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.ArticalType;
import cn.itcast.service.ArticalTypeService;
import cn.itcast.service.MasterService;
public class ShowArtical extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalTypeService as= (ArticalTypeService) act.getBean("articalTypeServiceImpl");
List<ArticalType> list=as.getScrollData().getResultlist();
request.setAttribute("type", list);
request.getRequestDispatcher("/page/editblog.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ShowFriendArtical(显示朋友文章)
package cn.itcast.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Word;
import cn.itcast.service.ArticalService;
import cn.itcast.service.WordService;
public class ShowFriendArtical extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService as= (ArticalService) act.getBean("articalServiceImpl");
WordService ws= (WordService) act.getBean("wordServiceImpl");
Master master=(Master) request.getSession(false).getAttribute("master");
int id=Integer.parseInt(request.getParameter("id"));
Artical artical=as.find(id);
Object[] parm={artical};
List<Word> listword=ws.getScrollData(-1, -1, " o.artical=?1 ", parm).getResultlist();
artical.setCount(artical.getCount()+1);
request.setAttribute("artical", artical);
request.setAttribute("listword",listword);
request.setAttribute("master", master);
as.update(artical);
request.getRequestDispatcher("/page/friendartical.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ShowFriendBlog(显示朋友博客)
package cn.itcast.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Photo;
import cn.itcast.service.ArticalService;
import cn.itcast.service.FriendService;
import cn.itcast.service.MasterService;
import cn.itcast.service.PhotoService;
import cn.itcast.utils.MyFileUtils;
public class ShowFriendBlog extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
PhotoService ps= (PhotoService) act.getBean("photoServiceImpl");
int id=Integer.parseInt(request.getParameter("id"));
Master friendmaster=as.find(id);
System.out.print(friendmaster.getName());
Object[] parm={friendmaster};
List<Artical> articallist=articalService.getScrollData(-1, -1, " o.master=?1 ",parm ).getResultlist();
List<Photo> photolist=ps.getScrollData(0,6," o.master=?1 ", parm).getResultlist();
for(Photo photo:photolist)
{
File file=MyFileUtils.byteTofail(photo.getImage(),this.getServletContext().getRealPath("/")+photo.getPathname());
}
request.setAttribute("friendmaster", friendmaster);
request.setAttribute("articallist", articallist);
request.setAttribute("photolist", photolist);
request.getRequestDispatcher("/page/friendblog.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
ShowIndex(显示主界面)
package cn.itcast.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.service.ArticalService;
import cn.itcast.service.FriendService;
import cn.itcast.service.MasterService;
public class ShowIndex extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
MasterService as= (MasterService) act.getBean("masterServiceImpl");
LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
orderby.put("count", "desc");
List<Artical> articallist=articalService.getScrollData(0, 20,orderby).getResultlist();
List<Master> friendlist=as.getScrollData(0, 20).getResultlist();
request.setAttribute("articallist", articallist);
request.setAttribute("friendlist",friendlist);
request.getRequestDispatcher("/page/index.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ShowPhoto(显示图片)
package cn.itcast.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.db.Artical;
import cn.itcast.bean.db.Friend;
import cn.itcast.bean.db.Master;
import cn.itcast.bean.db.Photo;
import cn.itcast.service.ArticalService;
import cn.itcast.service.FriendService;
import cn.itcast.service.PhotoService;
import cn.itcast.utils.MyFileUtils;
public class ShowPhoto extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
ArticalService articalService= (ArticalService) act.getBean("articalServiceImpl");
PhotoService ps= (PhotoService) act.getBean("photoServiceImpl");
Master m=(Master) request.getSession(false).getAttribute("master");
Object[] parm={m};
List<Photo> photolist=ps.getScrollData(-1,-1," o.master=?1 ", parm).getResultlist();
for(Photo photo:photolist)
{
File file=MyFileUtils.byteTofail(photo.getImage(),this.getServletContext().getRealPath("/")+photo.getPathname());
}
request.setAttribute("photolist", photolist);
request.getRequestDispatcher("/page/showphoto.jsp").forward(request, response);
return;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
control没有交给spring处理,只是交给web处理
CharacterEncodingFilter(字符编码)
package cn.itcast.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter {
private String encode;
@Override
public void destroy() {
// TODO Auto-generated method stub
this.encode=null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if(encode!=null)
{
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
encode=config.getInitParameter("encode");
}
}
Web-Xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>CKEditor for Java - Sample Web Application</display-name>
<description>CKEditor for Java. The server side integration for CKEditor.</description>
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ShowArtical</servlet-name>
<servlet-class>cn.itcast.action.ShowArtical</servlet-class>
</servlet>
<servlet>
<servlet-name>ArticalForm</servlet-name>
<servlet-class>cn.itcast.action.form.ArticalForm</servlet-class>
</servlet>
<servlet>
<servlet-name>MasterFormAction</servlet-name>
<servlet-class>cn.itcast.action.form.MasterFormAction</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginForm</servlet-name>
<servlet-class>cn.itcast.action.form.LoginForm</servlet-class>
</servlet>
<servlet>
<servlet-name>CencalMaster</servlet-name>
<servlet-class>cn.itcast.action.CencalMaster</servlet-class>
</servlet>
<servlet>
<servlet-name>MasterBlog</servlet-name>
<servlet-class>cn.itcast.action.MasterBlog</servlet-class>
</servlet>
<servlet>
<servlet-name>SearchFriend</servlet-name>
<servlet-class>cn.itcast.action.SearchFriend</servlet-class>
</servlet>
<servlet>
<servlet-name>AddFriend</servlet-name>
<servlet-class>cn.itcast.action.AddFriend</servlet-class>
</servlet>
<servlet>
<servlet-name>PhotoUpLoad</servlet-name>
<servlet-class>cn.itcast.action.form.PhotoUpLoad</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowPhoto</servlet-name>
<servlet-class>cn.itcast.action.ShowPhoto</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowPhotos</servlet-name>
<servlet-class>cn.itcast.action.ShowPhotos</servlet-class>
</servlet>
<servlet>
<servlet-name>ListArtical</servlet-name>
<servlet-class>cn.itcast.action.ListArtical</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteArtical</servlet-name>
<servlet-class>cn.itcast.action.DeleteArtical</servlet-class>
</servlet>
<servlet>
<servlet-name>ReEditBlog</servlet-name>
<servlet-class>cn.itcast.action.ReEditBlog</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowIndex</servlet-name>
<servlet-class>cn.itcast.action.ShowIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowFriendBlog</servlet-name>
<servlet-class>cn.itcast.action.ShowFriendBlog</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowFriendArtical</servlet-name>
<servlet-class>cn.itcast.action.ShowFriendArtical</servlet-class>
</servlet>
<servlet>
<servlet-name>WordForm</servlet-name>
<servlet-class>cn.itcast.action.form.WordForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowArtical</servlet-name>
<url-pattern>/servlet/ShowArtical</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ArticalForm</servlet-name>
<url-pattern>/servlet/ArticalForm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MasterFormAction</servlet-name>
<url-pattern>/servlet/MasterFormAction</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginForm</servlet-name>
<url-pattern>/servlet/LoginForm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CencalMaster</servlet-name>
<url-pattern>/servlet/CencalMaster</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MasterBlog</servlet-name>
<url-pattern>/servlet/MasterBlog</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SearchFriend</servlet-name>
<url-pattern>/servlet/SearchFriend</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddFriend</servlet-name>
<url-pattern>/servlet/AddFriend</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PhotoUpLoad</servlet-name>
<url-pattern>/servlet/PhotoUpLoad</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowPhoto</servlet-name>
<url-pattern>/servlet/ShowPhoto</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowPhotos</servlet-name>
<url-pattern>/servlet/ShowPhotos</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListArtical</servlet-name>
<url-pattern>/servlet/ListArtical</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteArtical</servlet-name>
<url-pattern>/servlet/DeleteArtical</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ReEditBlog</servlet-name>
<url-pattern>/servlet/ReEditBlog</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowIndex</servlet-name>
<url-pattern>/servlet/ShowIndex</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowFriendBlog</servlet-name>
<url-pattern>/servlet/ShowFriendBlog</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowFriendArtical</servlet-name>
<url-pattern>/servlet/ShowFriendArtical</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WordForm</servlet-name>
<url-pattern>/servlet/WordForm</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>cn.itcast.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>