【发布时间】:2020-08-19 08:53:07
【问题描述】:
目标: 我只有一页,当页面加载时,它应该从 servlet 运行查询并在 index.jsp 页面上显示所有值。
存在的问题: 当我将页面从“提交”按钮提交到另一个页面时,它工作正常,但是当我加载页面 index.jsp 的值时,它会给出 NullPointerException,因为 servlet 没有在 index.jsp 页面之前运行。
我的小服务:
public class GetStudentController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao sd = new StudentDao(); // model
StudentInfo si = sd.getInfo();
request.setAttribute("si", si);
RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
}
我的 JSP:
<body>
<form action="displayStud"> <--my servlet controller name -->
Student id <input type="text" name = "sid">
<button name="test" type="submit"">Primary Button</button>
</body>
</html>
<button type="submit" class="btn btn-primary" name="action" formaction="ddd" value="find">Test2</button>
<!-- <input type ="submit" value ="Submit"> -->
</form>
StudentDao 里面有查询
再次: 我只是希望它在页面加载时运行相同的代码并且所有数据都应该加载(不点击提交)
感谢您的帮助
【问题讨论】:
-
因此,当您点击提交按钮时,导航器会使用正确的数据呈现 jsp(我看不到您如何访问 jsp 中的“si”属性),以及当您做“页面加载”,,什么是页面加载?刷新?这个链接对stackoverflow.com/questions/10898393/…有帮助吗?