0% found this document useful (0 votes)
54 views12 pages

WriteUp Computer Networks 1234

The document provides an introduction to web programming and servlets. It discusses static and dynamic websites, and how servlets allow websites to be dynamic by processing requests and generating responses on the server-side using Java. The document also covers servlet lifecycles, APIs, and how to install and use Tomcat server to run servlets.

Uploaded by

rajjukurmi
Copyright
© Attribution Non-Commercial (BY-NC)
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)
54 views12 pages

WriteUp Computer Networks 1234

The document provides an introduction to web programming and servlets. It discusses static and dynamic websites, and how servlets allow websites to be dynamic by processing requests and generating responses on the server-side using Java. The document also covers servlet lifecycles, APIs, and how to install and use Tomcat server to run servlets.

Uploaded by

rajjukurmi
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 12

Introduction To Web Programming Whenever we go to the Internet we have to hit (access) one or more websites to do our work complete

or get the required Information. Broadly websites can be categorized into 2 categories One is Static websites and another one is Dynamic Websites Example of Static website is http://www.sgei.org while the example of Dynamic website is http://www.uptu.org

In the case of static websites who so ever is the user i.e. myself, you or any third party will always get the same web page whenever they access the website. In the case of dynamic websites the new page is generated always as per the requirement of the user. For example in the case of UPTU website when I will insert my roll number I will get my result and when you will insert your roll number, you will be able to access your result not mine Static websites can be building basically using HTML only as it supports text, audio and video basically required for static websites For building Dynamic websites we have broadly 2 technologies through which these can be created .One is Microsoft Technology which provides ASP, ASP DOT NET, VB DOT NET, C SHARP as a front end tool Another one is JAVA Technology, which provides Java Servlets and Java Server Pages (JSP) to build dynamic pages. For building the dynamic websites using Java technologies obviously we have to require a server on which we will keep our pages/class files. There are many servers available out of which any one can be used. For example Java Web Server, J2EE Server, BEA WEBLOGIC, Apache Tomcat Server. Java has provide another s/w which can be downloadable free of cost which is JAVA SERVLET DEVELOPMENT KIT (JSDK2.0) for creating servlets. JSDK will provide the same functionality as normal server and is just like a tool APPLETVIEWER.But it has one drawback (restriction of the port number). We have used Tomcat 4.1 which can be downloadable freely from the website http://apache.org

Servlets

Java servlets are small, platform independent server-side programs that programmatically extend the functionality of web server .The Java Servlet API provides a simple framework for building applications on such web servers. The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages An alternate form of server-side computation that uses Java. Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol.

HOW IT WORKS Read data send by the client (Form data) Generates the result. Send the data back to the client (HTML) ADVANTAGES Only a single copy of class file is loaded into memory. No need to learn new programming languages if you are familiar with Java already. Easy to implement Database connection. Provides an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions and other utilities Run on virtually all operating systems and servers

Servlets Life Cycle


Initialization the servlet engine loads the servlets *.class file in the JVM memory space and initializes any objects Execution when a servlet request is made, a ServletRequest object is sent with all information about the request and a ServletResponse object is used to return the response Destruction the servlet cleans up allocated resources and shuts down Each servlet instance is loaded once. Method init() is only called once during initialization of the servlet Method service() is invoked every time a request comes it. It spawns off threads to perform doGet or doPost based on the method invoked Method service should not be redefined Method doGet and doPost typically need to do perform the same actions Using destroy method the servlet cleans up allocated resources and shuts down

The Servlet API


Servlets use classes and interfaces from two packages: javax.servlet : Contains classes to support generic, protocol-independent servlets and javax.servlet.http: Contains classes to support HTTP-specific functionality Every servlet must implement the javax.servlet.Servlet interface. The servlet packages define two abstract classes that implement the interface Servlet class GenericServlet (from the package javax.servlet) and class HttpServlet (from the package javax.servlet.http). A protocol independent servlet should subclass GenericServlet, while an HTTP servlet should subclass HttpServlet, which itself a subclass of GenericServlet and added HTTP-specific functionality

An HTTP servlet usually overrides doGet() to handle GETrequests and doPost() to handle POST request. The service () method of HttpServlet handles the setup and dispatching to all the doXXX() methods, which is why it usually should not be overridden. The servlet extends the HttpServlet class and overrides the doGet()method inherited from it. Each time the web server receives a GET request for this servlet, the server invokes this doGet() method, passing it an HttpServletRequest object and an HttpServletResponse object. The HttpServletRequest represents the clients request. This object gives a servlet access to information about the client, the parameters for this request, the HTTP headers passed along with the request, and so forth . The HttpServletResponse represents the servlets response. A servlet can use this object to return data to the client.

