0% found this document useful (0 votes)
12 views

Code

x

Uploaded by

bc210420793mma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Code

x

Uploaded by

bc210420793mma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

FirstServlet processRequest() Method

protected void processRequest(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException{
//Retrieving form data from the request
String title = request.getParameter("title");
String firstName = request.getParameter("firstName ");
String lastName = request.getParameter("lastName ");

//concatenating the first and last name


String fullName= firstName + " " + lastName;

//Storing the full name in the request attribute


request.setAttributes("fullName",fullName);

//forwading the request to the SecondServlet


RequestDispatcher dispatcher = request.getRequestDispatcher("SecondServlet");
dispatcher.forwad(request,response);
}

_______________

SecondServlet processRequest() Method:

protected void processRequest(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException{
//Retrieving the full name from the request
String fullName = (String) request.getAttributes("fullName");

//displaying the full name


response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.printIn("<html>");
out.printIn("<body>");
out.printIn("<h1> Full Name: "+ fullName +"</h1>");
out.printIn("</body>");
out.printIn("</html>");
}

___________________________________________________________________________________
_______________________________
Final Solution:

FirstServlet processRequest() method:

protected void processRequest(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {
String title = request.getParameter("title");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");

String fullName = firstName + " " + lastName;

request.setAttribute("fullName", fullName);

RequestDispatcher dispatcher =
request.getRequestDispatcher("/SecondServlet");
dispatcher.forward(request, response);
}

SecondServlet processRequest() method:

protected void processRequest(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {
String fullName = (String) request.getAttribute("fullName");

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Full Name: " + fullName + "</h1>");
out.println("</body>");
out.println("</html>");
}

You might also like