I got a problem with 2 servlets. Basically I got 1 jsp page with form, when I click on submit, i need to get all variables from this jps, in order to inject them into the second on (input type=”hidden”), for finally submit all together.
My servlet 1 (Add client)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("salut 1 ");
request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddClient.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String address = request.getParameter("address");
String city = request.getParameter("city");
String zipcode = request.getParameter("zipcode");
String phone = request.getParameter("phone");
if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {
if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {
request.setAttribute("firstname", firstname);
request.setAttribute("lastname", lastname);
request.setAttribute("email", email);
request.setAttribute("address", address);
request.setAttribute("city", city);
request.setAttribute("zipcode", zipcode);
request.setAttribute("phone", phone);
request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);
} else {
response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
}
} else {
response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
}
}
My servlet 2 (Add account)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddAccount.jsp").forward(request, response);
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String address = request.getParameter("address");
String city = request.getParameter("city");
String zipcode = request.getParameter("zipcode");
String phone = request.getParameter("phone");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String address = request.getParameter("address");
String city = request.getParameter("city");
String zipcode = request.getParameter("zipcode");
String phone = request.getParameter("phone");
//The two news variables
String accountName = request.getParameter("accountname");
String accountType = request.getParameter("accounttype");
if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {
if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {
// ACTION
} else {
response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
}
} else {
response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
}
}
Thanks
In servlet 1 you should use :
instead of :
because with
ServletRequest#getRequestDispatcher(java.lang.String), if the path begins with a “/” it is interpreted as relative to the current context root (i.e. context path of the current request).http://test/myapp/totoit will forward onhttp://test/myapp/toto/BankAdvisor/AddAccountWhereas with
ServletContext#getRequestDispatcher(java.lang.String)path is interpreted as relative to the current context root (i.e. context path of the current web application).http://test/myapp/totoit will forward onhttp://test/myapp/BankAdvisor/AddAccountif the webapp context root ishttp://test/myapp