Java Server Pages - Testing a Servlet with Forms
In application development, common design challenges can be tackled using software design patterns.
This post will discuss:
- Testing a Servlet with forms
A Brief Reminder
GET and POST request methods are illustrated in the below diagram. They are the method by which the servlet can provide responses to requests from the client.
Testing a Servlet with forms
The client begins by making a POST request to the server.
<form action="http://localhost:8080/MyFirstWeb/spring" method="post">
<input type="text" name="jsp"/>
<input type="text" name="boot"/>
<input type="text" name="jpa"/>
<input type="submit"/>
</form>
The servlet receives a POST request and executes the contents of the method getPost().
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String formA = request.getParameter("formA");
String formB = request.getParameter("formB");
String formC = request.getParameter("formC");
request.setAttribute("formA",formA);
request.setAttribute("formB",formB);
request.setAttribute("formC",formC);
RequestDispatcher dp = request.getRequestDispatcher("servletForm/springResult.jsp");
dp.forward(request, response);
}
The servlet redirects to the JSP page specified above. The JSP page displays the received values.
<body>
${formA}<br>
${formB}<br>
${formC}
</body>
-gonkgonk