我遇到了2个servlet的问题。基本上我有1个带有表单的jsp页面,当我点击提交时,我需要从这个jps中获取所有变量,以便将它们注入第二个(输入类型=“隐藏”),最后一起提交。 / p>
我的servlet 1(添加客户端)
@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");
}
}
我的servlet 2(添加帐户)
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");
}
}
由于
答案 0 :(得分:0)
在servlet 1中,你应该使用:
getServletContext().getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);
而不是:
request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);
因为使用ServletRequest#getRequestDispatcher(java.lang.String)
,如果路径以“/”开头,则将其解释为相对于当前上下文根(即当前请求的上下文路径)。
http://test/myapp/toto
收到了请求,则会转发http://test/myapp/toto/BankAdvisor/AddAccount
而ServletContext#getRequestDispatcher(java.lang.String)
路径被解释为相对于当前上下文根(即当前Web应用程序的上下文路径)。
http://test/myapp/toto
收到了请求,则会在http://test/myapp/BankAdvisor/AddAccount
转发,如果该网址的上下文根为http://test/myapp