Forms allow the web application to take user input - really that is ALL that forms do.
They take user input and pass it to the web application.
Let us create a form which lets the users register their first and last names.
Write your HelloWorld.jsp to create the form, like this:
<html>
<head>
</head>
<body>
<form method=post action="/HelloWorld/register">
First Name: <input type=text name=firstName>
<br/>
Last Name: <input type=text name=lastName>
<br/>
<input type=submit value="Register">
</form>
</body>
</html>
This form has two 'text' input fields to allow the user to enter first and last name.
It has a submit button which allows the form to pass the information back to the application.
Its 'action' tells the form that data should be sent to the /HelloWorld/register URL.
It also tells the form to use the 'POST' method, as opposed to the GET method.
In very brief, the POST method will put the data in the BODY of the HTTP request.
The GET method would have put it as part of the URL.
The principle is that POST is used for sending data to the server. In return a confirmation is expected.
GET is used for passing parameters to the server. In return the requested resource is expected - it can be a file, image, video, ... even the results of a computation can be considered a resource.
A POST should never be cached or bookmarked, whereas a GET can be cached and bookmarked.
Let us now update our web.xml to direct the /register URL to HelloWorld servlet:
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.confucius.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
Let us now update our HelloWorld.java servlet to handle this request:
package org.confucius;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
String firstName = null;
String lastName = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
firstName = request.getParameter("firstName");
lastName = request.getParameter("lastName");
if (firstName != null
&& lastName != null
&& firstName.length() != 0
&& lastName.length() != 0
&& Pattern.matches("[a-zA-Z]+", firstName)
&& Pattern.matches("[a-zA-Z]+", lastName)){
response.getWriter().write("Accepted: " + firstName + " " + lastName);
}
else{
response.getWriter().write("Error! You entered blank or names with numbers in them.");
}
}
}
Our servlet extracts the first and last names and does a series of checks on them, like checking for numbers in the names. It then accepts or rejects the request.
If you now build and deploy HelloWorld.war, then point your browser to:
http://localhost:8080/HelloWorld/jsp/HelloWorld.jsp
you will be able to play with the form input.
What we just did:
- Created a very basic form for accepting user inputs
- Connected the form to a Servlet
- Processed the form input in the Servlet
- Returned a message back to the user
This pattern of form usage is very fundamental to the web. So much that it has its own name - the MVC model, which we will understand next.
No comments:
Post a Comment