单击提交按钮单击后如何运行多个servlet?

时间:2014-03-04 13:23:23

标签: java html servlets

当我点击html页面中的提交按钮时,我想运行两个servlet 这是servlet和html代码

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();
        }

        }

}

这是第二个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();
    }
}

}

这是HTML文件

<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>

我尝试在不同的地方放置提交按钮,但它不起作用。有人可以告诉我如何编写代码,以便将两个servlet称为

2 个答案:

答案 0 :(得分:0)

一种解决方案可能是使用过滤器拦截请求,并从中转发到两种doPost方法。

这可以帮到你:

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

另一个解决方案是从一个servlet转发到另一个servlet。如果你可以修改servlet,可能这比过滤器好。

看看这个:

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

你也可以解决它使用JavaScript从浏览器发送两个请求,但我知道问题的范围仅限于Java。

答案 1 :(得分:0)

您可以使用

将请求从一个servlet传递到另一个servlet

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

基于您希望如何处理servlet生成的响应。我不清楚你为什么在不同的servlet中得到useruser1参数。而是在Single Servlet类中完成所有操作。请查看主要用于初步处理请求的Servlet过滤器。阅读Servlet Fiters以正确使用它们