⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formservlet.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;/** * This servlet demonstrates how to service HTTP GET and POST requests. */public class FormServlet extends HttpServlet {  /**   * Process the HTTP Get request.   */  public void doGet(HttpServletRequest request,                     HttpServletResponse response) throws ServletException,                     IOException {    // Identify this response as an HTML document    response.setContentType("text/html");    PrintWriter out = response.getWriter();    // Start the HTML document    out.println("<html>");    // Set the title of the document    out.println("<head><title>FormServlet</title></head>");    // Start the document body    out.println("<body>");    /**     * print some information about the request     */    // What protocol are we using?    out.println("Using protocol: " + request.getProtocol() + "<br>");    // What URL was requested?    out.println("The URL you requested is ");    String my_url =     // Add the scheme we're using (http)    request.getScheme() + "://" +     // Add the hostname and port the server is using    request.getServerName() + ":" + request.getServerPort() +     // Add the path to the servlet    request.getServletPath();    out.println(my_url + "<br>");    // Print the requestor's IP address    out.println("Your computer is " + request.getRemoteHost() + "<br>");    out.println("<hr>");    // Show all the headers    Enumeration headerNames = request.getHeaderNames();    while (headerNames.hasMoreElements()) {      String headerName = (String) headerNames.nextElement();      String headerValue = request.getHeader(headerName);      out.println("Header:" + headerName + " = " + headerValue + "<br>");    }     out.println("<hr>");    // Print a form asking for a name and a favorite color.    // Note that the ACTION refers the form back to this servlet    // and the method will be POST rather than GET.    out.println("<FORM METHOD=\"post\" ACTION=\"" + my_url + "\">");    out.println("<br>");    out.println("What is your name: <INPUT TYPE=\"text\" NAME=\"name\" >");    out.println("<br>");    out      .println("What is your favorite color: <INPUT TYPE=\"text\" NAME=\"color\" >");    out.println("<br>");    out.println("<INPUT TYPE=\"submit\" >");    out.println("<br>");    out.println("</FORM>");    out.println("</body></html>");  }   /**   * Process the HTTP POST request.  This is called when the user submits the form.   */  protected void doPost(HttpServletRequest request, HttpServletResponse response)           throws javax.servlet.ServletException, java.io.IOException {    // Identify this response as an HTML document    response.setContentType("text/html");    PrintWriter out = response.getWriter();    out.println("<html>");    out.println("<head><title>FormServlet</title></head>");    out.println("<body>");    // Get the values for the "name" and "color" form parameters.    out.println("Your name is " + request.getParameter("name") + "<br>");    out.println("Your favorite color is " + request.getParameter("color")                 + "<br>");    out.println("</body></html>");  } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -