Skip to content

Commit

Permalink
Add servlet for GET query.
Browse files Browse the repository at this point in the history
URL "/hello"
  • Loading branch information
JackKaif committed Feb 14, 2024
1 parent 508682c commit 549b0e1
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.hexlet.servlet;
package exercise.servlet;

import java.io.IOException;
import java.util.Objects;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
Expand All @@ -11,6 +12,15 @@
@WebServlet(name = "HelloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
// BEGIN

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
var sb = new StringBuilder("Hello, ");
var name = request.getParameter("name");
sb.append(Objects.requireNonNullElse(name, " Guest"));
sb.append("!");
request.setAttribute("message", sb.toString());
request.getRequestDispatcher("WEB-INF/hello.jsp").forward(request, response);
}
// END
}

0 comments on commit 549b0e1

Please sign in to comment.