📜  Servlet – 获取结果

📅  最后修改于: 2022-05-13 01:55:05.632000             🧑  作者: Mango

Servlet – 获取结果

Servlet 是一个简单的Java程序,运行在服务器上,能够处理来自客户端的请求并为客户端生成动态响应。

如何在 Servlet 中获取结果?

下面逐步描述如下:

  1. 您可以使用请求对象getParameter() 方法在 Servlet 中获取 HTML 表单的结果。
  2. request.getParameter()HTML 表单的相应输入字段的名称(我们要获取其值)作为参数。然后,此方法将获取/检索在该输入字段中传递的值。
  3. 将从 request.getParameter 获取的值分配给String变量,因为此方法返回一个 String 值。
  4. 注意:如果您通过 HTML 表单提交整数或浮点值并希望以相同的数据类型检索它,那么您必须在通过 request.getParameter() 检索时/之后对值进行类型转换,因为此方法仅返回一个字符串值。

为了通过 Servlet 显示结果,打印 String 变量,该变量存储了从 request.getParameter() 检索到的值

步骤如下  

  1. 使用适当的方法创建 HTML 表单。请注意您在 HTML 表单中使用的方法,无论是获取还是发布。
  2. 创建一个 Servlet。
  3. 在 Servlet 内部,相应地在 doGet() 或 doPost() 方法中编写逻辑。

示例: index.html

HTML




Form


Enter your name:
 


Java
// Java Program to illustrate Fetching Result From a
// Servlets
 
// Importing required classes
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;
 
// Annotation
@WebServlet("/GFGServletFetchResult")
 
// Main class
// Extending HttpServlet class
public class GFGServletFetchResult extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
    {
 
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        out.println("");
        out.println("");
        out.println("");
        out.println("Result Servlet");
        out.println("");
        out.println("

Welcome " + name + "!

");         out.println("");         out.println("");         out.println("");     }       protected void doPost(HttpServletRequest request,                           HttpServletResponse response)         throws ServletException, IOException     {           doGet(request, response);     } }


输出:

index.html 的输出

示例: GFGServlet1。Java 

Java

// Java Program to illustrate Fetching Result From a
// Servlets
 
// Importing required classes
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;
 
// Annotation
@WebServlet("/GFGServletFetchResult")
 
// Main class
// Extending HttpServlet class
public class GFGServletFetchResult extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
    {
 
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        out.println("");
        out.println("");
        out.println("");
        out.println("Result Servlet");
        out.println("");
        out.println("

Welcome " + name + "!

");         out.println("");         out.println("");         out.println("");     }       protected void doPost(HttpServletRequest request,                           HttpServletResponse response)         throws ServletException, IOException     {           doGet(request, response);     } }

输出:

GFGServletFetchResult 的输出。Java