@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String message = "Hello, " + name + "!";
request.setAttribute("message", message);
request.getRequestDispatcher("hello.jsp").forward(request, response);
}
}
html
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>