📜  Servlet – 响应

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

Servlet – 响应

使用 Servlet 技术构建 Web 应用程序(驻留在服务器端并生成动态网页)。由于Java编程语言,servlet 技术是可靠和可扩展的。 CGI(通用网关接口)脚本语言在 Servlet 之前被广泛用作服务器端编程语言。

Servlet – 响应

servlet 可以使用此对象来帮助它向客户端提供响应。 ServletResponse 对象由 servlet 容器创建,并作为参数传递给 servlet 的服务函数。

使用 getOutputStream 提供的 ServletOutputStream 在 MIME 正文响应 () 中传递二进制数据。使用getWriter给出的PrintWriter对象传递字符数据()。使用 ServletOutputStream 并手动控制字符部分以混合二进制和文本数据,例如生成多部分响应。

setCharacterEncoding(Java.lang.String) 和 setContentType(Java.lang.String) 方法可用于为 MIME 正文响应提供字符集,或者 setLocale(Java.util.Locale) 方法可用于隐式指定它.隐式需求被显式规范覆盖。如果未提供字符集,将使用 ISO-8859-1。对于要使用的字符编码,必须在 getWriter 和提交响应之前调用 setCharacterEncoding、setContentType 或 setLocale 方法。

ServletResponse的一些重要方法

Methods

Description

String getCharacterEncoding()It returns the name of the MIME charset that was used in the body of the client response.
String getContentType()It returns the response content type. e.g. text, HTML etc.
 ServletOutputStream getOutputStream()This method returns a ServletOutputStream that may be used to write binary data to the response.
PrintWriter getWriter()The PrintWriter object is used to transmit character text to the client.
void setContentLength(int len)Sets the length of the response’s content body. This function sets the HTTP Content-Length header in HTTP servlets.
void setContentType(String type)Sets the type of the response data.
void setBufferSize(int size)specifies the recommended buffer size for the response’s body.
int getBufferSize()Returns the buffer size
void flushBuffer()Any material in the buffer will be forced to be written to the client.
boolean isCommitted()If the response has been committed, this method returns a boolean.
void setLocale(Locale loc)If the answer hasn’t been committed yet, it sets the response’s location.
void reset()Clears the buffer’s data, as well as the headers and status code. To acquire a comprehensive list of ways, go here.

实现:在下面的示例中使用了 ServletResponse 接口的 setContentType() 和 getWriter() 方法。

A.文件: 索引.html

HTML


 GEEKSFORGEEKS 
 Enter your username: 





Java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class GFG extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
   {
     res.setContentType("text/html");
     PrintWriter pwriter=res.getWriter();
     String name=req.getParameter("uname");
     pwriter.println("This is user details page:");
     pwriter.println("Hello "+name);
     pwriter.close();
  }
}


XML


   GFG
   GFG

 
   GFG
   /GFG



B.应用文件(Java)

Java

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class GFG extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
   {
     res.setContentType("text/html");
     PrintWriter pwriter=res.getWriter();
     String name=req.getParameter("uname");
     pwriter.println("This is user details page:");
     pwriter.println("Hello "+name);
     pwriter.close();
  }
}

C.web.xml

XML



   GFG
   GFG

 
   GFG
   /GFG


输出:

第一个屏幕显示以下输出:

第二个屏幕显示以下输出: