Results 1 to 6 of 6
  1. #1
    bhauteshved is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default multiple servlets

    I have created a web application with three servlets. Servlet1,Servlet2,Servlet3

    //In Servlet 1 I have
    RequestDispatcher rd = request.getRequestDispatcher("/Servlet2");
    rd.forward(request, response);

    //In Servlet2 I have
    RequestDispatcher rd = request2.getRequestDispatcher("/Servlet3");
    rd2.forward(request, response);

    On clicking the submit button in servlet1, it redirects to servlet2. However, on clicking the submit button in servlet, it redirects to servlet1 instead of servlet3.

    Any ideas to solve the problem.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: multiple servlets

    Can you show all code? It is very short explain.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: multiple servlets

    Servlets do not have submit buttons.
    A submit button is on the client, and sends a request to a server...that request may involve a servlet.
    It's important to remember this, as I see too many people mixing up what happens on the client (that is, the browser) with what happens on the server (usually a servlet or JSP).

    Now, what does the client code say the second submit button is doing?
    How does that map to your servlets on the server?
    What code gets executed?

  4. #4
    bhauteshved is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: multiple servlets

    Here is my code

    //Servlet1
    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
    * Servlet implementation class servlet1
    */
    public class servlet1 extends HttpServlet {

    /**
    *
    */


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");
    String param = request.getParameter("value");
    if(param!=null && !param.equals(""))
    {
    request.setAttribute("value", param);

    RequestDispatcher rd = request.getRequestDispatcher("/Servlet2");
    rd.forward(request, response);

    return;
    }

    PrintWriter out = response.getWriter();
    out.println("<html");
    out.println("<head>");
    out.println("<title>Servlet #1</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>A form from servlet 1</h1>");

    out.println("<form>");
    out.println("<h2>Enter a value to send to servlet 2</h2>");

    out.println("<input name=\"value\"><br>");
    out.println("<input type=\"submit\" value=\"submit\" ");
    out.println("<value=\" send to servlet 2\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");



    }

    }


    //Servlet2
    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class Servlet2
    */
    public class Servlet2 extends HttpServlet {

    /**
    *
    */


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");



    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet #2</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet2</h1>");
    String value = (String)request.getAttribute("value");
    if(value!=null && !value.equals(""))
    {
    out.println("servlet 1 passed a string object via");
    out.println("request scope. the value2 of string is:");
    out.println("<b>"+value+"</b>");
    }
    else
    {
    out.println("no values passed!");
    }

    out.flush();

    String param = request.getParameter("value2");
    if(param!=null && !param.equals(""))
    {
    request.setAttribute("value2", param);

    RequestDispatcher rd = request.getRequestDispatcher("/Servlet3");
    rd.forward(request, response);

    }

    out.println("<form>");
    out.println("<h2>Enter a value to send to servlet 3</h2>");

    out.println("<input name=\"value2\"><br>");
    out.println("<input type=\"submit\" value2=\"submit\" ");
    out.println("<value2=\" send to servlet 3\">");
    out.println("</form>");

    out.println("</body>");
    out.println("</html>");
    }

    }


    //Servlet3
    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class Servlet3
    */
    public class Servlet3 extends HttpServlet {
    /**
    *
    */


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet #3</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet3</h1>");
    String value = (String)request.getAttribute("value2");
    if(value!=null && !value.equals(""))
    {
    out.println("servlet 2 passed a string object via");
    out.println("request scope. the value of string is:");
    out.println("<b>"+value+"</b>");
    }
    else
    {
    out.println("no values passed!");
    }

    out.println("</body>");
    out.println("</html>");
    }

    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: multiple servlets

    Quote Originally Posted by bhauteshved View Post
    Here is my code
    Java Code:
    //Servlet1
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * Servlet implementation class servlet1
     */
    public class servlet1 extends HttpServlet {
    	
    	/**
    	 * 
    	 */
    	
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		
    		response.setContentType("text/html");
    		String param = request.getParameter("value");
    		if(param!=null && !param.equals(""))
    				{
    					request.setAttribute("value", param);
    					
    					RequestDispatcher rd = request.getRequestDispatcher("/Servlet2");
    					rd.forward(request, response);
    					
    					return;
    				}
    		
    		PrintWriter out = response.getWriter();
    		out.println("<html");
    		out.println("<head>");
    		out.println("<title>Servlet #1</title>");
    		out.println("</head>");
    		out.println("<body>");
    		out.println("<h1>A form from servlet 1</h1>");
    		
    		out.println("<form>");
    		out.println("<h2>Enter a value to send to servlet 2</h2>");
    		
    		out.println("<input name=\"value\"><br>");
    		out.println("<input type=\"submit\" value=\"submit\" ");
    		out.println("<value=\" send to servlet 2\">");
    		out.println("</form>");
    		out.println("</body>");
    		out.println("</html>");
    		
    		
    		
    	}
    
    }
    Java Code:
    //Servlet2
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Servlet2
     */
    public class Servlet2 extends HttpServlet {
    
    	/**
    	 * 
    	 */
    	
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		
    		response.setContentType("text/html");
    		
    		
    		
    		PrintWriter out = response.getWriter();
    		out.println("<html>");
    		out.println("<head>");
    		out.println("<title>Servlet #2</title>");
    		out.println("</head>");
    		out.println("<body>");
    		out.println("<h1>Servlet2</h1>");
    		String value = (String)request.getAttribute("value");
    		if(value!=null && !value.equals(""))
    		{
    			out.println("servlet 1 passed a string object via");
    			out.println("request scope. the value2 of string is:");
    			out.println("<b>"+value+"</b>");
    		}
    		else
    		{
    			out.println("no values passed!");				
    		}
    		
    		out.flush();
    		
    		String param = request.getParameter("value2");
    		if(param!=null && !param.equals(""))
    				{
    					request.setAttribute("value2", param);
    					
    					RequestDispatcher rd = request.getRequestDispatcher("/Servlet3");
    					rd.forward(request, response);	
    					
    				}
    		
    		out.println("<form>");
    		out.println("<h2>Enter a value to send to servlet 3</h2>");
    		
    		out.println("<input name=\"value2\"><br>");
    		out.println("<input type=\"submit\" value2=\"submit\" ");
    		out.println("<value2=\" send to servlet 3\">");
    		out.println("</form>");
    		
    		out.println("</body>");
    		out.println("</html>");
    	}
    
    }
    Java Code:
    //Servlet3
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Servlet3
     */
    public class Servlet3 extends HttpServlet {
    	/**
    	 * 
    	 */
    	
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		
    		response.setContentType("text/html");
    		
    		PrintWriter out = response.getWriter();
    		out.println("<html>");
    		out.println("<head>");
    		out.println("<title>Servlet #3</title>");
    		out.println("</head>");
    		out.println("<body>");
    		out.println("<h1>Servlet3</h1>");
    		String value = (String)request.getAttribute("value2");
    		if(value!=null && !value.equals(""))
    		{
    			out.println("servlet 2 passed a string object via");
    			out.println("request scope. the value of string is:");
    			out.println("<b>"+value+"</b>");
    		}
    		else
    		{
    			out.println("no values passed!");				
    		}
    		
    		out.println("</body>");
    		out.println("</html>");
    	}
    
    }
    That should make it more readable.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: multiple servlets

    You forms don't say where to send a submit request do they?
    Or am I missing something?

Similar Threads

  1. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 01:00 PM
  2. Servlets class not teaching servlets
    By tsky in forum Java Servlet
    Replies: 5
    Last Post: 04-26-2011, 10:47 PM
  3. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  4. Servlets
    By javahelp00 in forum New To Java
    Replies: 1
    Last Post: 02-28-2009, 06:28 PM
  5. Servlets
    By nagaroopanandha in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 02:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •