Understanding Java Web Development with Servlet and JSP Environment

Slide Note
Embed
Share

Learn about the evolution of web development from static HTML pages to dynamic pages using Java servlets and JSP. Explore the concept of Request-Response cycle, advantages of dynamic web pages, and the history of CGI technology in creating dynamic server-side content.


Uploaded on Oct 06, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Java web development Servlet & Java server pages

  2. topics Setting 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 Windows Java Servlet Java server pages.

  3. Setting Up a Servlet and JSP Environment Quick history of web development In 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 .

  4. RequestResponse ClientServer Browser view the web page request Response (HTML) HTML file search HTML files

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

  6. Quick history of web development Web server 1 Browser Request to execute the program 5 The result (HTML page) Show the page by the browser 2 6 The server call the needed program 4 The result (HTML page) Execute the program 3 database If the program need database

  7. CGI The 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.

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

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

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

  11. Installing Jakarta Tomcat 5 on Windows Apache 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

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

  13. Installation steps: Copy the file servlet-api.jar found in the path : tomcat\lib to this path : C:\Program Files\Java\jdk1.6.0_22\jre\lib\ext If you don t copy this file, this common error will appear : javax.servlet package doesn t exist Go to the path : C:\tomcat\webapps\ROOT\WEB-INF And 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

  14. Startup the tomcat. Go to : Start-> run -> cmd Use the command cd.. until reach to the folder : c:\tomcat\bin Write the command startup this window will appear:

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

  16. Open the server from the browser

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

  18. Execute servlet pages Write http://localhost:8 080/servlet/New Servlet the output is :

  19. Use netbeans to install tomcat and execute the servlet pages :

  20. 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;

  21. doGet and doPost methods

  22. HTML forms and Servlet code example

  23. 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 %>.

  24. Java server pages simple example

  25. JSP A 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.

  26. JSP Scriptlets 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 %>

  27. The output.

  28. JSP Expressions 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() %>

  29. 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() %>

  30. scripltles expression

  31. The output.

  32. JSP Declarations 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 in conjunction with JSP expressions or scriptlets

  33. declaration

Related


More Related Content