话说:

各位读者朋友,晚上好!前面介绍了注册登录、首页,本篇介绍个人中心

难度系数:★★☆☆☆
目录
1.页面展示
2.个人中心pojo
3.个人中心帖子列表、发帖、修改、删除

1.页面展示
userInfo.jsp-个人中心
addText.jsp-发帖
updateText.jsp-修改帖子

userInfo.jsp-个人中心

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!--引入JSTL标签库  --><%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<!--Author:Meice 2018年1月1日下午6:43:53 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小确幸论坛</title>
<link href="css/textList.css" rel="stylesheet" type="text/css">
<link href="css/userinfo.css" rel="stylesheet" type="text/css">
<style type="text/css">a:hover{color:orange;}
</style></head>
<body><jsp:include page="head.jsp" ></jsp:include><!--登录后用户信息  --><div id="userInfo"><a href="textShow" class="userInfo">首页</a><span class="userInfo">欢迎【${user.userNick}】!</span><br/><a href="addText.jsp" class="addText" style="color:orange;">发帖</a><a href="textShow" class="login.jsp">退出</a></div><!--个人资料  --><div id="myInfo" style="width: 300px;margin-left:100px;"><img src="data:images" style="width:270px;height:170px;"><br/><span style="padding-left:85px;">性别:&nbsp;&nbsp;${user.sex }</span><br/><span>最新登录时间:&nbsp;&nbsp;${user.loginTime }</span><br/><span>注&nbsp;&nbsp;册&nbsp;&nbsp;时&nbsp;&nbsp;间:&nbsp;&nbsp;${user.registerTime}</span><br/><a  href="editUserInfo.jsp" style="padding-left:85px;">修改资料</a></div><!--个人帖子  --><div id="myText"><span>Ta滴帖子</span><p style="height: 20px;"></p><!-- <hr style="font-size: 10px;font-color:red;"/> --><table border="1 solid red" width="100%" ><thead><tr style="background-color: rgb(233,248,230);"><th>帖子名</th><th>栏目</th><th>发布时间</th><th>操作</th></tr></thead><tbody><c:forEach var="listUserInfo" items="${listUserInfo}"><tr><td><a href="textDetail?textId=${listUserInfo.text.textId}">${listUserInfo.text.title }</a></td><td>${listUserInfo.category.categoryName }</td><td>${listUserInfo.text.textTime}</td><td align="center"><a href="updateText?textId=${listUserInfo.text.textId}" style="color:green;font-weight:bolder">修改</a> |<a href="delText?textId=${listUserInfo.text.textId}" style="color:green;font-weight:bolder" onclick="return confirm('真的忍心删除我麽?')">删除</a></td></tr></c:forEach></tbody></table></div><jsp:include page="foot.jsp"></jsp:include></body>
</html>

addText.jsp-发帖

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!--Author:Meice 2018年1月3日上午3:48:27 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小确幸论坛</title>
<link href="css/textList.css" rel="stylesheet" type="text/css">
<link href="css/addText.css" rel="stylesheet" type="text/css"><style type="text/css">textarea{resize:none;}
</style>
</head>
<body>  <jsp:include page="head.jsp"></jsp:include><!--登录后用户信息  --><div id="userInfo" style=" margin:-100px 0px 0px 1280px;"><a href="textShow" class="userInfo">首页</a><span class="userInfo" style="color:white;">欢迎【${user.userNick}】!</span><br/><a href="login.jsp" class="login.jsp">退出</a></div><div id="mycookie">论坛首页 > 发新帖</div><div id="addText"><form action="addText" method=-"get"><table><tr><td>标题:</td><td><input type="text" name="title" style="width:600px;height:30px;"></td><td><select name="category" style="width:100px;height:30px;"><option>选择栏目</option><option>Java</option><option>MySQL</option><option>大数据</option><option>人工智能</option><option>HTML5</option><option>生活</option><option>有趣有料</option></select></td></tr><tr><td style="text-align: inherit;">内容:</td></tr><tr><td colspan="3"><textarea name="context" cols="150" rows="20"></textarea></td></tr><tr><td colspan="3" align="right"> <input type="submit" value="发布" style="width:100px;height:30px;"></td></tr></table></form></div><jsp:include page="foot.jsp"></jsp:include></body>
</html>

