This is the final part of "Handling user inputs using Sevlets". In the last post we have seen how we can create servlet which will handle our server side processing.
To begin with we need to create an HTML page which will hold our sign up form. We have created sign_up.html page.
When we run our application, setting this html as first welcome file in deployment descriptor (WEB-INF/web.xml) we will be able to see below page in browser.
Now we will create the servlet which will process our form at server end.
Above we have only override doPost method as we are submitting form using POST method. For now we will be printing form values on console. You can save these values to database as well but that's a separate topic which we will cover later.
When below form is submitted we will be able to see below values in console.
To begin with we need to create an HTML page which will hold our sign up form. We have created sign_up.html page.
<html>
<head>
<title>Sign Up</title>
</head>
<body>
<form action="sign_up.do" method="post">
First Name : <input type="text" name="first_name">
<br/><br/>
Last Name : <input type="text" name="last_name">
<br/><br/>
Gender : <input type=radio name="gender" value="male"> Male <input type=radio name="gender" value="female"> Female
<br/><br/>
Favorite Color : <select name="color">
<option value="Red">Red</option>
<option
value="Blue">Blue</option> <option
value="Green">Green</option> </select>
<br/><br/>
<input type="submit" /> <input type="reset" />
</form>
</body>
</html>
When we run our application, setting this html as first welcome file in deployment descriptor (WEB-INF/web.xml) we will be able to see below page in browser.
![]() |
HTML Form |
Now we will create the servlet which will process our form at server end.
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sign_up.do")
public class SignUpServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String gender = request.getParameter("gender");
String color = request.getParameter("color");
System.out.println("Form submitted by user with below value");
System.out.println(firstName+" : "+lastName+" : "+gender+" : "+color);
}
}
Above we have only override doPost method as we are submitting form using POST method. For now we will be printing form values on console. You can save these values to database as well but that's a separate topic which we will cover later.
When below form is submitted we will be able to see below values in console.
This is how we handle user inputs in servlet. Thanks for seeing all my posts on "Handling user inputs using Servlets". There are more to come please follow my blog and give your suggestions, any suggestion given is valuable to me.
No comments:
Post a Comment