我完全不熟悉JAVA。我需要編寫一個將使用從客戶端發送的http post參數的servlet。我已經有一個Java應用程序平臺服務器,可以在那裏部署我的應用程序我需要的是寫一個servlet,它會對發佈的參數做出反應。Servlet消耗http發佈參數
顯然我需要在Servlet中公開這些變量?我做了谷歌,並且遇到了建議使用REST框架來實現這一目標的結果。如果不使用任何額外的框架,就不可能寫平面java代碼(例如運行Tomcat)?樣本或教程的鏈接也會有所幫助。
謝謝
我完全不熟悉JAVA。我需要編寫一個將使用從客戶端發送的http post參數的servlet。我已經有一個Java應用程序平臺服務器,可以在那裏部署我的應用程序我需要的是寫一個servlet,它會對發佈的參數做出反應。Servlet消耗http發佈參數
顯然我需要在Servlet中公開這些變量?我做了谷歌,並且遇到了建議使用REST框架來實現這一目標的結果。如果不使用任何額外的框架,就不可能寫平面java代碼(例如運行Tomcat)?樣本或教程的鏈接也會有所幫助。
謝謝
不,你不需要一個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
如果你想建立與JAX-RS應用程序,在部署到Tomcat之前,您只需在項目中包含幾個jar。您的IDE可能會在構建戰爭文件時爲您做這件事。如果您希望在部署時減少上載的大小,可以將這些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();
}
}
}
擴展的HttpServlet – km1
試用谷歌搜索Java servlet爲http://www.novocode.com/doc/servlet-essentials/ – km1
問題是關於使用REST的絕對必要性。 duffymo提供了一個很好的解釋,可能需要他花2分鐘才能寫出令人敬畏的官方教程(這不是我從谷歌獲得的,或者是不確定的,是最好的)。對他來說,這可能比投票更快 - 這是反過來肯定會幫助一些Java新手。感謝duffymo。 – Tintin