Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass string from servlet to jsp [duplicate]

Tags:

java

jsp

servlets

I had tried to pass string from servlet to jsp .But it never forwarded to jsp .The following are the files

servlet file:

String login = "asd";
request.setAttribute("myname",login);
request.getRequestDispatcher("GetCategory.jsp").forward(request, response); 

jsp file:

<% String name = (String)request.getAttribute("myname");
System.out.println(name);%>
like image 793
Pravin Avatar asked Dec 20 '25 20:12

Pravin


1 Answers

I am a bit rusty in that topic but i think i used the forwarding this way:

String nextJSP = "/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);

So getting the RequestDispatcher from the ServletContext rather than from the request, but im not sure if there is any difference behind the scenes of both calls.

Edit: Where do you set that attribute? (Is it in one of the two Methods doGet/doPost?

like image 178
JBA Avatar answered Dec 22 '25 08:12

JBA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!