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?
Asked
Active
Viewed 3,401 times
0
-
are you trying to create a login ? – Kenneth Clark Jul 31 '18 at 07:52
-
Please post your code , so that it will help others to find solution. – Avijit Barua Jul 31 '18 at 17:10
2 Answers
1
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);
In 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
In JSP:
<div>${username}</div>

Trần Nam Trung
- 103
- 1
- 6
0
Write your Servlet class like this
public class ServletToJSP extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//communicating a simple String message.
String message = "Example source code of Servlet to JSP communication.";
request.setAttribute("message", message);
//Servlet JSP communication
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("jsp url");
reqDispatcher.forward(request,response);
}
}
jsp page
<%
String message = (String) request.getAttribute("message");
out.println("Servlet communicated message to JSP "+ message);
%>

Bill the Lizard
- 398,270
- 210
- 566
- 880

Rohini Rajpal
- 1
- 2