java 将值从 Servlet 传递到我的 JSP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16300755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:33:33  来源:igfitidea点击:

Passing values from Servlet to my JSP

javajspservlets

提问by Bright

I need to pass the String value h from Servlet to the jsp page. If login is success it will be redirected to success page else it need to display "Invalid login message" in login.jsp page

我需要将字符串值 h 从 Servlet 传递到 jsp 页面。如果登录成功,它将被重定向到成功页面,否则它需要在 login.jsp 页面中显示“无效登录消息”

    if(success)
    {
        RequestDispatcher view=getServletContext().getRequestDispatcher("/Success.html");
        view.forward(request, response);
    }
    else
    {
        String h="Invalid Login";
        RequestDispatcher view=getServletContext().getRequestDispatcher("/Login.jsp");
        view.forward(request, response);
    }

I tried a lot, but its not working.

我尝试了很多,但它不起作用。

回答by Sazzadur Rahaman

Your else block will be like bellow:

您的 else 块将如下所示:

else
{
   String h="Invalid Login";
   request.setAttibute("message",h);
   RequestDispatcher view=getServletContext().getRequestDispatcher("/Login.jsp");
   view.forward(request, response);
}

And in your Login.jspfile you should use:

在您的Login.jsp文件中,您应该使用:

${message}  //(El expression to access value)

And your message will be displayed.

并且将显示您的消息。

回答by Lightning Boy

You can store your data either in request or in session.Then access that value from your jsp page by using sessionScope or simply ${data}.

您可以将数据存储在请求中或会话中。然后通过使用 sessionScope 或简单地 ${data} 从您的 jsp 页面访问该值。

You can set values in request or session by using the code below in your Servlet.

您可以在 Servlet 中使用以下代码在请求或会话中设置值。

request.setAttribute("data",h);
request.getSession.setAttribute("data",h);

In order to access this value in your jsp page. Add the below code in your jsp page

为了在您的 jsp 页面中访问此值。在你的jsp页面添加以下代码

${data} //if you are storing data in request
${sessionScope.data} //If you are storing data in Session 

回答by user2302288

Try to store your data in some scope

尝试将您的数据存储在某个范围内

In servlet

在小服务程序中

   request.setAttribute("Data", h);

in jsp

在jsp中

request.getAttribute("Data")