将数据从servlet发送到jsp? [英] Sending data from servlet to jsp?

查看:80
本文介绍了将数据从servlet发送到jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsp中有一个表单,我可以通过doPost方法从中检索数据到servlet,假设用户名和密码现在在同一个表单中有一个空的textarea字段,我想从servlet发送用户名,我该怎么办?这样做吗?

I have a form in a jsp from which i am retrieving data to servlet via doPost method, suppose username and password now there is an empty textarea field inside the same form where i want to send the username from servlet,how do i do it?

推荐答案

我不太了解您的情况.但是,有2种常见的方法将数据从servlet发送到JSP:

I don't really understand your case. But there're 2 common ways to send data from servlet to JSP:

请求属性:如果数据是在同一请求中传输的,则可以使用它.

Request attributes: you can use this if data is transferred along a same request.

request.setAttribute("username",obj);
request.getRequestDispatcher("url").forward(request,response);

在JSP中:

<div>${username}</div>

会话属性:使用该属性在会话期间保留数据

Session attribute: use this to retain data during a session

Session session = request.getSession(true); //true: create session if not existed
session.setAttribute("username",obj);
response.sendRedirect("url"); //or use request dispatcher to forward the request

在JSP中:

<div>${username}</div>

这篇关于将数据从servlet发送到jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