通过登录不同的账号,管理员发布,会员查看。
首先写好登录页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style>
* {
margin: 0px;
padding: 0px;
}
</style>
<body>
<form action="login" method="POST">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录">
<input type="reset"></td>
</tr>
<tr>
<!-- 密码错误是的提示语句显示 -->
<td colspan="2" align="center">
<p style="color: red;">${msg }</p>
</td>
</tr>
</table>
</form>
</body>
</html>
简单的表单提交到后台处理,提示语句使用el表达式响应
登录页面如下:
分别创建实体类:
用户类:
package com.bbs.pojo;
public class User {
private String account;
private String password;
private String type;// 0为普通用户 1为管理员
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
用于登陆操作
信息类:
package com.bbs.pojo;
public class Title {
private int on;
private String title;
public int getOn() {
return on;
}
public void setOn(int on) {
this.on = on;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
用于添加和查看信息。
在登录的servlet中,将会使用密码的验证,我将它写在的数据库操作Dao包下,
loginDao:
package com.bbs.Dao;
import com.bbs.pojo.User;
public class UserDao {
public User login(String account, String password) {
// 模拟两个账号,管理员
if (account.equals("gly") && password.equals("123456")) {
User user = new User();
user.setAccount(account);
user.setPassword(password);
user.setType("1");
return user;
// 会员
} else if (account.equals("pt") && password.equals("321654")) {
User user = new User();
user.setAccount(account);
user.setPassword(password);
user.setType("0");
return user;
}
return null;
}
}
处理登录的servlet:
package com.bbs.Servlet;
import java.io.IOException;
import com.bbs.Dao.UserDao;
import com.bbs.pojo.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.Session;
/**
* Servlet implementation class login
*/
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public login() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String account = request.getParameter("account");
String password = request.getParameter("password");
User user = new User();
user = new UserDao().login(account, password);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("index.jsp");
} else {
request.setAttribute("msg", "用户或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
使用转发的方式跳转页面,可以将提示语句传回前端。
登陆成功后的页面如下:
完成发布和查看界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${user.type==1}">
<h3>
欢迎登录,管理员:<b>${user.account}</b>
</h3>
<form action="titleServlet" method="post">
<table>
<tr>
<td>标题</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交">
<input type="reset"></td>
</tr>
<tr colspan="2" align="center">
<td style="color: red;">${msg}</td>
</tr>
</table>
</form>
<a href="showTitle.jsp">查看标题</a>
</c:if>
<c:if test="${user.type==0 }">
<h3>
欢迎登录,会员:<b>${user.account}</b>
</h3>
<br>
<a href="showTitle.jsp">查看标题</a>
</c:if>
</body>
</html>
通过jetl的if判断会员的管理员,显示不同的页面;
管理员多一个发布的表单,会员只有查看发布信息。
将数据添加到数据库,添加数据Dao:
package com.bbs.Dao;
import java.util.ArrayList;
import java.util.List;
import com.bbs.pojo.Title;
public class TitleDao {
public static List<Title> titleList = new ArrayList<Title>();
public void addTitle(String title) {
Title t = new Title();
t.setTitle(title);
if (titleList.size() > 0) {
int id = titleList.get(titleList.size() - 1).getOn();
id++;
t.setOn(id);
} else {
t.setOn(1);
}
titleList.add(t);
}
}
让编号实现自动增长,取出最后一个,累加,集合中没有数据就默认加入1;
实现发布servlet:
package com.bbs.Servlet;
import java.io.IOException;
import com.bbs.Dao.TitleDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class titleServlet
*/
@WebServlet("/titleServlet")
public class titleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public titleServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
TitleDao tdao = new TitleDao();
tdao.addTitle(title);
request.setAttribute("msg", "发帖成功");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
调用操作数据库Dao方法,返回数据。
查看信息页面:
<%@page import="com.bbs.Dao.TitleDao"%>
<%@page import="com.bbs.pojo.Title"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Title> list = TitleDao.titleList;
pageContext.setAttribute("list", list);
%>
<table>
<tr>
<td>编号</td>
<td>标题</td>
</tr>
<c:forEach items="${list}" var="i">
<tr>
<td>${i.on }</td>
<td>${i.title }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
使用jetl的循环遍历list中的数据。
以上便是一个简单的发布消息系统,使用的java+html+servlet+jetl使用MVC架构,各个包用途分明。