使用 servlet 通过 2 个表单发送变量
我遇到了 2 个 servlet 的问题。基本上我有1个带有表单的jsp页面,当我点击提交时,我需要从这个jps中获取所有变量,以便将它们注入第二个页面(输入类型=“隐藏”),最后一起提交。
我的 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");
}
}
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 servlet 1 中,您应该使用 :
而不是 :
因为
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
上收到请求,如果 web 应用程序上下文,它将在http://test/myapp/BankAdvisor/AddAccount
上转发根目录是http://test/myapp
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/toto
it will forward onhttp://test/myapp/toto/BankAdvisor/AddAccount
Whereas 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/toto
it will forward onhttp://test/myapp/BankAdvisor/AddAccount
if the webapp context root ishttp://test/myapp