Developing Servlets
2.1 Creating and Mapping A Servlet
2.1.1 How a Servlet Application works
Web container is responsible for managing execution of servlets and
JSP pages for Java EE application.
When a request comes in for a servlet, the server hands the request to
the Web Container.
Web Container is responsible for instantiating the servlet or creating
a new thread to handle the request. Its the job of Web Container to get
the request and response to the servlet. The container creates multiple
threads to process multiple requests to a single servlet.
Servlets don't have a main() method. Web Container manages the
life cycle of a Servlet instance.
2.1.2 Mapping a servlet
Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets.
The WEB-INF Directory
Files stored inhere are not supposed to be accessible from a browser (although your web app can access them internally, in your code).
Inside the WEB-INF directory there are (classes and lib, and web.xml).
2.1.3 HTML forms with Servlet
1. Create a HTML form page
2. Create LoginInfo Servlet to receive submitted data from HTML form
2.2 Handling the Client Request
Handling Client Request form data:
-Whenever we want to send an input to a servlet that input must be passed through html form.
-An html form is nothing but various controls are inherited to develop an application.
-Every form will accept client data end it must send to a servlet which resides in server side.
Example of Developing a FORM
Steps:
Use <form>...</form> tag.
To develop various controls through <input>...</input> tag and all <input> tag must be enclosed with in <form>...</form> tag.
<form name="name of the form" action="either absolute or relative address"
method="get or post">
.........
.........
</form>
2.2.1 HTTP Get method
• The GET method sends the encoded user information appended to the page request.
• The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser's Location:box.
• The GET method has size limitation: only 1024 characters can be used in a request string.
• This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable and Servlet handles this type of requests using doGet() method.
2.2.2 HTTP POST method
POST Method
A generally more reliable method of passing information to a backend. Servlet handles this type of requests using doPost() method.
- User entered information is sent as data (not appended to URL)
- Can send any amount of data
2.2.3 Writing servlet to get
parameters values
The doGet () method is invoked by server through service () method to handle a HTTP GET request.
The getParameter () method of HttpServletRequest interface is used to retrieve data attached to the URL sent to the server.
2.2.4 Use forward request
The RequestDispatcher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.
There are two methods defined in the RequestDispatcher interface.
-forward
-include
2.3 Validating Data
2.3.1 VALIDATING DATA ON THE
CLIENT
validation work, JAVASCRIPT code can be embedded into an HTML page to perform simple validation work client-side, before the form data is transmitted across the internet and alert the user to any problems.
2.3.2 VALIDATING DATA ON
SERVER
Example Login Screen Validation
There will be two programs – one running on client machine and the other on server.
I. Client Program – UserPass.html
II. Servlet program – Validation.java