updateText.jsp-修改帖子

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>    <!--Author:Meice 2018年1月3日上午3:48:27 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小确幸论坛</title>
<link href="css/textList.css" rel="stylesheet" type="text/css">
<link href="css/addText.css" rel="stylesheet" type="text/css"><style type="text/css">textarea{resize:none;}
</style>
</head>
<body>  <jsp:include page="head.jsp"></jsp:include><!--登录后用户信息  --><div id="userInfo" style=" margin:-100px 0px 0px 1280px;"><a href="textShow" class="userInfo">首页</a><span class="userInfo" style="color:white;">欢迎【${user.userNick}】!</span><br/><a href="login.jsp" class="login.jsp">退出</a></div><div id="mycookie">论坛首页 > 修改帖子</div><div id="addText"><form action="doUpdateText?textId=${userInfo.text.textId}" method="get"><table><tr><td>标题:</td><td><input type="text" name="title"   value="${userInfo.text.title}"   style="width:600px;height:30px;"></td><td><input type="hidden" name="textId" value="${userInfo.text.textId}"></td><td><select name="category" style="width:100px;height:30px;"><!-- 这里不能写固定,要通过遍历写 --><!-- <option>选择栏目</option> -->  <c:forEach var="category" items="${listCategory}" ><c:if test="${category.categoryId==userInfo.category.categoryId}"><option selected="selected">${category.categoryName}</option></c:if><c:if test="${category.categoryId!=userInfo.category.categoryId}"><option >${category.categoryName}</option></c:if></c:forEach></select></td></tr><tr><td style="text-align: inherit;">内容:</td></tr><tr><td colspan="3"><textarea name="context"   cols="150" rows="20">${userInfo.text.context }</textarea></td></tr><tr><td colspan="3" align="right"> <input type="submit" value="修改" style="width:100px;height:30px;"></td></tr></table></form></div><jsp:include page="foot.jsp"></jsp:include></body>
</html>

2.个人中心pojo
个人中心,本质还是显示帖子,只不过多了个栏目,所以Pojo这里改动了下,直接查四个表,里面直接放对象。这样取值非常方便。
useInfo

package com.hmc.pojo;/**
*
*2018年1月1日
*User:Meice
*下午7:53:46
*/
public class UserInfo {//个人中心帖子和首页帖子唯一区别就是个人中心多了栏目,所以,可以定义2个对象就行private Text text;private Category category;public UserInfo() {}public UserInfo(Text text,Category category) {this.text = text;this.category = category;}public Text getText() {return text;}public void setText(Text text) {this.text = text;}public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;}@Overridepublic String toString() {return "UserInfo [text=" + text + ", category=" + category + "]";}}

这里面全是对象,到时候在框架部分,就绑定配置即可。

3. 个人中心帖子列表、发帖、修改、删除
add update del底层封装了Cud方法,因为都是直接修改数据库,不涉及查出来放到一个容器中,所以Cud方法照样适用。

复杂点的地方上就是修改的时候,栏目页面不能写死,要从页面遍历,这样才能保证option选择的是对应的栏目。

这里面除了首页查在TextShowDaoImpl里面写了方法,其他都调用Cud()方法即可。

1)个人中心帖子列表
2)发帖
3)修改
4)删除

1)个人中心帖子列表
UserInfoServlet

package com.hmc.controller;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hmc.pojo.User;
import com.hmc.pojo.UserInfo;
import com.hmc.service.TextShowService;
import com.hmc.service.TextShowServiceImpl;/**
*
*2018年1月1日
*User:Meice
*下午7:11:51
*/
public class UserInfoServlet  extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.接收参数 userId 用户存在session里面 直接取值User user =(User)req.getSession().getAttribute("user");int userId = user.getUserId();//2.调用方法  显示当前用户的信息TextShowService tss = new TextShowServiceImpl();String sql = "select t.textId,t.title,t.textTime,t.context,t.categoryId,t.userId,c.categoryName "+ "from text t "+ "inner join "+ "category c on t.categoryId = c.categoryId "+ "where userId = ?"+ " order by "+ "t.textId desc";Object[] params = {userId}; List<UserInfo> listUserInfo =   tss.listUserInfo(sql, params);//3.跳转页面req.setAttribute("listUserInfo", listUserInfo);req.getRequestDispatcher("userInfo.jsp").forward(req, resp);}
}

2)发帖
AddTextServlet

package com.hmc.controller;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hmc.pojo.Category;
import com.hmc.pojo.User;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetFormatTimeUtil;/**
*
*2018年1月3日
*User:Meice
*上午4:19:22
*/public class AddTextServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.接收发帖内容String title =  req.getParameter("title");String categoryName = req.getParameter("category");String context = req.getParameter("context");System.out.println("发帖内容:"+title + "   "+categoryName+"  "+context);CategoryService cs = new CategoryServiceImpl();Category category = cs.getOneCategory(categoryName);String textTime =GetFormatTimeUtil.getFormatTime(24);User user =(User)req.getSession().getAttribute("user");int userId = user.getUserId();//2.调用发帖方法UserRegisterService urs = new UserRegisterServiceImpl();String sql = "insert into text (title,context,textTime,categoryId,userId) values (?,?,?,?,?)";Object[] params = {title,context,textTime,category.getCategoryId(),userId};int result =    urs.Cud(sql, params);//3.跳转个人中心if(result>0) {req.getRequestDispatcher("userInfo").forward(req, resp);}else {System.out.println("遗憾,发帖失败");}}}

