+ All Categories
Home > Documents > Java web development Servlet & Java server pages.

Java web development Servlet & Java server pages.

Date post: 23-Dec-2015
Category:
Upload: valentine-greene
View: 251 times
Download: 0 times
Share this document with a friend
Popular Tags:
33
Java web development Servlet & Java server pages
Transcript
Page 1: Java web development Servlet & Java server pages.

Java web developmentServlet & Java server pages

Page 2: Java web development Servlet & Java server pages.

topicsSetting Up a Servlet and JSP Environment *Quick history of web development *CGI *Java servlet *Installing the java 2 Standard

Edition 1.4 (J2SE 1.4). *Installing Jakarta Tomcat 5 on

WindowsJava ServletJava server pages.

Page 3: Java web development Servlet & Java server pages.

Setting Up a Servlet and JSP Environment

Quick history of web developmentIn the beginning , all the web pages is found in the

server as HTML files , to open any web page the client send request to the server by sending the web page name or click on the link for this page, when the request reach to the server ,this server search about this file and send the response to the client ,then the browser show this page at the client.

This is called : RequestResponse ClientServer and the request and the response is HTML file.

This called : static web pages .

Page 4: Java web development Servlet & Java server pages.

request

HTML files

search HTML file

Response (HTML)

Browser view the

web page

RequestResponse ClientServer

Page 5: Java web development Servlet & Java server pages.

Quick history of web development

The number of this static pages become too large and the server can’t keep all these pages.

So the users need dynamic pages such as the search pages , the page contents is changed for each client , this contents is come from database which contains new contents for each client request .

Page 6: Java web development Servlet & Java server pages.

Web server

Browser Request to execute the program

The result (HTML page)

database

The result (HTML page)

Show the page by the browser

If the program need database

Execute the program

The server call the needed program

1

2

3

4

5

6

Quick history of web development

Page 7: Java web development Servlet & Java server pages.

CGIThe Common Gateway Interface , or CGI, is

commonly referred to as one of the first practical technologies for creating dynamic server-side content . with CGI a server passes a client’s request to an external program. This program executes, creates some contents, and sends a response to the client . When first developed, this functionality available to a web developer . Needless to say CGI quickly grew in popularity and become a standard method for creating dynamic web pages.

Page 8: Java web development Servlet & Java server pages.

CGI is not perfect …….Each request to a CGI resource creates a new process on the server and passes information to the process via standard inputs and environment variables .The CGI life cycle is very taxing on server resources and greatly limits the number of concurrent CGI users a server can support.It takes a noticeable amount of time to start and stop the entire process.

Page 9: Java web development Servlet & Java server pages.

Java servlet In the java world, servlets were designed to solve the

problems of CGI . Similar to CGI, servlets allow a request to be processed

by a program and let the same program produce a dynamic response. Servlets additionally defined an efficient life cycle that include the possibility of using a single process for managing all requests. This eliminated the multi_process overhead of CGI and allowed for the mail process to share recourses between multiple servlets and multiple requests.

Page 10: Java web development Servlet & Java server pages.

Installing the java 2 Standard Edition 1.4 (J2SE 1.4).

J2SE is required to run all java codes, go to http://java.sun.com/j2se/1.4/

You can use jcretor or netbeans to compile and run the programs.

Page 11: Java web development Servlet & Java server pages.

Installing Jakarta Tomcat 5 on WindowsApache Jakarta is freely available

for download from http://jakarta.apache.org/tomcat3.

To download tomcat5.5.17 go to http://tomacat.apache.org/download-55.cgi

Why we need the tomcat ? The servlet can’t be execute on any

computer, it must execute on a server computer

Page 12: Java web development Servlet & Java server pages.

Installation steps:1] after download the file,

extract the file on the C drive, change the file name to tomcat, the path off the file become :

c:\tomcat

2] right click on my computer properties advance Environment Variable

3] in System Variable click on new the variable name is JAVA_HOME and the value is the path of the JDK in your computer. This path usually is

