一个很完整的例子
1,前端页面设计。
2,servlt接收数据。
3,跳转到新页面,servlet传递数据。
4,新页面接收数据。
数据传递的各个周期,全部介绍了了。
3,跳转到新页面,servlet传递数据。
@Override
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
System.out.println("in doget");
String account=req.getParameter("account");
String password=req.getParameter("mypassword");
System.out.println(account);
System.out.println(password);
MyService myService = new MyService(account,password);//调用类Myservice
boolean istrue=myService.istrue();
System.out.println(istrue);
if(istrue){//如果密码正确,重定向到success.jsp
System.out.println("正确");
HttpSession jspSession = req.getSession();
jspSession.setAttribute("account", account);
jspSession.setAttribute("password", password);
resp.sendRedirect("success.jsp");
}else{//密码错误,回login页面
System.err.println("错误");
resp.sendRedirect("http://localhost:8080/JSP_Servlet/");
}
4,新页面接收数据。
<body>
<div><span>你的密码正确:</span></div>
<div>
<ul>
<li>账户:<%=(String)session.getAttribute("account")%> </li>
<li>密码:<%=(String)session.getAttribute("password")%> </li>
</ul>
</div>
<br>
</body>
疑问:
TODO 如果使用response,怎么返回数据?
现在是跳转到新页面,将数据保存在session里面,然后response重定向到新页面。
但是感觉,好像还有其他方式的。
参考例子:
最简单的JSP-Servlet案例
https://blog.csdn.net/zzg19950824/article/details/78462892