Servers Installation
Now the problem arises How to installation TOMCAT SERVER 4.1. What settings we have to make to work it properly. What port number we have to use . Other problems are where to write Servlet program. Where to store it .How to compile it and how to run it. ANSWERS 1. Use port number not used by any other service during installation of Tomcat Server. (Mind it you must have installed JDK1.4 or later version before installing TOMCAT server) 2. Use the following settings My ComputerAdvanceEnvironment Variablesadd 3 new system variables (if not already present) and give the values as follows CATALINA_HOME --- C:\Program Files\Apache Software Foundation\Tomcat 4.1 CLASSPATH --C:\j2sdk1.4.0_01\bin; %CATALINA_HOME%\common\lib\servlet.jar; JAVA_HOME--C:\j2sdk1.4.0_01 Path --%JAVA_HOME%\bin; %CATALINA_HOME%\bin;

Writing, compiling, storing and accessing Servlet file


Write a Program in notepad and make its file extension. java Start the server using startup command at cmd Store the file in the folder having path C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\examples\WEB-INF\classes Compile the file after reaching the path C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\examples\WEB-INF\ classes> javac yourfilename.java To check the servlet at browser type the following in the address bar http://localhost: 9090/examples/servlet/yourfilename Do not forget to shutdown the Tomcat properly

COOKIES
Idea is servlet sends a simple name and value to the client. Client returns same name and value when it connects to same site. Cookie is a class in Java used to set a cookie on client side by server and to retrieve the information stored by server Typical uses of cookie Identify a user during E-commerce Session. Avoiding username and password customizing a site Focusing Advertising Why Cookies To transfer the information from one page to another, for example using shopping sites, or your name at mail.yahoo.com

So basically a cookie is a small amount of information sent by a Web server to a Web browser, saved by the browser, and sent back to the server later. Cookies are transmitted inside the HTTP header. Cookies move from server to browser, and back to server as follows: Web Web Local Web Web Server Browser System Browser Server Send Receive Save Send back Receive cookies --> cookies --> cookies --> cookies --> cookies As you can see from the diagram, cookies are actually saved to the hard disk of Web browser user's machines. Many users are concerned about this. But I think it is pretty safe to allow your browser to save cookies. If you are really concerned, you can change your browser's settings to reject cookies. But this may cause many Web based applications fail to run on your browser. Sending and Receiving Cookies

Sending cookies to the browser can be done by the addCookie() method on the response object; while receiving cookies from the browser can be done by the getCookies() method on the request object.

Persistent Cookies
A persistent cookie is one stored as a file on your computer, and it remains there when you close Internet Explorer. The cookie can be read by the Web site that created it when you visit that site again. You can use cookie.setMaxAge(s) to set the cookie to be persistent for 's' seconds. A temporary or session cookie is stored only for your current browsing session, and is deleted from your computer when you close Internet Explorer. You can use cookie.setMaxAge(-1) to set the cookie to be temporary. Other properties of a cookie object that you can set and get:

setPath(): Defines the path name so that when a pages under this path name is requested, the browser will send this cookie to the server. The path name is the url without the server name and the port number parts. setDomain(): Defines the domain name so that when a page under this domain name is requested, the browser will send this cookie to the server. The default domain name is the server name from which the cookie was created. setMaxAge(): Defines how long the cookie should be stored on the browser's machine. MaxAge value is in unit of second. A negative value tells the browser to not save it to the harddisk. A zero value tells the browser to delete it. setVersion(): Defines the version number to which this cookie is compliant with

Conclusion

Cookie is a piece of information the server is asking client the pass it back on the next request. Cookie is best way to link multiple requests into a "session". Cookies are generally safe to accept. Persistent cookies are stored as files on client system. Cookies are passed in HTTP request and response header section. Setting and receiving cookies are easy to do in JSP pages.

Java Server Pages (JSP)


Another way of creating Dynamic Pages The power of JSP is that it is server-based and provides a framework for Web application development JSP have all the advantages of Servlets No special client setup is required They have build in support for HTTP sessions, which makes application programming possible They have full access to java technologies networking awareness, threads, and database connectivity But in addition JSP have advantages of their own. They are automatically recompiled when ever necessary Because JSP pages are HTML-Like they have greater compatibility with web development tools

How JSP Works


JSP page exists in 3 forms/version

JSP source Code (.jsp file) Java Source Code (.java file) Compiled Java File (.class file) JSP Source code Generated Servlet Source Code Compiled servlet class User is totally unaware of these changes /transparent to the user

Difference Between JSP and Servlets


Servlets are fast because they are pure java classes...whereas JSP is slow because first they converted in java code that in class file.... When we use HTML in java code than it is called servlet and when we use Java code is HTML then it is called JSP JSP and Servlet differ each other in terms of representation and execution cycle.

10

11

12

You might also like