JSP implementation of the message Board page display, the new message displayed in the first ~

Source: Internet
Author: User

page :

Message servlet-----Messageservlet

Package CN. MuJH.newsManager.servlet;

Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.List;

Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Cn.MuJH.newsManager.biz.MessageManager;
Import Cn.MuJH.newsManager.biz.impl.MessageManagerImpl;
Import CN. MuJH.newsManager.entity.Message;
Import CN. MUJH.NEWSMANAGER.ENTITY.PAGEMSG;

@WebServlet (name = "MSG", Urlpatterns = {"/msg"})
public class Messageservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
Public Messageservlet () {
Super ();

}


protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
DoPost (request, response);
}

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
MessageManager mm=new Messagemanagerimpl ();
String OPR = Request.getparameter ("mth");
String author =request.getparameter ("author");
String message =request.getparameter ("message");
if (opr!=null) {
if (Author!=null | | author!= "") {
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Message msg=new Message (0,author,message,sdf.format (new Date ()));
Mm.addmsg (msg);
}
}else{
Request.setattribute ("author", author);
Request.setattribute ("message", message);
}

Pagemsg page=new pagemsg ();
Page.setpagesize (5);//per page show number of message lines
int Totalpage=mm.gettotalcount ()%page.getpagesize () ==0?mm.gettotalcount ()/page.getpagesize (): Mm.getTotalCount () /page.getpagesize (+1);
Page.settotalpage (Totalpage);
if (Request.getparameter ("PageIndex") ==null| | Request.getparameter ("pageIndex") = = "") {
Page.setpageindex (1);
}else{
Page.setpageindex (Integer.parseint (Request.getparameter ("PageIndex"));
}
List<message> List=mm.getmessagebypageindex (Page.getpageindex (), page.getpagesize ());
Page.setlist (list);
Request.setattribute ("page", page);

Request.getrequestdispatcher ("message.jsp"). Forward (request, response);
}

}

See figure:

Page Content---message.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
<! Doctype>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" src= "Js/jquery1.8.0.min.js" ></script>
<body>
<c:foreach items= "${page.list}" var= "MSG" >
<p>${msg.author}</p>
<p> content: ${msg.message}</p>
<p> Release date: ${msg.pubdate}</p>
</c:forEach>
<input type= "button" value= "previous" onclick= "Fenye (${page.pageindex-1})"
<c:if test= "${page.pageindex eq 1}" >disabled= "Disabled" </c:if>
>

<input type= "button" value= "Next" onclick= "Fenye (${page.pageindex+1})"
<c:if test= "${page.pageindex eq page.totalpage}" >disabled= "Disabled" </c:if>
>


<form action= "Msg?mth=save" method= "post" id= "MyForm" onsubmit= "return Check ()" >
<p> Username: <input type= "text" name= "author" id= "author" value= "${author}"/></p>
<p> message: <input type= "text" name= "message" id= "message" value= "${message}"/></p>
<p><input type= "Submit" value= "Leave a Message"/></p>
<input type= "hidden" name= "PageIndex" id= "PageIndex" >
</form>

<script>
function Check () {
if (document). getElementById ("Author"). value== "") {
Alert ("User name cannot be empty");
document.getElementById ("author"). focus ();
return false;
}
if (document). getElementById ("message"). value== "") {
Alert ("User name cannot be empty");
document.getElementById ("message"). focus ();
return false;
}
return true;
}
function Fenye (pageIndex) {
document.getElementById ("PageIndex"). Value=pageindex;
document.getElementById ("MyForm"). action= "MSG";
document.getElementById ("MyForm"). Submit ();
}
</script>
</body>

See figure:

Underlying code Picture: Message entity class

----Messagedao

The implementation class of the interface----Messagedaoimpl

----MessageManager

----Messagemanagerimpl

Database pictures:

JSP implementation of the message Board page display, the new message displayed in the first ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.