servlet如何始终执行相同的任务?(How can a servlet always perform the same task?)
我希望Servlet始终执行相同的任务。 无论是GET还是POST。 目前我只是从
doPost()
调用doGet()
doPost()
,这很好。然后我尝试重写
service()
方法,我认为它会以同样的方式工作。 但事实并非如此!代码以某种方式执行,但响应不生成网页:
response.getWriter(); response.println(string);
此代码适用于doGet / doPost方法,但不适用于该服务。 为什么?
Servlet的:
class MyWebServlet extends HttpServlet { @Override public void service(ServletRequest request, ServletResponse response) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String string = "teststring"; out.println(string); } }
I want a Servlet to perform always the same tasks. Regardless of if it is a GET or POST. At the moment I just call the
doGet()
fromdoPost()
, which works fine.Then I tried overriding the
service()
method, and I thought it would just work the same way. But it does not!The code somehow gets executed, but the response does not generate the webpage:
response.getWriter(); response.println(string);
This code works for the doGet/doPost methods, but not for the service. Why?
Servlet:
class MyWebServlet extends HttpServlet { @Override public void service(ServletRequest request, ServletResponse response) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String string = "teststring"; out.println(string); } }
原文:https://stackoverflow.com/questions/12637084
最满意答案
那篇文章已经过时了; 它引用了Web API的BETA版本。 如此处所述,已发布的版本中删除了通用
HttpRequestMessage<T>
类。 相反,您应该使用PostAsJsonAsync<T>
扩展方法。 这是一个更新的教程 ,可以为您提供所需的信息。That article is out of date; it is referencing the BETA version of Web API. The generic
HttpRequestMessage<T>
class was removed in the released version as explained here. Instead you should use thePostAsJsonAsync<T>
extension method. Here is a more up-to-date tutorial that should give you what you need.
相关问答
更多-
在web.config中添加以下代码
-
这是通过指定一个空配置来解决的: request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); 我从这里得到了答案 ASP.NET WebApi单元测试与Request.CreateResponse This was solved by specifying an empty configuration: request.Properties.Add(HttpPropertyKeys.Http ...
-
请求标头和内容标头有不同的用途。 虽然请求标头包含有关请求本身和客户端(调用方)的信息,但内容标头描述“实体”或其元数据。 看一下示例http请求: POST /some/url HTTP/1.1 Host: someHost User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/ ...
-
您可以简单地序列化任何HttpRequestMessage ,然后将其存储在任何您想要的位置,例如在文本文件中。 稍后,您可以读取该文件并将其内容反序列化为HttpRequestMessage实例。 You can simply serialize any HttpRequestMessage and then store it wherever you want, e.g. in a text file. Later, you can read the file and deserialize its c ...
-
那篇文章已经过时了; 它引用了Web API的BETA版本。 如此处所述,已发布的版本中删除了通用HttpRequestMessage
类。 相反,您应该使用PostAsJsonAsync 扩展方法。 这是一个更新的教程 ,可以为您提供所需的信息。 That article is out of date; it is referencing the BETA version of Web API. The generic HttpRequestMessage class was removed ... -
容器在注册阶段后阻止任何注册。 这将容器的使用分为两个阶段:注册和解决。 Simple Injector并不是唯一能够做到这一点的容器。 例如,Autofac通过允许用户使用使用Build方法构建容器的ContainerBuilder作为配置阶段的最后一步来进行注册,从而更加明确地实现了这一点。 Simple Injector不允许这样做主要是因为在应用程序生命周期中稍后进行注册很容易导致各种有问题的行为,例如竞争条件。 它还使得DI配置更难理解,因为注册分散在整个应用程序中,而您应该在应用DI时尝试集中注 ...
-
这是一个将HttpClient与HttpRequestMessage相结合的答案。 HttpRequestMessage保存数据和客户端处理数据的发送方式。 WebRequestHandler handler = new WebRequestHandler(); X509Certificate2 certificate = GetMyX509Certificate(); handler.ClientCertificates.Add(certificate); HttpClient client = new ...
-
您也可以在.net 4中使用HttpRequestMessage。 You can use HttpRequestMessage in .net 4 also.
-
Web API - 使用HttpRequestMessage id设置每个线程?(Web API - Set each thread with the HttpRequestMessage id?)[2024-03-18]
对于与上下文相关的问题,您可以使用CallContext.LogicalSetData和CallContext.LogicalGetData ,它只是一个IDictionary,它在上下文之间流动,并具有写时复制(浅拷贝)语义。 由于您的数据是不可变的(根据文档 ),您可以将您的相关ID映射到您的线程托管线程ID: protected async override Task SendAsync( ... -
这是来自我的一个工作系统的代码,它通过HTTPClient提交POST请求。 [Route("resource")] public async Task
CreateResource([FromBody]Resource resource) { if (resource == null) return BadRequest(); dynamic response = null; resource.Topic = GetDataFromSomewhereElse(); ...