Basic Introduction of JSP & Servlets

Introduction to Servlet & JSP

Servlet & JSP

Servlet & JSP

When an user requests for a page through the browser, the request is sent to the server. The requested page may be .html, .jpg, .asp, .jsp, etc., If the requested page is a static page like .html, .jpg, .gif, etc., the server itself sends the response to the requested browser. If the requested page is a dynamic page like .asp, .aspx, .jsp, etc, the request will be sent to the respective web server or application server to execute the page which in turn sends the response as html to the server.

In our case, let us consider our application is hosted in a web server. A web server will contain Web container which handles requests, processes it and sends response to the browser. A web container will contain both static and dynamic pages like .html, .jpg, .js, .jsp, etc. Once a request is made for a jsp page, the web container will create a servlet instance for that particular jsp page and converts into .java file. Only one servlet instance will be created for a jsp page even if multiple request is made for it until the particular jsp file is modified.

When the user requests for a page (either static or dynamic), the request is sent to web server. It contains web container which will process the request received by web server. If the requested page is a static page, the web container sends the response to the browser. If the requested page is a dynamic page (let us consider a jsp page), the web container creates a servlet instance of a jsp page. If required, it will communicate with DB server and returns the response as html code to the browser.

Introduction to Session Management


Session Management

Http protocol is a stateless protocol, which means that it can’t persist the data. Http treats each request as a new request so every time you will send a request you will be considered as a new user.

In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. We can also say that the process of managing the state of a web based client is through the use of session IDs. Session IDs are used to uniquely identify a client browser, while the server side processes are used to associate the session ID with a level of access. Thus, once a client has successfully authenticated to the web applicatiion, the session ID can be used as a stored authentication voucher so that the client does not have to retype their login information with each page request. Now whenever a request goes from this client again, the ID or token will also be passed through the request object so that the server can understand from where the request is coming.
Session management can be achieved by any one of the following four methods:

1. Cookies: cookies are small bits of textual information that a web server sends to a browser and that browsers returns the cookie when it visits the same site again. In cookie the information is stored in the form of a name, value pair. By default the cookie is generated. If the user doesn’t want to use cookies then it can disable them through browser setting.

2. URL rewriting: In URL rewriting we append some extra information on the end of each URL that identifies the session. This URL rewriting can be used where a cookie is disabled. It is a good practice to use URL rewriting. In this session ID information is embedded in the URL, which is recieved by the application through Http GET requests when the client clicks on the links embedded with a page.
Exmaple :

3. Hidden form fields: In hidden form fields the html entry will be like this :

<input type ="hidden" name ="name" value="">

This means that when you submit the form, the specified name and value will be get included in get or post method. In this session ID information would be embedded within the form as a hidden field and submitted with the Http POST method.

4. HttpSession object : javax.servlet.http.HttpSession is an interface that provides a way to identify a user across more than one page request or visit to a web site. This is the way mainly used in webapplication. HttpSession object maintain session for you. You don’t need to do any session management.

String name = session.getAttribute("name");

You will get the same value which you have set.

How does JSP Work



JSP files are dynamically invoked through browsers on requesting the corresponding URLs. These files are usually compiled at the first time when they are displayed on the server. JSPs are translated into servlets at runtime; each JSP’s servlet is cached and re-used until the original JSP is modified.

JSP can be used independently or as the view component of a server-side model–view–controller design, normally with JavaBeans as the model and Java servlets (or a framework such as Apache Struts) as the controller. This is a type of Model 2 architecture.

JSP allows Java code and certain pre-defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver a document. The compiled pages, as well as any dependent Java libraries, use Java bytecode rather than a native software format. Like any other Java program, they must be executed within a Java virtual machine (JVM) that integrates with the server’s host operating system to provide an abstract platform-neutral environment.

JSP pages are usually used to deliver HTML and XML documents, but through the use of OutputStream, they can deliver other types of data as well.

A JavaServer Pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets. A program of this type is usually embedded into the application server and run automatically the first time a JSP is accessed, but pages may also be precompiled for better performance, or compiled as a part of the build process to test for errors.

Some JSP containers support configuring how often the container checks JSP file timestamps to see whether the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development, and a longer interval (perhaps minutes, or even never) for a deployed Web application.

What is MVC

MVC architecture

MVC architecture

Let us consider a Login page for example to explain MVC architecture in a simpler way.

When the user types username and password in login page (VIEW) and hits submit button, the value is set in Java Bean (MODEL) through Servlet Instance (CONTROLLER). Java Bean validates whether the username and password matches each other by communicating with the database through DAO class. It returns success / failure based on the input validation. If the username exists and it matches with the password, the controller returns success and it redirects to welcome.jsp which will display all the information about the user. If Java Bean returns failure, Controller displays error message in login page.

There are two types of MVC:

MVC 1 : JSP acts as a controller

MVC 2 : Servlet acts as a controller

Struts use MVC 2 pattern.

Share This Post

Related Articles

Leave a Reply

© 2017 Techy Diary. All rights reserved.
Powered by Charvi Groups