在运行jsp的时候第一次输入数据库有的账号和密码会成功跳转
但如果输错了会重新回到这个界面,但此时再输入正确的账号和密码会失败
失败原因是因为重新回到的界面req.getParameter()取出来的值为null,只有通过网页的返回键返回后输入才能取到值
这个问题导致我跳转到其他jsp中将值传到servlet中也是空值
应该如何解决
代码后端看上去没有问题,你在jsp那里应该是通过 form表达进行提交的,你先看看打开开发者工具,先试着看看请求的参数是什么
以下答案引用自GPT-3大模型,请合理使用:
在这种情况下,您可以使用Session对象来解决您的问题。Session存储了用户当前会话中的属性,并允许servlet及其他页面在当前会话期间共享这些属性。
您可以使用以下代码将用户信息存储到session中:
// Get a reference to the current session
HttpSession session = request.getSession(true);
// Set the user name and password in the session object
session.setAttribute("username", username);
session.setAttribute("password", password);
然后,您就可以从任何其他servlet或JSP文件中轻松访问它们:
// Get a reference to the current session
HttpSession session = request.getSession(true);
// Get the user name and password from the session object
String username = (String) session.getAttribute("username");
String password = (String) session.getAttribute("password");
如果我的回答解决了您的问题,请采纳我的回答