JWT Unit 1
JWT Unit 1
https://www.javatpoint.com/steps-to-create-a-servlet-usi
ng-tomcat-server (go threw this link)
A Servlet That Generates HTML
Most servlets generate HTML, not plain text as in the previous example. To build HTML, you need
1. Tell the browser that you are sending back HTML, and
2. Modify the println statements to build a legal Web page.
You accomplish the first step by setting the HTTP Content-Type response header. In general,
headers are set by the setHeader method of HttpServletResponse, but setting the content type is
such a common task that there is also a special setContentType method just for this purpose. The
way to designate HTML is with a type of text/html, so the code would look like this:
response.setContentType("text/html");
Although HTML is the most common type of document servlets create, it is not unusual
to create other document types. For example, in the previous article (Using Servlets to
Generate GIF Images) shows how servlets can build and return custom images,
specifying a content type of image/gif. As a second example, in (The contentType
Attribute) article shows how to generate and return Excel spreadsheets, using a
content type of application/vnd.ms-excel. Don't be concerned if you are not yet familiar
with HTTP response headers; they are discussed in next article. Note that if you need
to set response headers before actually returning any of the content via the Printwriter.
That's because an HTTP response consists of the status line, one or more headers, a
blank line, and the actual document, in that order.
Servlets - Packaging
/myapp/WEB-INF/classes/com/myorg/MyServlet.class
Compiling Servlets in Packages
$ javac MyServlet.java
If the servlet depends on any other libraries, you have to include those JAR
files on your CLASSPATH as well. I have included only servlet-api.jar JAR
file because I'm not using any other library in Hello World program.
Packaged Servlet Deployment
If you have a fully qualified class name of com.myorg.MyServlet, then this servlet class
must be located in WEB-INF/classes/com/myorg/MyServlet.class and you would need to
create following entries in web.xml file located in
<Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/
<servlet>
<servlet-name> MyServlet</servlet-name>
<servlet-class> com.myorg.MyServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> MyServlet</servlet-name>
<url-pattern> /MyServlet </url-pattern>
</servlet-mapping>
Life Cycle of a Servlet (Servlet Life Cycle)
The web container maintains the life cycle of a servlet instance. Let's see
the life cycle of the servlet:
init method is used to initialize the servlet. It is the life cycle method of the
The web container calls the service method each time when request for the servlet is received.
If servlet is not initialized, it follows the first three steps as described above then calls the
service method. If servlet is initialized, it calls the service method. Notice that servlet is
initialized only once. The syntax of the service method of the Servlet interface is given below:
● 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.
● Since html is a static language which cannot validate the client data. Hence, in real time applications
client data will be accepted with the help of html tags by developing form and every form must call a
servlet.
GET Method
The GET method sends the encoded user information appended to the page request. The
page and the encoded information are separated by the ? (question mark) symbol as
follows −
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. Never use the
GET method if you have password or other sensitive information to pass to the server.
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.
POST Method
A generally more reliable method of passing information to a backend program is the POST
method. This packages the information in exactly the same way as GET method, but instead
of sending it as a text string after a ? (question mark) in the URL it sends it as a separate
message. This message comes to the backend program in the form of the standard input
which you can parse and use for your processing. Servlet handles this type of requests using
doPost() method.
Given below is the HelloForm.java servlet program to handle input given by web
browser. We are going to use getParameter() method which makes it very easy to
access passed information −
Servlets - Client HTTP Request
When a browser requests for a web page, it sends lot of information to the web server which
cannot be read directly because this information travel as a part of header of HTTP request.
You can check HTTP Protocol for more information on this.
Following is the important header information which comes from browser side and you would
use very frequently in web programming −
Methods to read HTTP Header