0% found this document useful (0 votes)
27 views

JWT Unit 1

jwqeuiweh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

JWT Unit 1

jwqeuiweh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT-1

Servlet Basics, Handling the Client Request: Form


Data, HTTP Request Headers
Servlet Basics
Servlets are the Java programs that run on the Java-enabled web server or application
server. They are used to handle the request obtained from the web server, process the
request, produce the response, and then send a response back to the web server.
The properties of Servlets are as follows:
● Servlets work on the server side.
● Servlets are capable of handling complex requests obtained from the web server.
Servlet Architecture can be depicted from the image itself as provided below as follows:
Execution of Servlets basically involves six basic steps:
1. The clients send the request to the web server.
2. The web server receives the request.
3. The web server passes the request to the corresponding servlet.
4. The servlet processes the request and generates the response in the form of output.
5. The servlet sends the response back to the webserver.
6. The web server sends the response back to the client and the client browser displays
it on the screen.
Steps to create a servlet example

The servlet example can be created by three ways:

1. By implementing Servlet interface,


2. By inheriting GenericServlet class, (or)
3. By inheriting HttpServlet class

The mostly used approach is by extending HttpServlet because it


provides http request specific method such as doGet(), doPost(),
doHead() etc.
1. Create a directory structure
2. Create a Servlet
3. Compile the Servlet
4. Create a deployment descriptor
5. Start the server and deploy the project
6. Access the servlet

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

two additional steps:

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

Creating Servlets in Packages


The WEB-INF/classes directory contains all the servlet classes and other
class files, in a structure that matches their package name. For example,
If you have a fully qualified class name of com.myorg.MyServlet, then
this servlet class must be located in the following directory −

/myapp/WEB-INF/classes/com/myorg/MyServlet.class
Compiling Servlets in Packages

There is nothing much different to compile a class available in package. The


simplest way is to keep your java file in fully qualified path, as mentioned
above class would be kept in com.myorg. You would also need to add this
directory in CLASSPATH.

Assuming your environment is setup properly, go in


<Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes directory
and compile MyServlet.java as follows

$ 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

By default, a servlet application is located at the path


<Tomcat-installationdirectory>/webapps/ROOT and the class file would reside in
<Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes.

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:

1. Servlet class is loaded.


2. Servlet instance is created.
3. init method is invoked.
4. service method is invoked.
5. destroy method is invoked.
1) Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is loaded
when the first request for the servlet is received by the web container.

2) Servlet instance is created


The web container creates the instance of a servlet after loading the servlet class.
The servlet instance is created only once in the servlet life cycle.
3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The

init method is used to initialize the servlet. It is the life cycle method of the

javax.servlet.Servlet interface. Syntax of the init method is given below:

1. public void init(ServletConfig config) throws ServletException

4) service method is invoked

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:

1. public void service(ServletRequest request, ServletResponse response) throws


ServletException, IOException
5) destroy method is invoked
The web container calls the destroy method before removing the servlet instance from the
service. It gives the servlet an opportunity to clean up any resource for example memory, thread
etc. The syntax of the destroy method of the Servlet interface is given below:

1. public void destroy()


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.
● 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.

Steps for DEVELOPING a FORM:


1. Use <form>...</form> tag.
2. To develop various controls through <input>...</input> tag and all <input> tag must be enclosed with
in <form>...</form> tag.
3. Every form must call a servlet by using the following:
<form name="name of the form" action="either absolute or relative address"
method="get or post">
.........
.........
</form>
Servlets - Form Data

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 −

http://www.test.com/hello?key1 = value1&key2 = value2

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.

Reading Form Data using Servlet


Servlets handles form data parsing automatically using the following methods depending on
the situation −

getParameter() − You call request.getParameter() method to get the value of a form


parameter.
getParameterValues() − Call this method if the parameter appears more than once and
returns multiple values, for example checkbox.
getParameterNames() − Call this method if you want a complete list of all parameters in
the current request.
GET Method Example using URL
Here is a simple URL which will pass two values to HelloForm program using GET
method.

http://localhost:8080/HelloForm?first_name = ZARA&last_name = ALI

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy