Servlet – 获取结果
Servlet 是一个简单的Java程序,运行在服务器上,能够处理来自客户端的请求并为客户端生成动态响应。
如何在 Servlet 中获取结果?
下面逐步描述如下:
- 您可以使用请求对象的getParameter() 方法在 Servlet 中获取 HTML 表单的结果。
- request.getParameter()将HTML 表单的相应输入字段的名称(我们要获取其值)作为参数。然后,此方法将获取/检索在该输入字段中传递的值。
- 将从 request.getParameter 获取的值分配给String变量,因为此方法返回一个 String 值。
- 注意:如果您通过 HTML 表单提交整数或浮点值并希望以相同的数据类型检索它,那么您必须在通过 request.getParameter() 检索时/之后对值进行类型转换,因为此方法仅返回一个字符串值。
为了通过 Servlet 显示结果,打印 String 变量,该变量存储了从 request.getParameter() 检索到的值
步骤如下:
- 使用适当的方法创建 HTML 表单。请注意您在 HTML 表单中使用的方法,无论是获取还是发布。
- 创建一个 Servlet。
- 在 Servlet 内部,相应地在 doGet() 或 doPost() 方法中编写逻辑。
示例: index.html
HTML
Form
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);
}
}
输出:
Note: In the following code, logic is written in doGet() method because, in the (above) HTML form, the method is “get” by default as the method is not explicitly mentioned as the post in the HTML form.
示例: 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);
}
}
输出:
Note: Kindly learn how to create a Servlet before moving on to fetching results.