-2

我对 JAVA 完全陌生。我需要编写一个 servlet,它将使用从客户端发送的 http post 参数。我已经有一个 java 应用程序平台服务器,可以在那里部署我的应用程序。我需要的是编写一个 servlet,它会对发布的参数做出反应。

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

谢谢

4

2 回答 2

5

不,您不需要 REST 框架。

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

从 Sun/Oracle 教程开始:

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

以下是父类的 javadocs:

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

于 2012-09-10T20:37:00.303 回答
1

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

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

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();
        }
    }
}
于 2012-09-10T20:42:31.747 回答