乱码优化
package com.example.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class EncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
try {
//将请求,响应对象转换为HTTP协议相关
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//设置编码格式
httpServletRequest.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("text/html;charset=UTF-8");
//放行
chain.doFilter(httpServletRequest,httpServletResponse);
}catch (Exception e){
e.printStackTrace();
}
}
}
登录检查
package com.example.filter;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "LoginFilter", value = {"/addStudent.jsp","/listStudentServlet"})
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
try {
//将请求,响应对象转换为HTTP协议相关
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//获取会话域中的数据
Object username = httpServletRequest.getSession().getAttribute("username");
if(username == null || "".equals(username)){
//重定向到登录页面
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/stu/login.jsp");
return;
}
//放行
chain.doFilter(httpServletRequest,httpServletResponse);
}catch (Exception e){
e.printStackTrace();
}
}
}
优化JSP页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
<title>学生管理系统首页</title>
</head>
<body>
<%--
获取会话域中的数据
如果获取到了则显示添加和查看功能的超链接
如果没获取到则显示登录功能的超链接
--%>
<c:if test="${sessionScope.username eq null}">
<a href="${ctx}/stu/login.jsp">请登录</a>
</c:if>
<c:if test="${sessionScope.username ne null}">
<a href="${ctx}/addStudent.jsp">添加学生</a>
<a href="${ctx}/listStudentServlet">查看学生</a>
</c:if>
</body>
</html>
addStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
<title>添加学生</title>
</head>
<body>
<form action="${ctx}/addStudentServlet" method="get" autocomplete="off">
学生姓名:<input type="text" name="username"> <br>
学生年龄:<input type="number" name="age"> <br>
学生成绩:<input type="number" name="score"> <br>
<button type="submit">保存</button>
</form>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
<title>学生登录</title>
</head>
<body>
<form action="${ctx}/LoginStudentServlet" method="get" autocomplete="off">
姓名:<input type="text" name="username"> <br>
密码:<input type="password" name="password"> <br>
<button type="submit">登录</button>
</form>
</body>
</html>
listStudent.jsp
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.example.domain.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
<title>查看学生</title>
</head>
<body>
<table width="600px" border="1px" align="center">
<tr>
<th>学生姓名</th>
<th>学生年龄</th>
<th>学生成绩</th>
</tr>
<c:forEach items="${sessionScope.students}" var="stu">
<tr align="center">
<td>${stu.username}</td>
<td>${stu.age}</td>
<td>${stu.score}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
报错
java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/ConditionalTagSupport
解决方法
<!--jstl-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!--javax/servlet/jsp/jstl/core/ConditionalTagSupport-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>