av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

本文實(shí)例為大家分享了java web實(shí)現(xiàn)簡單留言板的具體代碼,供大家參考,具體內(nèi)容如下

一、目標(biāo)

用戶可以登錄并記住密碼進(jìn)入留言板,添加留言,點(diǎn)擊留言列表中的標(biāo)題可顯示該條留言全部內(nèi)容。

二、相關(guān)知識

1、使用Cookie實(shí)現(xiàn)自動登錄

用戶第一次登錄網(wǎng)站,服務(wù)器將用戶名和密碼以Cookie的形式發(fā)送到客戶端。當(dāng)客戶之后再次訪問該網(wǎng)站時(shí),瀏覽器自動將Cookie文件中的用戶名和密碼隨請求一起發(fā)送到服務(wù)器,服務(wù)器從Cookie中取出用戶名和密碼并且通過驗(yàn)證。

2、java類的定義與使用

定義java類時(shí)注意屬性的定義、構(gòu)造方法的重寫,定義兩個(gè)方法,一個(gè)是訪問方法(getter),一個(gè)是修改方法(setter)。

三、實(shí)現(xiàn)思路

1、登錄

輸入用戶名、密碼登錄,對應(yīng)檢測登錄BoardLoginServlet可實(shí)現(xiàn)記住登錄信息 Cookie實(shí)現(xiàn)登錄成功顯示留言板標(biāo)題列表頁面board-title.jsp,登錄失敗返回登錄頁面

2、顯示留言標(biāo)題

標(biāo)題列表為上下文變量lists,其中標(biāo)題為鏈接,點(diǎn)擊可顯示留言具體信息界面(跳轉(zhuǎn)到MessageServlet處理)。

list = username+':'+'<a href=’MessageServlet?title='+title+'’target=’_parent’>'+title+'</a>';

頁面下方有新增留言的鏈接,點(diǎn)擊后跳轉(zhuǎn)到寫留言頁面

<a href='http://m.4tl426be.cn/bcjs/board-write.jsp' rel='external nofollow' rel='external nofollow' >新增留言</a>

3、添加留言

輸入標(biāo)題、內(nèi)容 post給WriteServlet

4、顯示留言具體信息

在MessageServlet中遍歷查找留言并傳遞變量,顯示留言信息在board-message.jsp

四、代碼實(shí)現(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://m.4tl426be.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> 時(shí)間:${date}<br>

Message.java(將留言信息存儲在Message類中)

package board;import java.util.Date;public class Message { private String title;//標(biāo)題 private String time;//時(shí)間 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)前時(shí)間 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)文章:
主站蜘蛛池模板: 日本三级精品 | 国产成人福利在线观看 | 精品一区二区三区中文字幕 | 成人免费一区二区三区牛牛 | 日本特黄a级高清免费大片 成年人黄色小视频 | 国产剧情一区二区三区 | 久久久久国色av免费观看性色 | 狠狠操狠狠 | 九九久久久久久 | 久久精品亚洲精品国产欧美kt∨ | 久久久www成人免费无遮挡大片 | 国产精品久久久久久吹潮日韩动画 | 一本色道精品久久一区二区三区 | 久久国内 | 亚洲欧洲日本国产 | 国产精品久久久久久久毛片 | 久久久久久国产精品免费免费 | 天天射影院 | 日韩在线欧美 | 亚洲一区二区三区 | 久久成人一区 | 一区二区三区四区不卡 | 亚洲视频在线播放 | 青娱乐av | 热久久国产 | 成年人黄色一级片 | 在线看亚洲 | 久久久精品网 | 玖玖在线精品 | 久草在线 | 久久精品男人的天堂 | av看看| 国产一区二区三区久久久久久久久 | 免费在线观看一级毛片 | 欧美激情一区二区三级高清视频 | 日韩久久久久 | 久久久久9999亚洲精品 | 日韩一级 | 精品欧美一区二区在线观看欧美熟 | 一区二区视频在线 | 久久国产婷婷国产香蕉 |