0

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 Answers2

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>
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