Tutorias on Web Technology
TUTORIAL I
(Dated: 22-01-2024)
TUTORIAL II
(Dated: 12-02-2024)
TUTORIAL III
(Dated: 04-03-2024)
2. Illustrate with example the process involved in reading the initial parameters of a servlet from servlet descriptor (web.xml).
3. Explain session tracking using cookies in a servlet.
A Session simply means a particular interval of time. Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.
Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize the particular user. The figure given below shows the need for session tracking:
Session Tracking Techniques:
There are four techniques used in Session tracking:
- Cookies
- Hidden Form Field
- URL Rewriting
- HttpSession
Session Handling using Cookies:
Cookies are small pieces of information that are sent in response from the web server to the client. Cookies are the simplest technique used for storing client state.
Cookies are stored on client's computer. They have a lifespan and are destroyed by the client browser at the end of that lifespan.
Cookies are created using Cookie class present in Servlet API. Cookies are added to response object using the addCookie()
method. This method sends cookie information over the HTTP response stream. getCookies()
method is used to access the cookies that are added to response object.
There are two types of cookies. They are Session Cookies and Persistent Cookies.
The session cookies do not have any expiration time. It is present in the browser memory. When the web browser is closed then the cookies are destroyed automatically.
The Persistent cookies have an expiration time. It is stored in the hard drive of the user and it is destroyed based on the expiry time.
When a user Start a web and request information from the website. The website server replies and it sends a cookie. This cookie is put on the hard drive. Next time when you return to the same website your computer will send the cookies back.
Here is a presentation on Session Handling using servlets.
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Three Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<UL>\n" +
" <LI>param1: "
+ request.getParameter("param1") + "\n" +
" <LI>param2: "
+ request.getParameter("param2") + "\n" +
" <LI>param3: "
+ request.getParameter("param3") + "\n" +
"</UL>\n" +
"</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- javax.servlet &
- javax.servlet.http
Servlet:-
- The Servlets runs as a thread in a web-container instead of in a seperate OS process.
- Only one object is created first time when first request comes, other request share the same object.
- Servlet is platform independent.
- Servlet is fast.
GenericServlet:-
- General for all protocol.
- Implements Servlet Interface.
- Use Service method.
HttpServlet:-
- Only for HTTP Protocol.
- Inherit GenericServlet class.
- Use doPost, doGet method instead of service method.
TUTORIAL IV
(Dated: 27-03-2024)
Java Server Pages (JSP) is an extension of Servlet technology that simplifies the delivery of dynamic web content. It provides a set of predefined components for the web application programmers to develop server side scripts that can generate dynamic web pages.
- javax.servlet.jsp &
- javax.servlet.jsp.tagext
JSP often present dynamically generated content as part of an HTML document sent to the client in response to a request. In some cases, the content is static, but is output only if certain conditions are met during a request. JSP programmers can insert Java code and logic in a JSP using script. To run JSP, we need to have a web server that supports JSP and also the packages jsp and jsp.tagext, which are part of the Java Servlet API.
1. Web browser sends an HTTP request to the web server requesting JSPpage.
2. Web server recognizes that the HTTP request by web browser is for JSP page by checking
the extension of the file (i.e.jsp)
3. Web server forwards HTTP Request to JSPengine.
4. JSP engine loads the JSP page from disk and converts it into aservlet
5. JSP engine then compiles the servlet into an executable class and forward original request
to a servletengine.
6. Servlet engine loads and executes the Servletclass.
7. Servlet produces an output in HTMLformat
8. Output produced by servlet engine is then passes to the web server inside an HTTP
response.
9. Web server sends the HTTP response to Web browser in the form of static HTMLcontent.
10. Web browser loads the static page into the browser and thus user can view the dynamically
generatedpage.
JSP components include scriptlets, comments, expressions, declarations and escape sequence characters. Scriptlets are blocks of code delimited by <% and %>. They contain Java statements that the container places in method –jspService at translation time.
JSPs supports three comment styles: JSP comments, HTML comments and comments from the scripting language like JavaScript. JSP comments are delimited by <%-- and --%>. HTML comments are delimited with <!-- and -->. Scripting language comments are Java comments and are delimited by / and /. JSP comments and scripting-language comments are ignored and do not appear in the response to a client.
A JSP expression, delimited by <%= and %> contains Java code that is evaluated when a client requests the JSP containing the expression. The container converts the result of a JSP expression to a String object, and then outputs the string as part of the response to the client.
Declarations (delimited by <%! and %>) enable a JSP programmer to define variables and methods. The variables become instance variables of the Servlet class and the methods become members. Declarations of variables and methods in a JSP use Java syntax. Hence, declaring a variable without using a semicolon is a syntax error.
Escape sequences are sequence of characters used in JSP to make use of the special characters like <% and %> as part of the output. The escape sequences are as follows:
<\% %\> \’ \” \\
JDBC helps the programmers in performing the following activities:
1. It helps a web developer to connect to a data source, like a database.
2. It helps us in sending queries and updating statements to the database, and
3. Retrieving and processing the results received from the database in terms of answering your query.
The following are the major steps involved in developing a JSP page that can connect to and retrieve data from a database:
Step1: Load and Register the Driver
First, you need to load the driver or register it before using it in the program. Registration is to be done once in your program. The forName() method of the Class class is used to register the driver class.
Syntax: public static void forName(String className)throws ClassNotFoundException
You can register the Driver by following two ways:
- Class.forName(): Here we load the driver’s class file into memory at the runtime. No need of using new or creation of objects.
- DriverManager.registerDriver(): DriverManager is a Java inbuilt class with a static member register. Here we call the constructor of the driver class at compile time.
Step2: Establish a Database Connection
In order to establish communication with the database, you must first open a JDBC connection to the database. After loading the driver, establish connections using the DriverManager.getConnection() method. Following are three methods of DriverManager.getConnection():
- getConnection(String url)
- getConnection(String url, Properties prop)
- getConnection(String url, String user, String password)
Syntax: public static Connection getConnection(String url,String name,String password) throws SQLException
Step3: Create the Statement
Once a connection is established you can interact with the database. The createStatement() method of the Connection interface is used to create a statement. The object of the statement is responsible to execute queries with the database.
Syntax: public Statement createStatement()throws SQLException
Step4: Execute the Query
Now it’s time to process the result by executing the query. The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. The executeUpdate(sql query) method of the Statement interface is used to execute queries of updating/inserting.
Syntax: public ResultSet executeQuery(String sql)throws SQLException
Step5: Close the Connection
So finally, we have sent the data to the specified location. By closing the connection object statement and ResultSet will be closed automatically. The close() method of the Connection interface is used to close the connection.