<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/details">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<br> <input type="submit" value="Submit">
</form>
</body>
</html>
@WebServlet("/details")
public class ShowDetails extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7384650098624517113L;
public void service(HttpServletRequest request, HttpServletResponse response) {
String fName = request.getParameter("fname");
String lName = request.getParameter("lname");
System.out.println(fName + " " + lName );
}
}
after clicking submit
it must print the value on console
HTML page
Change <form action="/details">
to <form action="/DemoServlet/details">
.
Since the servlet path is relative to /DemoServlet
(you can set this in web.xml
), the ShowDetails
servlet will be at localhost:2222/DemoServlet/details
and not just localhost:2222/details
.