'How need to use doPut() method to meet the requirements
I started studying java servlets. In one example, I need to use the doGet() and doPut() methods to meet the required conditions. User can put expression to evaluate or variable values via PUT requests. For set an expression user executes PUT request to /calc/expression URI providing expression as a request body and for set a variable user executes PUT request to /calc/<variable_name> URI where <variable_name> is a variable name and variable value is the request body. For this I created CalcServlet class with methods: doGet() and doPut().
@WebServlet(urlPatterns = {"/calc"})
public class CalcServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uri = req.getRequestURI();
}
}
and I configured web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>CalcServlet</servlet-name>
<servlet-class>CalcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalcServlet</servlet-name>
<url-pattern>/calc</url-pattern>
</servlet-mapping>
</web-app>
I'm trying in the doPut() method to get getRequestURI(), but I don't understand what happen in the doPut() method and what needs to be done next. When run test, expected respone is 1 but appear:
:<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.47 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - /calc/result</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>/calc/result</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.0.47</h3></body></html>
This is a part of test code:
@BeforeAll
public static void startServer() throws Exception {
//tomcat set port and other code...
}
@AfterAll
public static void stopServer() throws Exception {
tomcat.stop();
}
private void testExpression(final String expression, final ImmutableMap<String, Object> params, final int result) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().build())
.build()) {
HttpContext httpContext = createCookieAwareHttpContext();
putArgument("expression", expression, httpClient, httpContext);
for (Map.Entry<String, Object> param : params.entrySet()) {
putArgument(param.getKey(), String.valueOf(param.getValue()), httpClient, httpContext);
}
ResponseRecord response = getResult(httpClient, httpContext);
assertEquals(String.valueOf(result), response.body);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private ResponseRecord getResult(final CloseableHttpClient httpClient, final HttpContext httpContext) {
try {
HttpGet httpGet = new HttpGet(calcUriBuilder("result").build());
try (final CloseableHttpResponse response = httpClient.execute(httpGet, httpContext)) {
return toRecord(response);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private ResponseRecord putArgument(final String name, final String value, final CloseableHttpClient httpClient, final HttpContext httpContext) {
try {
HttpPut httpPut = new HttpPut(calcUriBuilder(name).build());
httpPut.setEntity(makePlainEntity(value));
try (final CloseableHttpResponse response = httpClient.execute(httpPut, httpContext)) {
return toRecord(response);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private ResponseRecord deleteArgument(final String name, final CloseableHttpClient httpClient, final HttpContext httpContext) throws URISyntaxException, IOException {
HttpDelete req = new HttpDelete(calcUriBuilder(name).build());
try (final CloseableHttpResponse response = httpClient.execute(req, httpContext)) {
return toRecord(response);
}
}
private ResponseRecord toRecord(final CloseableHttpResponse response) throws IOException {
final String body = response.getEntity() == null ? null : EntityUtils.toString(response.getEntity());
return new ResponseRecord(
response.getStatusLine().getStatusCode(),
response.getStatusLine().getReasonPhrase(),
body
);
}
private static StringEntity makePlainEntity(final String expression) {
return new StringEntity(expression, ContentType.create("text/plain", Consts.UTF_8));
}
private static URIBuilder calcUriBuilder(final String entity) {
return new URIBuilder()
.setScheme("http")
.setHost("localhost:8080")
.setPathSegments("calc", entity);
}
private static HttpContext createCookieAwareHttpContext() {
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
return httpContext;
}
@Test
public void test001() throws Exception {
testExpression("a+b/c",
ImmutableMap.of("a", 1, "b", 2, "c", 3),
1);
}
If anyone can help me, how do I use the doPut() method to meet the requirements written at the beginning of the post.
May be need more information?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
