亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

java web實現(xiàn)簡單留言板功能

瀏覽:3日期:2022-08-20 15:13:19

本文實例為大家分享了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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产二区视频在线观看 | 美女久久精品 | 免费超爽成年大片黄 | 91青青青青青爽在线 | 放荡女同老师和女同学生 | 国产私密视频 | 精品在线视频免费观看 | 嫩草成人永久免费观看 | 国产精品视频在线免费观看 | 黄色h网站 | 婷婷综合在线观看丁香 | 亚洲日本欧美综合在线一 | 涩涩色中文综合亚洲 | 天天在线天天综合网色 | 国产一级精品视频 | 99国产精品一区二区 | 精品日本亚洲一区二区三区 | 性做爰片视频毛片 | 中文字幕s级优女区 | 欧美国产精品 | 一区二区中文字幕在线观看 | 日本欧美韩国一区二区三区 | 亚洲激情中文字幕 | 黄网站色在线视频免费观看 | 国产在线一区二区三区欧美 | 军营里娇喘呻吟声乳 | 国产成人精品综合在线观看 | 免费性| 男人你懂的在线观看视频 | 片在线观看视频免费 | 亚洲国产精品毛片∧v卡在线 | 国产婷婷色一区二区三区深爱网 | 男女配种超爽免费视频 | 日韩一区在线播放 | 国产精品福利在线观看入口 | 国产亚洲精品麻豆一区二区 | 国产三级网站在线观看 | 国产毛片精品 | 久久精品视频99精品视频150 | 1000部啪啪未满十八勿入福利 | 欧美成人久久一级c片免费 欧美成人午夜不卡在线视频 |