📄 myblogaction.java
字号:
package com.blog;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
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 com.blog.tableclass.*;
public class MyBlogAction extends HttpServlet {
/**
* Constructor of the object.
*/
public MyBlogAction() {
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 {
// 设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String blogId = request.getParameter("blogid");
String sortId = request.getParameter("sortid");
String pageId = request.getParameter("pageid");
HttpSession session = request.getSession(true);
int iBlogId = 0;
OperateDatabase db=new OperateDatabase();
if (blogId!=null) {
iBlogId = Integer.parseInt(blogId);
Blog blog = db.getBlog(iBlogId);
session.setAttribute(Constants.LOGIN_USER_KEY,blog);
}
else {
Blog blog = (Blog)session.getAttribute(Constants.LOGIN_USER_KEY);
iBlogId = blog.getId();
}
int iSortId = 0;
if (sortId!=null) {
iSortId = Integer.parseInt(sortId);
session.setAttribute(Constants.CUR_SORTID_KEY,new Integer(iSortId));
}
else{
Integer temp = (Integer)session.getAttribute(Constants.CUR_SORTID_KEY);
iSortId = temp.intValue();
}
/*
* 页号处理
*/
int iPageId = 0;
int pageCount = 0;
List articleList=db.getBlogArticles(iBlogId,iSortId);
if (pageId!=null) {
iPageId = Integer.parseInt(pageId);
if (iPageId<0) iPageId = 0;
/*
* 获取总页数
*/
if (articleList.size()%Constants.ARTICLE_PAGE_SIZE ==0){
pageCount=articleList.size() / Constants.ARTICLE_PAGE_SIZE;
}
else{
pageCount=articleList.size() / Constants.ARTICLE_PAGE_SIZE+1;
}
if (iPageId>=pageCount) iPageId = pageCount-1;
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(iPageId));
}
else {
Integer temp = (Integer)session.getAttribute(Constants.CUR_PAGEID_KEY);
iPageId = temp.intValue();
}
/*
* 分页显示
*/
List dispList=new ArrayList();
if ((articleList.size()>iPageId * Constants.ARTICLE_PAGE_SIZE )&&(iPageId>=0)){
for (int i=iPageId*Constants.ARTICLE_PAGE_SIZE;i<(iPageId+1)*Constants.ARTICLE_PAGE_SIZE;i++){
if (i<articleList.size()){
dispList.add(articleList.get(i));
}
}
}
session.setAttribute(Constants.ARTICLE_LIST_KEY,dispList);
response.sendRedirect("/NewBlog/users/MyBlog.jsp");
}
/**
* 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 {
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -