Reputation: 87
I have ServletDemo1.java class containing Get and Post methods. And I have ServletDemo2.java class containing Get method.
There are two jsp file :- demo1.jsp(have form layout) and demo2.jsp(just to display "Welcome").
This is ServletDemo1.java as follows:-
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("demo1.jsp").include(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("-------demo 1request post-------");
String name = request.getParameter("userName");
String pwd = request.getParameter("userPass");
System.out.println("-------name-------> "+name);
System.out.println("-------pwd-------> "+pwd);
response.sendRedirect("demo2/?userName="+name+"&&pwd="+pwd);
}
}
The ServletDemo2.java as follows :-
@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("-------demo2 request get-------");
request.getRequestDispatcher("demo2.jsp").include(request, response);
}
}
The following steps are executed :-
I load demo1.jsp page calling ServletDemo1.java url.
Fill in the username and password in demo1.jsp and login button is
clicked.
The ServletDemo1.java class Post method is called and in browser it gives 404 not found error.
I want to load demo2.jsp page using Get method of ServletDemo2.java
How can I achieve this?
Upvotes: 0
Views: 2407
Reputation:
try out this
response.sendRedirect(req.getContextPath()+"/demo2/?userName="+name+"&&pwd="+pwd);
Also note some containers(such as Tomcat) has encoding issue/bug with request dispatching, try to utilize client redirect if it's possible.
Upvotes: 1