jsp页面用request.getAttribute去取值为空null的问题
jsp页面用request.getAttribute去取值为空的问题
Servlet后台
@WebServlet("/servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
request.setAttribute("user","霓虹身体");
request.getRequestDispatcher("list.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%=request.getAttribute("user")%>
</body>
</html>
启动服务器,访问资源
出现问题:页面值为null
问题原因:直接访问了jsp页面,未访问后台的Servlet文件,就不会执行servlet中的request 数据共享,所以jsp中的request自然为空。
问题解决:
先访问后台Servlet文件,servlet会自动跳转到指定的jsp页面。
问题解决:
先访问后台Servlet文件,servlet会自动跳转到指定的jsp页面。
上一篇:
IDEA上Java项目控制台中文乱码
