3

I want to run two servlet when I click submit button in html page Here is servlet and html code

package com.serv;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            String user=request.getParameter("user");
            out.println("<h2>"+user+"</h2>");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }

}

here is 2nd servlet

package com.serv;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class MyServlet2
 */
@WebServlet("/MyServlet2")
public class MyServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    PrintWriter out1=response.getWriter();
    try {
        String user1=request.getParameter("user1");
        out1.println("<h2>"+user1+"</h2>");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Here is HTML file

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form method="post" action="MyServlet">
        Name<input type="text" name="user">

    </form>
    <form method="post" action="MyServlet2">
        Sir Name<input type="text" name="user1">
    <input type="submit" value="submit">
    </form>

    </body>
</html>

I tried placing submit button at different places but its not working . Can some tell me how shall i write the code so both servlet will be called

user1121210
  • 83
  • 1
  • 4
  • 14
  • a servlet can handle only one http request, you can create a filter(s) if you need pre-procecessing – Athanor Mar 04 '14 at 13:26
  • What would be the use case for that? Usually there should be 1:1 mapping from request to servlet. If you want multiple things to happen, your servlet should make different calls into the business layer. – Gyro Gearless Mar 04 '14 at 13:47

2 Answers2

0

One solution could be to intercept the request with a filter, and forward to both doPost methods from it.

This could help you:

How to use a servlet filter in Java to change an incoming servlet request url?

Another solution would be to make a forward from one servlet to the other. If you can modify the servlets, probably this is better than the filter.

Take a look at this:

http://www.devmanuals.com/tutorials/java/servlet/RequestDispatcher/forward.html

You could also resolve it sending two requests from the browser, using JavaScript, but I understand the scope of the problem is limited to Java.

Community
  • 1
  • 1
Andres
  • 10,561
  • 4
  • 45
  • 63
0

You can pass the request from one servlet to the other using

Request.getRequestDispatcher(String).forward() or Request.getRequestDispatcher(String).include()

based on how you would like to handle the response generated by the servlets. I'm not clear why are you getting user and user1 parameter in different servlets. Instead do everything in a Single Servlet class. Please take a look at Servlet filters mainly intended to preliminary processing of the requests. Read about Servlet Fiters to make use of them appropriately

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53