CS506 SOLUTION ASSIGNMENT NO 1 2023 - .Docx by Pin2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

CS506 SOLUTION ASSIGNMENT NO

2023 :-
FirstServlet Method
public class FirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String middleName = request.getParameter("middleName");
String lastName = request.getParameter("lastName");

String fullName = firstName + " " + middleName + " " + lastName;

request.setAttribute("fullName", fullName);

// Forward the request to SecondServlet


RequestDispatcher dispatcher = request.getRequestDispatcher("/SecondServlet");
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
SecondServlet Method
public class SecondServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Retrieve the full name attribute from the request


String fullName = (String) request.getAttribute("fullName");

// Set the content type of the response


response.setContentType("text/html");

// Write the full name to the response


response.getWriter().println("<h1>Full Name: " + fullName + "</h1>");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

You might also like