3)修改
UpdateTextServlet

package com.hmc.controller;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hmc.pojo.Category;
import com.hmc.pojo.Text;
import com.hmc.pojo.UserInfo;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.TextShowService;
import com.hmc.service.TextShowServiceImpl;
import com.hmc.util.GetStrToInt;/**
*
*2018年1月3日
*User:Meice
*上午5:12:55
*/
public class UpdateTextServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.接收参数String textIdStr =req.getParameter("textId");   int textId = GetStrToInt.getInt(textIdStr);//2.调用方法String sql = "select t.textId,t.title,t.context,t.categoryId,t.textTime, c.categoryId,c.categoryName ,t.userId from text t inner join category c on t.categoryId = c.categoryId where textId = ?";Object[] params = {textId};TextShowService tss = new TextShowServiceImpl();List<UserInfo> listUserInfo =   tss.listUserInfo(sql, params);UserInfo userInfo = listUserInfo.get(0);System.out.println("得到单个帖子信息:"+userInfo);//把整个栏目对象传过去,便于页面取值CategoryService gs = new CategoryServiceImpl();String sql2 = "select * from category";List<Category> listCategory =   gs.getAllCagegory(sql2, null);System.out.println("得到所有栏目集合:"+listCategory);//3.页面跳转req.setAttribute("userInfo", userInfo);req.setAttribute("listCategory", listCategory);req.getRequestDispatcher("updateText.jsp").forward(req, resp);}
}

上面是把要修改的帖子放到页面,下面才真正执行修改:
DoUpdateTextServlet

package com.hmc.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hmc.pojo.Category;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetStrToInt;/**
*
*2018年1月3日
*User:Meice
*上午6:23:35
*/
public class DoUpdateTextServlet  extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.接收参数String textIdStr = req.getParameter("textId");int textId = GetStrToInt.getInt(textIdStr);String title =  req.getParameter("title");String categoryName =   req.getParameter("category");String context  = req.getParameter("context");System.out.println(textId);CategoryService cs = new CategoryServiceImpl();Category category = cs.getOneCategory(categoryName);int categoryId = category.getCategoryId();//2.调用方法UserRegisterService urs = new UserRegisterServiceImpl();String sql = "update text set title=?,categoryId = ?,context=? where textId = ?";Object[] params = {title,categoryId,context,textId};int result =    urs.Cud(sql, params);System.out.println(result);//3.跳转页面if(result>0) {req.getRequestDispatcher("userInfo").forward(req, resp);}else {System.out.println("遗憾,修改失败!");}}
}

4)删除
DelTextServlet

package com.hmc.controller;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetStrToInt;/**
*
*2018年1月3日
*User:Meice
*上午6:36:42
*/
public class DelTextServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.接收参数String textIdStr = req.getParameter("textId");int textId = GetStrToInt.getInt(textIdStr);//2.调用方法UserRegisterService urs = new UserRegisterServiceImpl();String sql = "delete from text where textId = ?";Object[] params = {textId};int result =    urs.Cud(sql, params);//3.页面跳转if(result>0) {req.getRequestDispatcher("userInfo").forward(req, resp);}else {System.out.println("删除失败。。");}}
}

好了,到此为止,今天内容结束了,暂不总结吧,总结放在最后面。

其实,这里可以看到,每写一个功能,调用一个Servlet,这样Servlet很多,后面框架就改善了这种情况,一个Controller里面,写多个方法,根据路径,直接调用到方法。由此可知,Servlet 本质根据路径直接访问Servlet(类),框架是直接根据路径访问方法。

好了,晚安!下期预告:帖子详情

