jsp servlet完整例子-数据传递全周期

20 篇文章 1 订阅

一个很完整的例子

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值