javax.servlet.http.HttpServlet.doGet()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(227)

本文整理了Java中javax.servlet.http.HttpServlet.doGet()方法的一些代码示例,展示了HttpServlet.doGet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpServlet.doGet()方法的具体详情如下:
包路径:javax.servlet.http.HttpServlet
类名称:HttpServlet
方法名:doGet

HttpServlet.doGet介绍

[英]Called by the server (via the service method) to allow a servlet to handle a GET request.

Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object.

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.

Where possible, set the Content-Length header (with the javax.servlet.ServletResponse#setContentLength method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.

The GET method should be safe, that is, without any side effects for which users are held responsible. For example, most form queries have no side effects. If a client request is intended to change stored data, the request should use some other HTTP method.

The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent.

If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message.
[中]由服务器调用(通过service方法),以允许servlet处理GET请求。
重写此方法以支持GET请求也会自动支持HTTP头请求。HEAD请求是一个GET请求,它在响应中不返回任何主体,只返回请求头字段。
重写此方法时,读取请求数据,写入响应头,获取响应的writer或output stream对象,最后写入响应数据。最好包括内容类型和编码。使用PrintWriter对象返回响应时,请在访问PrintWriter对象之前设置内容类型。
servlet容器必须在提交响应之前写入头,因为在HTTP中,头必须在响应主体之前发送。
在可能的情况下,设置内容长度头(使用javax.servlet.ServletResponse#setContentLength方法),以允许servlet容器使用持久连接将其响应返回给客户端,从而提高性能。如果整个响应适合响应缓冲区,则会自动设置内容长度。
当使用HTTP 1.1分块编码(这意味着响应具有传输编码头)时,不要设置内容长度头。
GET方法应该是安全的,也就是说,没有任何用户要负责的副作用。例如,大多数表单查询没有副作用。如果客户机请求要更改存储的数据,则该请求应使用其他HTTP方法。
GET方法也应该是幂等的,这意味着它可以安全地重复。有时,使方法安全也使其幂等。例如,重复查询既安全又幂等,但在线购买产品或修改数据既不安全也不幂等。
如果请求格式不正确,doGet将返回HTTP“错误请求”消息。

代码示例

代码示例来源:origin: voldemort/voldemort

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
    IOException {
  super.doGet(req, resp);
  Map<String, Object> m = Maps.newHashMap();
  engine.render("query.vm", m, resp.getOutputStream());
}

代码示例来源:origin: javax.servlet/servlet-api

doGet(req, response);
response.setContentLength();

代码示例来源:origin: javax.servlet/servlet-api

doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
  doGet(req, resp);
} else {
  resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

代码示例来源:origin: org.apache.geronimo.specs/geronimo-servlet_3.0_spec

NoBodyResponse response = new NoBodyResponse(resp);
doGet(req, response);
response.setContentLength();

代码示例来源:origin: org.jboss.spec.javax.servlet/jboss-servlet-api_3.0_spec

doGet(req, response);
  response.setContentLength();

代码示例来源:origin: org.apache.geronimo.specs/geronimo-servlet_3.0_spec

doGet(req, resp);
} else {
  long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
    doGet(req, resp);
  } else {
    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

代码示例来源:origin: org.jboss.spec.javax.servlet/jboss-servlet-api_3.0_spec

doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
  doGet(req, resp);
} else {
  resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

代码示例来源:origin: com.caucho/javaee-16

/**
 * Process a HEAD request.  By default, uses doGet.
 *
 * @param req the client request
 * @param res response to the client
 */
protected void doHead(HttpServletRequest req, HttpServletResponse res)
 throws ServletException, IOException
{
 doGet(req, res);
}

代码示例来源:origin: org.openrdf/sesame

protected void _doGet(
  HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException
{
  super.doGet(request, response);
}

代码示例来源:origin: com.bbossgroups/bboss-mvc

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  // TODO Auto-generated method stub
  super.doGet(req, resp);
}

代码示例来源:origin: errai/errai

@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 super.doGet(req, resp);
}

代码示例来源:origin: OneDayNoMore/Hook

@Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);
  }
}

代码示例来源:origin: geoserver/geofence

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
  super.doGet(req, resp);
}

代码示例来源:origin: org.jboss.as/jboss-as-websockets

@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 super.doGet(req, resp);
}

代码示例来源:origin: mikebrock/jboss-websockets

@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 super.doGet(req, resp);
}

代码示例来源:origin: com.atlassian.oauth/atlassian-oauth-shared

protected void doRestrictedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  super.doGet(req, resp);
}

代码示例来源:origin: de.ruedigermoeller/kontraktor-http

protected void unhandledGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  super.doGet(req,resp);
}

代码示例来源:origin: com.ebmwebsourcing.petalsbpm/petalsbpm-utils

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  
  try {
    execute(req, resp);
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  super.doGet(req, resp);
}

代码示例来源:origin: org.jvnet.hudson.winstone/winstone

protected void doHead(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  NoBodyResponse response = new NoBodyResponse(resp);
  doGet(req, response);
  response.setContentLength();
}

代码示例来源:origin: guru.nidi.raml/raml-doc-client

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  if (!initer.waitReady()) {
    super.doGet(req, res);
    return;
  }
  if (req.getPathInfo() == null || req.getPathInfo().length() <= 1) {
    res.sendRedirect(req.getRequestURL().append("/" + IoUtil.urlEncoded(initer.baseDir) + "/index.html").toString().replaceAll("([^:])/+", "$1/"));
    return;
  }
  final String path = req.getPathInfo().substring(1);
  final File source = new File(docDir(), path);
  if (!source.exists() || !source.isFile()) {
    res.sendError(HttpServletResponse.SC_NOT_FOUND);
  } else {
    setContentType(findContentType(source.getName()), res);
    writeOutput(new FileInputStream(source), res);
  }
  res.flushBuffer();
}

相关文章