C:\Program Files\Java\jdk1.6.0_22

Page 13: Java web development Servlet & Java server pages.

Installation steps:Copy the file servlet-api.jar found in the path : tomcat\libto this path :C:\Program Files\Java\jdk1.6.0_22\jre\lib\extIf you don’t copy this file, this common error will appear : javax.servlet package doesn’t existGo to the path : C:\tomcat\webapps\ROOT\WEB-INFAnd create a new folder name it classes : C:\tomcat\webapps\ROOT\WEB-INF\classes

Now the tomcat is ready, we need to startup the tomcat

Page 14: Java web development Servlet & Java server pages.

Startup the tomcat.

Go to :Start-> run -> cmdUse the command

cd.. until reach to the folder :

c:\tomcat\binWrite the command

startup this window will appear:

Page 15: Java web development Servlet & Java server pages.

Open the server from the browser The port number usually is 8080 you can

sure by search about the port number from the file :

C:\tomcat\conf\sever.xml

Search for this statement

Open the browser then write : http://localhost:8080/

You can use the shutdown command to shutdown the tomcat server.

Page 16: Java web development Servlet & Java server pages.

Open the server from the browser

Page 17: Java web development Servlet & Java server pages.

Execute servlet pages Write the servlet code initially try this code and save it as

NewServlet.java :

Compile the code and but the NewServlet.class file on the path: C:\tomcat\webapps\ROOT\WEB-INF\classes

Page 19: Java web development Servlet & Java server pages.

Use netbeans to install tomcat and execute the servlet pages :

Page 20: Java web development Servlet & Java server pages.

Servlets…import javax.servlet.servlet; interface define the init() method to match the initilaization phase of servlet life cycle.

import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;These two objects represent a client’s request for dynamic resource and the Servlet’s response to the client.

import javax.servlet.http.HttpServlet;

Page 21: Java web development Servlet & Java server pages.

doGet and doPost methods

Page 22: Java web development Servlet & Java server pages.

HTML forms and Servlet code example

Page 23: Java web development Servlet & Java server pages.

Java server pages

JavaServer Pages (JSP) technology enables you to mix regular, static HTML with dynamically generated content from servlets. You simply write the regular HTML in the normal manner, using familiar Web-page-building tools. You then enclose the code for the dynamic parts in special tags, most of which start with <% and end with %>.

Page 24: Java web development Servlet & Java server pages.

Java server pages simple example

Page 25: Java web development Servlet & Java server pages.

JSPA clear important distinction to

make about JSP is that coding one is nothing like coding a servlet.

There are three different types of scripting elements available for use in jsp:

Scriptlets, expression, and declarations.

Page 26: Java web development Servlet & Java server pages.

If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the jsp page. Scriptlets have the following form:<% Java Code %>

JSP Scriptlets

Page 27: Java web development Servlet & Java server pages.

The output.

Page 28: Java web development Servlet & Java server pages.

A JSP expression is used to insert values directly into the output. It has the following form:<%= Java Expression %>The expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run time (when the page is requested) and thus has full access to information about the request. For example, the following shows the date/time that the page was requested:Current time: <%= new java.util.Date() %>

JSP Expressions

Page 29: Java web development Servlet & Java server pages.

Deference between expression and scriptles. if you want output to appear in the resultant

page, you would use the out variable in the scriptles, as in the following example.

<%String queryData = request.getQueryString();out.println("Attached GET data: " + queryData);%>In this particular instance, you could have

accomplished the same effect more easily by using the following JSP expression:

Attached GET data: <%= request.getQueryString() %>

Page 30: Java web development Servlet & Java server pages.

expression

scripltles

Page 31: Java web development Servlet & Java server pages.

The output.

Page 32: Java web development Servlet & Java server pages.

A JSP declaration lets you define methods or fields. A declaration has the following form:<%! Java Code %>Since declarations do not generate any output, they are normally used inconjunction with JSP expressions or scriptlets

JSP Declarations

Page 33: Java web development Servlet & Java server pages.

declaration


Recommended