Servlet消耗http post参数

时间:2012-09-10 20:35:27

标签: java rest servlets httpwebrequest

我是JAVA的新手。我需要编写一个servlet,它将使用从客户端发送的http post参数。我已经有一个Java应用程序平台服务器,可以在那里部署我的应用程序我需要的是编写一个servlet来响应发布的参数。

显然我需要在Servlet中公开这些变量?我做了谷歌并遇到了建议使用REST框架来实现这一目标的结果。这不可能在不使用任何其他框架的情况下编写平面java代码(例如运行Tomcat)吗?样本或教程的链接也会有所帮助。

由于

2 个答案:

答案 0 :(得分:5)

不,您不需要REST框架。

只需编写一个扩展HttpServlet并实现doPost()的servlet。

从Sun / Oracle教程开始:

http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html

以下是父类的javadoc:

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

答案 1 :(得分:1)

如果您想使用jax-RS构建应用程序,只需在部署到Tomcat之前在项目中包含几个jar。您的IDE可能会在构建war文件时为您执行此操作。如果要在部署时减小上载的大小,可以将这些jar移动到Tomcat lib文件夹。但您当然不需要使用REST来访问请求参数。

实际上,所有传统的Web应用都需要捕获http post参数。我编写了一个小实用程序servlet来列出所有参数,以帮助我防止愚蠢的错误,我在HTML中使用一个名称和servlet中的另一个名称调用参数。此代码显示了如何获取请求和会话参数:(请参阅HttpServletRequest docs

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 * This Servlet contains some helpful debugging methods.
  * @author Leon LaSpina
 */
@WebServlet(name = "UtilityServlet", urlPatterns = {"/dev/Utility"})
public class UtilityServlet extends HttpServlet {

    /**
     * This is a utility method for displaying the list of all request parameters
     * sent to a Servlet or JSP.
     * @param request - The HTTP request sent to the Servlet or JSP
     * @param out The stream so that the method may write directly to the web page.
     */
    public static void printMap(HttpServletRequest request, PrintWriter out) {
        java.util.Map<String, String[]> paramMap = request.getParameterMap();
        out.println("<h3>From Data</h3>");
        out.println("<table border='1'><tr>");
        out.print("<td>attribte name</td><td>Attribute Data</td></tr>");
        String[] attribute;
        for (String name : paramMap.keySet()) {
            out.print("<tr>");
            out.println("<td>" + name + "</td><td>");
            attribute = paramMap.get(name);
            if (attribute.length == 1) {
                out.print(attribute[0]);
            } else {
                for (String s : attribute) {
                    out.print(s + ", ");
                }
            }
            out.println("</td></tr>");
        }
        out.println("</table>");
    }

    /**
     * This is a simple utility method for displaying the list of all Session
     * Objects in a simple table.
     * @param request - sent to servlet or JSP
     * @param out the OutputStream so that we may write directly to the web page
     */
    public static void printSessionMap(HttpServletRequest request, PrintWriter out) {
        HttpSession session = request.getSession();
        printSessionMap(session, out);
    }

    /**
     * This is a simple utility method for displaying the list of all Session
     * Objects in a simple table.
     * @param request - sent to servlet or JSP
     * @param out the OutputStream so that we may write directly to the web page
     */
    public static void printSessionMap(HttpSession session, PrintWriter out) {
        java.util.Enumeration<String> names = session.getAttributeNames();
        out.println("<h3>Session Objects</h3>");
        out.println("<table border='1'><tr>");
        out.print("<td>attribte</td><td>DataType</td><td>Object Data</td></tr>");
        while (names.hasMoreElements()) {
            out.print("<tr><td>");
            String attribute = names.nextElement();
            out.print(attribute + "</td><td>");
            out.print(session.getAttribute(attribute).getClass().getName());
            out.print("</td><td>");
            out.println(session.getAttribute(attribute));
            out.println("</td></tr>");
        }
        out.println("</table>");
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //HttpSession theSession = request.getSession();
        try {
            out.println("<html><head>");
            out.println("<title>UtilityServlet</title></head>");
            out.println("<body><h1>UtilityServlet for development</h1>");
            printMap(request, out);
            printSessionMap(request, out);
            out.println("</body></html>");
        } finally {
            out.close();
        }
    }
}