Java JSP Servlet项目中JSTL库使用问题求助
问题解决步骤
一、解决JSTL标签未识别(未知标签(c:choose)/未知标签(c:when))
Tomcat 10.1 基于 Jakarta EE 9.1,原javax命名空间已全面替换为jakarta,需使用适配Jakarta生态的JSTL版本:
修正Maven依赖
替换原有JSTL依赖为Jakarta版本(包含API与实现包):<dependencies> <!-- JSTL API --> <dependency> <groupId>jakarta.servlet.jsp.jstl</groupId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId> <version>2.0.0</version> <scope>provided</scope> </dependency> <!-- JSTL 实现包 --> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jakarta.servlet.jsp.jstl</artifactId> <version>2.0.0</version> </dependency> </dependencies>若手动放置jar包,需下载
jakarta.servlet.jsp.jstl-api-2.0.0.jar和jakarta.servlet.jsp.jstl-2.0.0.jar放入WEB-INF/lib目录。添加正确的taglib声明
在JSP页面最顶部添加标签库引用:<%@ taglib prefix="c" uri="http://xmlns.jcp.org/jsp/jstl/core" %>
二、修正EL表达式错误
1. HttpServletRequest类型中未定义getGetParameter()
该报错是EL解析异常,且原写法冗余,直接使用EL内置对象param获取请求参数即可:
原代码:
${pageContext.request.getParameter('username').equals('admin') && pageContext.request.getParameter('password').equals('password')}
替换为:
${param.username == 'admin' && param.password == 'password'}
param是EL内置对象,自动映射请求参数,无需手动调用getParameter方法。
2. ServletResponse类型中未定义getSendRedirect()
- 首先,
sendRedirect是HttpServletResponse的专属方法,而非ServletResponse;但核心问题是:绝对不能在JSP的EL中调用重定向。当JSP开始渲染时,响应头已部分发送,此时调用重定向会触发IllegalStateException。
正确做法是将登录逻辑移至Servlet处理:
@WebServlet("/login") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("admin".equals(username) && "password".equals(password)) { response.sendRedirect(request.getContextPath() + "/profile.jsp"); } else { // 处理登录失败逻辑,比如跳转回登录页 response.sendRedirect(request.getContextPath() + "/login.jsp"); } } }
JSP仅负责页面展示,业务逻辑应放在Servlet中实现。
内容的提问来源于stack exchange,提问作者user29184944
相关产品推荐
相关产品推荐