小确幸BBS论坛-3-个人中心相关推荐

  1. 小确幸BBS论坛-6-优化不止步!

    话说: 各位读者朋友,中午好!春节越来越近,归家心切丫! 前面一系列博客详细介绍了过程,但是没有复盘,这结尾的2篇博客就完成这件事. 这篇博客目标:优化小确幸论坛. 小确幸BBS论坛-1-5实现了最核 ...

  2. 小确幸BBS论坛-1-前期准备

    话说: 各位读者晚上好!终于到了2018!2018年的第一篇博客哈!前面中断了不少篇,并非作者怠惰了,确实感觉"拿不出手",嘿嘿.经过一段时间积累,这次就连续的发表一个系列吧--B ...

  3. 冬日里的小确幸无非是美味在旁

    你的生活是怎样的呢?每个人可能都会有不同的回答,对待生活的方式也不近相同.一日三餐,对于奔波的人可能仔细地去运作完成. 美食,在很大程度上给予了我们幸福感,也决定了你的生活态度. 冬日里不管是早起的你 ...

  4. 双11:天猫要“软着陆”,商家要“小确幸”

    世事如棋局局新,关关难过关关过.今年的双11,还是有不少变化的. 比如,满减的门槛降低.双11的满减变化,其实可以解读很多信息.2019年以及之前是"满400减50",2020年是 ...

  5. 计算机课堂热身游戏,童年小确幸 儿时电脑课里我们玩不腻的13个小游戏

    原标题:童年小确幸 儿时电脑课里我们玩不腻的13个小游戏 还记得小时候电脑课一到教室就是先开史莱姆之家下载小游戏到电脑裡来玩,因为那时候老师根本不给你网络上网, 但是那时候的小孩都很知足,只要随便一个 ...

  6. 华为盒子显示服务器繁忙,繁忙都市中的“小确幸”

    随着经济和网络的不断发展,现代人的生活被不断挤压,做不完的工作,赶不完的应酬和酒局,在繁忙的都市中似乎永远都慢不下来.走在下班回家的路上,想想自己已经有多久不曾好好读书:有多久没有完整看一部电影:又有 ...

  7. 飞利浦css5530+g评测,影院不开门,给生活多点小确幸,飞利浦CSS5235家庭影院开箱评测...

    原标题:影院不开门,给生活多点小确幸,飞利浦CSS5235家庭影院开箱评测 电影院已经停摆半年了,最后一次买电影票还是春节档,只是被自动退票了.最近的电影要么在电视上看,要么在电脑上看,视觉效果无法改 ...

  8. 小确幸与大抱负:为何自拍类APP成为新宠?

    新春伊始,自拍美图类APP成了香饽饽. 专注于自拍P图的美图,于2017年登陆港股,目前市值达400多亿港元.就在近日,消息称专注于自拍P图的Faceu激萌被今日头条以3亿美金收购.无独有偶,早在今年 ...

  9. 小时候计算机课玩的那个兔子的游戏是什么,童年小确幸 儿时电脑课里我们玩不腻的13个小游戏...

    还记得小时候电脑课一到教室就是先开史莱姆之家下载小游戏到电脑裡来玩,因为那时候老师根本不给你网络上网, 但是那时候的小孩都很知足,只要随便一个小游戏都可以让人玩一个下午. 有时候懒的下载还会直接存在3 ...

最新文章

  1. udp,tcp软件udp客户端发消息,udp服务器收不到,C#网络编程,多级路由间,UDP发送消息客户端接收不正常?...
  2. Ubuntu系统(七)-安装FTP
  3. centos ruby通过rvm更新版本
  4. 两个月计算机考研数学,管综数学考前两个月让你事半功倍的方法
  5. 3583. 整数分组
  6. LeetCode 349. 两个数组的交集(哈希)
  7. 树莓派安装mysql5.6_Linux上安装MySQL5.6
  8. 转载---LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
  9. Java :反射详解
  10. componentDidUpdate vs componentWillReceiveProps
  11. NAT with same subnetwork
  12. Java学习笔记——JDBC
  13. JS字符串转为数值方法详解
  14. 手机计算机隐藏图标,怎么隐藏图标-隐藏小米手机桌面图标文字,只需一小段代码...
  15. ansys通过扫掠(sweep)方法划分网格的方法
  16. 根据银行卡号获取银行名称
  17. 输出10000以内的质数C语言
  18. matlab打开时总闪一下才打开_matlab打不开,只在任务栏闪一下就无反应了怎么办?...
  19. CMS内容管理系统(含小程序,Uni APP) 搭建
  20. 如何使用 cri-docker 解决 Kubernetes 1.24 不支持 Docker 的问题

热门文章

  1. SpringBoot与整合其他技术
  2. 三大设计思路,让你的作品更有吸引力
  3. 铃盛疫情危机管理浅析:员工健康为企业之本
  4. c语言反cot函数,算三角函数的软件 安卓哪些软件可以计算反三角函数
  5. ORA-01008:nbsp;并非所有变量都已绑定
  6. 笑喷---买过的最离谱的东西
  7. 数据库 'MessageManage' 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
  8. User表及相关操作
  9. Python开发项目基于改进高斯混合模型的图割算法
  10. Windows程式开发设计指南(二十)多工和多执行绪