java web實現(xiàn)簡單留言板功能
本文實例為大家分享了java web實現(xiàn)簡單留言板的具體代碼,供大家參考,具體內(nèi)容如下
一、目標(biāo)
用戶可以登錄并記住密碼進入留言板,添加留言,點擊留言列表中的標(biāo)題可顯示該條留言全部內(nèi)容。
二、相關(guān)知識
1、使用Cookie實現(xiàn)自動登錄
用戶第一次登錄網(wǎng)站,服務(wù)器將用戶名和密碼以Cookie的形式發(fā)送到客戶端。當(dāng)客戶之后再次訪問該網(wǎng)站時,瀏覽器自動將Cookie文件中的用戶名和密碼隨請求一起發(fā)送到服務(wù)器,服務(wù)器從Cookie中取出用戶名和密碼并且通過驗證。
2、java類的定義與使用
定義java類時注意屬性的定義、構(gòu)造方法的重寫,定義兩個方法,一個是訪問方法(getter),一個是修改方法(setter)。
三、實現(xiàn)思路
1、登錄
輸入用戶名、密碼登錄,對應(yīng)檢測登錄BoardLoginServlet可實現(xiàn)記住登錄信息 Cookie實現(xiàn)登錄成功顯示留言板標(biāo)題列表頁面board-title.jsp,登錄失敗返回登錄頁面
2、顯示留言標(biāo)題
標(biāo)題列表為上下文變量lists,其中標(biāo)題為鏈接,點擊可顯示留言具體信息界面(跳轉(zhuǎn)到MessageServlet處理)。
list = username+':'+'<a href=’MessageServlet?title='+title+'’target=’_parent’>'+title+'</a>';
頁面下方有新增留言的鏈接,點擊后跳轉(zhuǎn)到寫留言頁面
<a href='http://www.aoyou183.cn/bcjs/board-write.jsp' rel='external nofollow' rel='external nofollow' >新增留言</a>
3、添加留言
輸入標(biāo)題、內(nèi)容 post給WriteServlet
4、顯示留言具體信息
在MessageServlet中遍歷查找留言并傳遞變量,顯示留言信息在board-message.jsp
四、代碼實現(xiàn)
board-login.jsp
<form action='board.do' method='post'> <br> <h1 >登錄界面</h1> 昵稱:<input type='text' name='username' value='${cookie.username.value}'/><br> 密碼:<input type='password' name='password' value='${cookie.password.value}'/><br><br> <input type='checkbox' name='check' value='check'/>記住密碼<br> <br> <input type='submit' value='登錄'/> <input type='reset' value='取消'/></form>
board-title.jsp
<h2>留言板</h2> ${lists}<a href='http://www.aoyou183.cn/bcjs/board-write.jsp' rel='external nofollow' rel='external nofollow' >新增留言</a>
boarrd-write.jsp
<form action='write.do' method='post'> 標(biāo)題:<input type='text' name='title'/><br> 內(nèi)容:<input type='text' name='text'/><br> <input type='submit'/></form>
board-message.jsp
<h2>留言詳情</h2> 用戶名:${name}<br> 標(biāo)題:${title}<br> 內(nèi)容:${text}<br> 時間:${date}<br>
Message.java(將留言信息存儲在Message類中)
package board;import java.util.Date;public class Message { private String title;//標(biāo)題 private String time;//時間 private String username;//用戶名 private String text;//內(nèi)容 public Message(){} //初始化 public Message(String title,String time,String username,String text){ this.title = title; this.time = time; this.username = username; this.text = text; } public String getTitle(){ return title; } public String getUsername(){ return username; } public String getTime(){ return time; } public String getText(){ return text; }}
BoardLoginServlet.java(用Cookie登錄)
package board;import javax.servlet.ServletException;import javax.servlet.http.*;import java.io.IOException;@javax.servlet.annotation.WebServlet(name = 'BoardLoginServlet',urlPatterns = '/board.do')public class BoardLoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType('text/html;charset = utf-8'); request.setCharacterEncoding('UTF-8'); HttpSession session=request.getSession(); String username = request.getParameter('username').trim(); String password = request.getParameter('password').trim(); session.setAttribute('username',username); if (!(username.equals('admin') &&password.equals('admin'))&&!(username.equals('123') &&password.equals('123'))) { response.sendRedirect('board-login.jsp'); } else { if ((request.getParameter('check') != null) && (request.getParameter('check').equals('check'))) { Cookie nameCookie = new Cookie('username', username); Cookie pswdCookie = new Cookie('password', password); nameCookie.setMaxAge(60 * 60); pswdCookie.setMaxAge(60 * 60); nameCookie.setPath('/'); pswdCookie.setPath('/'); response.addCookie(nameCookie); response.addCookie(pswdCookie); String value1 = '', value2 = ''; Cookie cookie = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if (cookie.getName().equals('username')) value1 = cookie.getValue(); if (cookie.getName().equals('password')) value2 = cookie.getValue(); } if (value1.equals('admin') && value2.equals('admin')||value1.equals('123') && value2.equals('123')) { response.sendRedirect('board-title.jsp'); } else { response.sendRedirect('board-login.jsp'); } } } else { response.sendRedirect('board-title.jsp'); } } }}
WriteServlet.java(新增留言)
package board;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.*;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;@WebServlet(name = 'WriteServlet',urlPatterns = '/write.do')public class WriteServlet extends HttpServlet { //messages列表中存放Message類對象 public List<Message> messages = new ArrayList<Message>(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType('text/html;charset=utf-8'); ServletContext context = request.getServletContext(); HttpSession session = request.getSession(); //用戶名存儲到session中 String username =(String)session.getAttribute('username'); String title = request.getParameter('title'); String text = request.getParameter('text'); //獲取當(dāng)前時間 SimpleDateFormat formatter= new SimpleDateFormat('yyyy-MM-dd ’at’ HH:mm:ss'); Date date = new Date(System.currentTimeMillis()); String time = formatter.format(date); //在messages列表中添加留言對象 messages.add(new Message(title,time,username,text)); context.setAttribute('messages',messages); String list = new String(); //list中存放留言用戶名+留言標(biāo)題,標(biāo)題為鏈接,可跳轉(zhuǎn)到留言具體信息,并傳參title list = username+':'+'<a href=’MessageServlet?title='+title+'’target=’_parent’>'+title+'</a>'; String lists = new String(); //將留言標(biāo)題列表信息存到上下文對象 if(context.getAttribute('lists')!=null){ lists = context.getAttribute('lists') + list + '<br>'; } else{ lists = list + '<br>'; } context.setAttribute('lists', lists); request.setAttribute('lists', lists); //轉(zhuǎn)發(fā) RequestDispatcher dispatcher = request.getRequestDispatcher('/board-title.jsp'); dispatcher.forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
MessageServlet.java
package board;import com.sun.net.httpserver.HttpContext;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;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 java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;@WebServlet(name = 'MessageServlet',urlPatterns = '/MessageServlet')public class MessageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType('text/html;charset=utf-8'); ServletContext context = request.getServletContext(); List<Message> messages = new ArrayList<Message>(); //獲取列表 messages = (List<Message>)context.getAttribute('messages'); //獲取鏈接中的參數(shù)title String title =request.getParameter('title'); String name = new String(); String date = new String(); String text = new String(); //遍歷列表,查找該title的具體信息 for(Message message : messages){ if((message.getTitle()).equals(title)){ name = message.getUsername(); date = message.getTime(); text = message.getText(); title = message.getTitle(); request.setAttribute('name',name); request.setAttribute('title',title); request.setAttribute('text',text); request.setAttribute('date',date); break; } } //將留言信息轉(zhuǎn)發(fā)到board-message.jsp RequestDispatcher dispatcher = request.getRequestDispatcher('/board-message.jsp'); dispatcher.forward(request, response); }}
(以上僅為部分代碼)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3實例分享之多重背景的實現(xiàn)(Multiple backgrounds)2. XHTML 1.0:標(biāo)記新的開端3. HTML5 Canvas繪制圖形從入門到精通4. XML解析錯誤:未組織好 的解決辦法5. ASP基礎(chǔ)知識VBScript基本元素講解6. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實現(xiàn)代碼7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 利用CSS3新特性創(chuàng)建透明邊框三角9. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法10. XML入門的常見問題(四)
